user69032: I 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

user57952: 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).

69032: <p><a href="https://i.stack.imgur.com/IFqGM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IFqGM.png" alt="enter image description here" /></a>I 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.</p>
<pre><code>#include &lt;dht.h&gt;
#include &lt;Servo.h&gt;

#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() &gt; 0){ // Checks whether data is comming from the serial port
      logicVal = Serial.read(); // Reads the data from the serial port
   }
   
 if(millis()-curTime&gt;=10000){
    int chk = DHT.read11(DHT11_PIN);
    gasVal=analogRead(gasSens);
    Serial.print(DHT.temperature);
    Serial.print(&quot; C&quot;);
    Serial.print(&quot;|&quot;);
    Serial.print(DHT.humidity);
    Serial.print(&quot;%&quot;);
    Serial.print(&quot;|&quot;);
    Serial.print(gasVal);
    Serial.print(&quot;|&quot;);
    if(gasVal&gt;500){Serial.println(&quot;Dangerous gas levels&quot;);buzzerTrig=1;}
    else{Serial.println(&quot;normal&quot;);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 &amp;&amp; rightIr==HIGH){
        stopAll();
        while(leftIr==LOW){
          rightMov();
          leftIr=digitalRead(lftIr);
        }
        stopAll();
       }
       else if (leftIr==HIGH &amp;&amp; rightIr==LOW){
        stopAll();
          while(rightIr==LOW){
            leftMov();
            rightIr=digitalRead(rghIr);
          }
        stopAll(); 
      }
      else if (leftIr==LOW &amp;&amp; centerIr==LOW &amp;&amp; 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 &amp;&amp; centerIr==LOW &amp;&amp; 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);
 }
</code></pre>
<p>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</p>
 57952: <p>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.</p>
<p>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 &amp; 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).</p>
