Nice cleaning robot with ultrasonic navigation and Bluetooth waits for your help

Hello Oleg, nicely made!

I'm not smart enough to write it from the beggining

If your smart enough to get this far, your smart enough to write any code! You just may not have the knowledge yet :slight_smile:

int trig = 3;
int echo = 4;
void setup() {
Serial.begin(9600);
}

void loop()
{

                           
long duration, cm;  //duration and the distance result in centimeters:

// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
pinMode(trig, OUTPUT);
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(5);
digitalWrite(trig, LOW);

// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echo,INPUT);
duration = pulseIn(echo, HIGH);

// convert the time into a distance
cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print("cm");
Serial.println();

delay(100);
}


long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}

This is the code for a hcsr04 to measure distance from an object.
You can use the same logic and find distances of the front 3 sensors, then add if conditions.
if distance of right sensor is lower than a certain value=> obstacle is close and to the right side=> turn left
You can extend this logic and create cases(if front+right distance is low, if all 3 distances are low, etc)
Test for different limiting distances, see which is optimum....and you have the obstacle avoiding bot ready! :slight_smile: