0
\$\begingroup\$

I am doing my final-year project of an autonomous river cleaning robot. I seem to have a problem with my code.. For now, i want to read my Ultrasonic Sensors (HCSR-04) while getting my DC motors to move and find a route once an obstacle is detected. Since my robot is driving freely in an open water, i have program my motors to run in a specific pattern (although the pattern is not complete yet). But my problem is, once my Ultrasonic detects an obstacle, my motor does not immediately stop and find another route.. Instead, it finishes my pattern first before finding another route.

Please help me with my code and if there's any suggestions.

//Code starts here

void setup() {
//Setup program here
}

void loop() {

frontsensor();

leftsensor();

rightsensor();

if(cm1 > object){

forwardpattern();
}

if(cm1 <= object){

  findroute();
  }
}


void forwardpattern(){

forward();

delay(5000); //Move Forward for 5 second

halt();

turnright();

forward();

delay(5000); //Move Forward for 5 second

turnleft();
}

void forward(){

//motor code here
}

void findroute(){

halt();

if (cm2 < cm3){

Serial.println("Turn Right");

turnright();

}
else{

Serial.println("Turn Left");

turnleft();

}
}

void halt(){

//motor code here
}

void turnright(){

//motor code here
}

void turnleft(){

//motor code here
}

void frontsensor(){

digitalWrite(trigPin1, LOW);
delayMicroseconds(5);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);

pinMode(echoPin1, INPUT);
duration1 = pulseIn(echoPin1, HIGH);

cm1 = ((duration1)/2) / 29.1;
}

void leftsensor(){

digitalWrite(trigPin2, LOW);
delayMicroseconds(5);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);

pinMode(echoPin2, INPUT);
duration2 = pulseIn(echoPin2, HIGH);
cm2 = ((duration2)/2) / 29.1;
}

void rightsensor(){

digitalWrite(trigPin3, LOW);
delayMicroseconds(5);
digitalWrite(trigPin3, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin3, LOW);

pinMode(echoPin3, INPUT);
duration3 = pulseIn(echoPin3, HIGH);

cm3 = ((duration3)/2) / 29.1;
}
\$\endgroup\$
  • \$\begingroup\$ Replace your big delay() with many repetitions of a short delay followed by an ultrasonic reading. If you get a few readings matching a condition change program flow. Possibly restructure the whole thing as a state machine (lookup the term). Also beware your hcsr04 will only by suitable for a simulation where you are driving on dry land... \$\endgroup\$ – Chris Stratton Jan 22 at 13:17
0
\$\begingroup\$

You should control ultrasonic sensor continuous when program is running. Your 5 second delay is block this. You put this function instead of delay(5000)

void delay_5000()
{
 for(int i = 0; i< 500; i++){
  if(cm1 <= object){
   findroute();
  }
  delay(10);
 }
}
\$\endgroup\$

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.