Ultrasonic Sensors Sending back low state infinite loop help

I am creating an automatic lawnmower that can cut a lawn in a fenced off area and can detect objects and avoid them by cutting in a random cut order kind of like Roomba does.

Basically my code would make my lawnmower move forward till one of the sensors sensed something. Then depending on which sensor is tripped the lawnmower would stop, reverse, then finally rotate before moving forward again.

I have built my lawnmower in small scale to confirm my code was working correctly, but now when implementing my design into the real prototype my right sensor keeps sending a low signal never letting my lawnmower move forward and keep on stopping reversing then rotating infinitely.

Any ways on how to bypass/fix this problem would be greatly appreciated. The code I am using is below.

     void loop() { 
      //amount of time to cut lawn
      time_to_cut = millis(); 
      while((millis() - time_to_cut) < interval){ 

      //speed of the motor 
      analogWrite(speedPin,225);       // originally 225
      analogWrite(speedPin2,225);     // originally 225
     
  
      digitalWrite(trigPin1, HIGH);    // this is telling the sensors to sense
      delayMicroseconds(10); 
      digitalWrite(trigPin1, LOW); 
      duration1 = pulseIn(echoPin1, HIGH); 
      distance1 = duration1/58.2; 
      delay(50); 
      digitalWrite(trigPin2, HIGH); 
      delayMicroseconds(10); 
      digitalWrite(trigPin2, LOW); 
      duration2 = pulseIn(echoPin2, HIGH); 
      distance2 = duration2/58.2; 
      delay(50); 
      digitalWrite(trigPin3, HIGH); 
      delayMicroseconds(10); 
      digitalWrite(trigPin3, LOW); 
      duration3 = pulseIn(echoPin3, HIGH); 
      distance3 = duration3/58.2; 
      delay(50); 
       
      if(distance1 <= 20 || distance2 <= 20 || distance3 <=20){  
        if(distance1 <= 20){ 
          randNumber = 0; 
        } 
        else if(distance3 <= 20){   //2nd if  ` 
          randNumber = 1; 
        } 
        else{             
        randNumber = random(2);} 
        randNumber2 = random(1000, 3001); 
        delay(10); 
        if(randNumber == 1){                 //
          digitalWrite(dir1, LOW);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, LOW);       
          digitalWrite(dir4, LOW); 
          delay(1000);                        //stop for 1 second
          digitalWrite(dir1, HIGH);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, HIGH);       
          digitalWrite(dir4, LOW); 
          delay(2000);                        //reverse for 2 second
          digitalWrite(dir1, HIGH);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, LOW);       
          digitalWrite(dir4, LOW); 
          delay(randNumber2);                 //reverse front right motor
          } 
        if(randNumber == 0){ 
          digitalWrite(dir1, LOW);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, LOW);       
          digitalWrite(dir4, LOW); 
          delay(1000);                       //stop for 1 second
          digitalWrite(dir1, HIGH);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, HIGH);       
          digitalWrite(dir4, LOW); 
          delay(2000);                       //reverse for 2 second
          digitalWrite(dir1, LOW);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, HIGH);       
          digitalWrite(dir4, LOW); 
          delay(randNumber2);                //reverse front left motor
          } 
        else{} 
      } 
      else{ 
      // Move 
      digitalWrite(dir1, LOW);       
      digitalWrite(dir2, HIGH); 
      digitalWrite(dir3, LOW);       
      digitalWrite(dir4, HIGH); 
      } 
  
    } 
    while((millis() - time_to_cut) > interval) { 
          digitalWrite(dir1, LOW);       
          digitalWrite(dir2, LOW); 
          digitalWrite(dir3, LOW);       
          digitalWrite(dir4, LOW); 
          } 
    } 

Maybe this is a hardware issue and not a code issue?

What about just writing some code which reads all 3 sensors and dumps the 3 calculated distances to the serial port each time through loop(). Then you can place things in front of each sensor to see how well they are working. If you find that one is always giving an incorrect reading, you could switch cables to see if the problem follows the sensor or not.

1 Like

So are you are saying the right sensor is not returning a pulse? I would think that is a hardware problem. Perhaps a connection or damaged sensor.

Comment:
I am not a "accomplished" coder but I would keep everything in µs. When you divide by 58.2 you force the result to be a float. Not needed in your situation. You would need to change your distance tests to µs but you should be working with integers throughout.

Suggestion:
If you add a OLED display (easy to to) you can have a troubleshooting device right on the mower. I suggest the 1.3" (128 x 64)

Hi,
Have you looked at NewPing library to make your code even simpler to use?

Google;

NewPing library arduino 3 hc-sr04

Tom.. :smiley: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.