1

enter image description hereI have been working on a 4WD robot for a while now. The parts I'm using are Arduino v4 shield, l298N driver motor, Hc 05 ultrasonic sensor, DHT 11, mq2, Bluetooth module, servo motor, ir transmitter and receiver module and an uno all powered by 2 18650 Li ion batteries and a power bank. when i tried using the Ir sensors only, ultrasonic only or both for obstacle detection and avoidance the code freezes and nothing works anymore until i move the obstacle away then the robot makes the decision it wanted to make. it also used to reset occasionally while frozen but i fixed that issue by powering the Arduino separately. i noticed this problem mostly occurs when all the IRs give a low signal. i think the problem is with my code but i don't really see what i typed wrong.

#include <dht.h>
#include <Servo.h>

#define servoPin 11

// Create a servo object 
Servo Servo1; 

//Ultrasonic Sensor
#define trigPin 9
#define echoPin 10
// defines variables
long duration;
int distance;

//sensor pins
#define gasSens A4
dht DHT;

#define DHT11_PIN 2

//motor driver pins
#define in1 8
#define in2 7
#define in3 6
#define in4 5

//infared pins
#define lftIr 12
#define ctrIr 4
#define rghIr 3

#define buzzer 13

int dist=1000;
int lastVal=0;
int degVal;
int leftDist;
int rightDist;
int gasVal;
int logicVal=49;
int buzzerTrig=0;
unsigned long curTime;

void setup() {
  // put your setup code here, to run once:
  Servo1.attach(servoPin); 
  Servo1.write(100); 

  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  
  pinMode(in1, OUTPUT);
  pinMode(in2, OUTPUT);
  pinMode(in3, OUTPUT);
  pinMode(in4, OUTPUT);

  pinMode(lftIr,INPUT);
  pinMode(ctrIr,INPUT);
  pinMode(rghIr,INPUT);

  pinMode(buzzer, OUTPUT);
  
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);

  curTime=millis();

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0){ // Checks whether data is comming from the serial port
      logicVal = Serial.read(); // Reads the data from the serial port
   }
   
 if(millis()-curTime>=10000){
    int chk = DHT.read11(DHT11_PIN);
    gasVal=analogRead(gasSens);
    Serial.print(DHT.temperature);
    Serial.print(" C");
    Serial.print("|");
    Serial.print(DHT.humidity);
    Serial.print("%");
    Serial.print("|");
    Serial.print(gasVal);
    Serial.print("|");
    if(gasVal>500){Serial.println("Dangerous gas levels");buzzerTrig=1;}
    else{Serial.println("normal");buzzerTrig=0;}
    curTime=millis();
  }

 if(buzzerTrig==1){
  buzzerCall();
  logicVal==48;
  }
 
 if(logicVal=='1'){
      dist=distanceCall();
      
      int leftIr=digitalRead(lftIr);
      int centerIr=digitalRead(ctrIr);
      int rightIr=digitalRead(rghIr);
      
      if (leftIr==LOW && rightIr==HIGH){
        stopAll();
        while(leftIr==LOW){
          rightMov();
          leftIr=digitalRead(lftIr);
        }
        stopAll();
       }
       else if (leftIr==HIGH && rightIr==LOW){
        stopAll();
          while(rightIr==LOW){
            leftMov();
            rightIr=digitalRead(rghIr);
          }
        stopAll(); 
      }
      else if (leftIr==LOW && centerIr==LOW && rightIr==LOW){
          stopAll();
              while(leftIr==LOW || centerIr==LOW || rightIr==LOW){
                backMov;
                leftIr=digitalRead(lftIr);
                centerIr=digitalRead(ctrIr);
                rightIr=digitalRead(rghIr);
                }
                stopAll();
          }
          
      else if (leftIr==HIGH && centerIr==LOW && rightIr==HIGH){
          stopAll();
          while(centerIr==LOW){
            backMov;
            centerIr=digitalRead(ctrIr);
          }
          stopAll();
          delay(10);
          leftMov();
          delay(500);
          stopAll();
        }
      else{
        fowardMov();
      }
    }
}

int distanceCall(){
  // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance= duration*0.034/2;
    return distance;
}

void fowardMov(){
  digitalWrite(in2,HIGH);
  digitalWrite(in4,HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in3, LOW);
}

void backMov(){
  digitalWrite(in1,HIGH);
  digitalWrite(in3,HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in4, LOW);
}

void rightMov(){
  digitalWrite(in1, HIGH);
  digitalWrite(in4, HIGH);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
}

void leftMov(){
  digitalWrite(in2, HIGH);
  digitalWrite(in3, HIGH);
  digitalWrite(in1, LOW);
  digitalWrite(in4, LOW);
}

void stopAll(){
  digitalWrite(in1, LOW);
  digitalWrite(in2, LOW);
  digitalWrite(in3, LOW);
  digitalWrite(in4, LOW);
}

