Desktop Sweeper

Parts:

2 x Small Wheel

2 x Continuous rotation servo

1 x brush

1 x Ultrasonic Sensor

1 x Arduino Nano

1 x LED of any color


The main goal of this desktop sweeper is to sweep an elevated surface by pushing dust and debris to the edge. It works by powering the 2 servo motors until an edge is detected by the ultrasonic sensor. Then, the robot backs up and turns 180 degrees to sweep the other direction. This runs until an edge perpendicular to the previous detected edge is found. The blue LED is used to note when an edge is detected. When testing it, one of the wheels would not touch the surface. As a result, the bot would move on a curved path. Since the ultrasonic sensor was only on one side, the robot would fall off from time to time due to the elliptical travel route.

#include <Servo.h>
Servo lServo;Servo rServo;
void setup() { pinMode(5, OUTPUT); // Trigger ping for the Ultrasonic sensor pinMode(4, INPUT); // Echo pin for the Ultrasonic sensor pinMode(2, OUTPUT); Serial.begin(9600);}
void debug(String msg, int var) { Serial.print("DEBUG: "); Serial.print(msg); Serial.println(var);}
void halt() { lServo.detach(); rServo.detach();}
void forward() { lServo.attach(9); rServo.attach(8); lServo.write(150); rServo.write(0);}
void reverse() { lServo.attach(9); rServo.attach(8); lServo.write(0); rServo.write(180);}
void left(int ldly) { lServo.attach(9); rServo.attach(8); lServo.write(0); rServo.write(0); delay(ldly); halt();}
void right(int rdly) { lServo.attach(9); rServo.attach(8); lServo.write(180); rServo.write(180); delay(rdly); halt();}
bool edgeDetected(int constr, int trigP, int echoP) { long duration; int distance; digitalWrite(trigP, LOW); delayMicroseconds(2); digitalWrite(trigP, HIGH); delayMicroseconds(10); digitalWrite(trigP, LOW); duration = pulseIn(echoP, HIGH); distance = duration * 0.034 / 2; if (distance >= constr) { digitalWrite(2, HIGH); return true; } else { digitalWrite(2, LOW); return false; } debug("CM: ", distance); debug("μs: ", duration);}
void loop() { if (!edgeDetected(5, 5, 4)) { forward(); } if (edgeDetected(5, 5, 4)) { halt(); reverse(); delay(250); right(250); }}