With the below code, the robot will look for an obstacle closer than 30 cm. If one is found, the robot will turn 90 degrees and check again. If again

Autonomous robot using Sphero RVR and Arduino Uno

submited by
Super Jumper
2020-06-21 08:17:51

With the below code, the robot will look for an obstacle closer than 30 cm. If one is found, the robot will turn 90 degrees and check again. If again there is an obstacle, the robot will move to 270 degrees to try and find a clear route. If this way is also blocked, the robot will move to 180 to try and find a solution.

#include
const int pingPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6;
long duration, inches, cm;
void setup() {
rvr.configUART(&Serial);
}
void searching() {
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = microsecondsToCentimeters(duration);
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
void loop() {
// reset the heading to zero
rvr.resetYaw();
searching();
if (cm < 30)
{
rvr.driveWithHeading(0, 90, static_cast(DriveFlags::none));
delay(2000);
searching();
if (cm < 30)
{
rvr.driveWithHeading(0, 270, static_cast(DriveFlags::none));
delay(2000);
}
else
{
rvr.driveWithHeading(0, 180, static_cast(DriveFlags::none));
delay(2000);
}
}
else {
rvr.driveWithHeading(34, 0, static_cast(DriveFlags::none));
delay(2000);
}
}
long microsecondsToCentimeters(long microseconds) {
return microseconds / 29 / 2;
}

In the above video compilation, my RVR is also using an 8by8 red LED matrix I bought from AZ delivery. I found an awesome and very helpful example code for this matrix that does NOT use a library, as unfortunately the LED-control library was not compatible with the Sphero RVR library. Or at least I couldn’t make it work anyway, although I am far from an expert.

Leave a Comment