void buzzerCall(){
    tone(buzzer, 5000);
    delay(500);
    noTone(buzzer);
    tone(buzzer, 1000);
    delay(500);
    noTone(buzzer);
 }

The goal is for the robot to avoid obstacles and move autonomously using only the IR sensors. I used to power everything with the batteries but sometimes it shakes and moves slowly so i decided to power them seperately. Do you think the problem is with my code or it's a hardware problem? Thank you

5
  • this seems to be an error logicVal==48; ... what is the purpose of logicVal? ... a more descriptive name would be useful ... assigning a numerical value and testing a character value tends to be confusing – jsotola Oct 14 '20 at 23:21
  • i move the obstacle away then the robot makes the decision it wanted to make and, what decision would that be? .. you did not say anything about how you expect the robot to behave, and how it should react ... there is no way to know if parts of code actually are meant to do what they do – jsotola Oct 14 '20 at 23:23
  • Without a bit more information it will be difficult to give you much help. At a minimum, we need a wiring diagram and some more specifics about the DHT library you are using, as there are several. Also, it appears you have code for a single ultrasonic sensor that is not used at all. I have some fairly extensive experience with the HC-04/05 ultrasonic sensors and may be able to help you sort out the problems, but not if we don't have some idea about what you are trying to accomplish – user3765883 Oct 15 '20 at 0:45
  • @jsotola i'm sorry if i confused you but i forgot to mention i made an app with MIT app builder to send data to switch the robot on or off and also to display sensor data. what i expected to happen is depending on which Ir goes on first .the robot should move away until that Ir reads high. it works for the left and right Ir but for the center it freezes. it also freezes when all the sensors read low or when the center and another read low. when i remove the obstacle in front of it then the robot turns left right or even turns around. Thank you for helping i hope you understand my problem now – Ayanfe Adekanye Oct 15 '20 at 7:51
  • @user3765883 i'm sorry i will add my schematic to the post I don't know who the dht library i'm using was created by i just downloaded it from the tutorial by circuit basics on how to use a dht (circuitbasics.com/…) Initially i used an ultrasonic sensor but it didn't really work well especially when it faced fabric so i decided to combine it with Ir but after a test my robot hit something then my servo stopped working it's hard and expensive to get parts in my country so i decided to use only IR thats why the variables are there – Ayanfe Adekanye Oct 15 '20 at 8:05
1

I'm not surprised that you are having trouble, as you have a very complicated system. When troubleshooting complicated systems it is important to break it down into very simple 'chunks' and make sure each simple piece works separately.

I would remove everything but the code for the three IR sensors from your system, and use a random number generator to generate a random number between 0 & 7. Then I would use the binary representation of that number to generate the return values for the digitalRead() calls to the sensors, with appropriate printouts to show you what values were generated and what effect those values produced. I think you'll find that one or more of the 8 possible values causes problems, as you only have IF clauses for 4 (or 6 if you count the two 2-valued systems twice).

7
  • Thank you for helping. I took your advice and removed everything except the code for the IR. i also generated random values between 0 and 1 to see what causes the code to freeze. The code doesn't freeze anymore everything works fine for some reason now. maybe the problem was from another part of my code. What do you think? Thank you. – Ayanfe Adekanye Oct 16 '20 at 17:54
  • I decided to modify the robot a bit. i'm now using 3 li ion batteries to power both the Arduino and the motors. I tried the script again with only Ir. instead of generating random numbers i decided to see how it'll work with obstacles. it didn't work.The robot froze immediately it got to a wall so i used the serial monitor to check it out. The problem is with the center IR anytime it reads low first although on the serial monitor the message prints the robot freezes. Do you think i should use PWM to make the robot slower? Would that improve obstacle detection or it won't matter – Ayanfe Adekanye Oct 16 '20 at 21:15
  • Congratulations on making progress! I'm not sure that going slower will solve your problem, but I doubt it will. You probably have an issue with your program logic that, if left unsolved, will continue to make your robot unreliable. The good news is, now that you know you have a problem, you should be able to figure it out. Maybe some more print statements at different points so that you can build a more complete picture of what is actually happening with your logic? The Mega is undoubtedly executing the programmed logic correctly, but it's also probably not doing what you think ;-). – user3765883 Oct 17 '20 at 1:45
  • Thank you very much for your help i have seen the problem. The issue was when the center IR reads low and it enters the while loop it does not move because i did not call the function properly i typed backMov; instead of backMov(); now it works a lot better but i noticed that the UNO resets every time its faces an obstacle. It still avoids the obstacle despite the reset but i don't know why it resets Could it be because the batteries are low? I don't even get a lot of use time with it and i feel it might be due to the fact that i made my own battery holder with easy materials what do you think – Ayanfe Adekanye Oct 17 '20 at 9:16
  • If the Mega is resetting it is almost certain it is due to something drawing more current and causing the Mega's internal voltage to drop below 5V. If you are using the barrel connector or Vin for supplying power to the Mega, there is about a 2V drop across the internal voltage regulator. – user3765883 Oct 17 '20 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.