While it is definitely neat to use your phone to control your robot, it is time to set your little robot free. The first step in making your robot autonomous is to add proximity sensors so it can avoid obstacles.
The simplest way to do this is to add a couple robot whiskers to sense when it physically runs into a wall or object. You simply write some code to listen for when the switch has been triggered, stop, turn around, and go forward again. These Micro Switches with a wire are perfect for this.
While this is a great way to navigate around obsticles, I prefer to use a something a bit more intelligent so my little robot doesn't actually have to run into things to navigate. There are plenty of distance sensor options out there (there is a whole category of them on the Adafruit shop), but the sensor I am going to use is the neat little IR sensor from from Sharp.
The obvious benefit here is the size, but also the price. This little sensor will sense an object about 10 centimeters away, and acts like a normal switch. There is a pin on the board that is normally high and switches to low when it senses an object. Because of the small size, we can put two of these on our little robot.
Mounting the Sensors
There are a ton of ways you can mount this sensor to your robot. The easiest way would be to just a bit of double sided foam tape to stick it in place, but I decided to make a super simple 3D printed mount.
I used a couple M2.5 screws to secure the mounts to the robot (required a drill bit to clean up the holes in the 3D print). I wired up the left sensor to pin A4, and the right sensor to pin A5.
The Code
The code to make your robot take advantage of its new eyes is very straight forward. For now, we are going to focus on the Adafruit MotorShield library.
#include <Wire.h> #include <Adafruit_MotorShield.h> #include "utility/Adafruit_MS_PWMServoDriver.h" // Create the motor shield object with the default I2C address Adafruit_MotorShield AFMS = Adafruit_MotorShield(); // And connect 2 DC motors to port M3 & M4 ! Adafruit_DCMotor *L_MOTOR = AFMS.getMotor(3); Adafruit_DCMotor *R_MOTOR = AFMS.getMotor(4); // And connect the Sharp distance sensors int leftSensor = A4; int rightSensor = A5; void setup() { Serial.begin(9600); // set up Serial library at 9600 bps Serial.println("Adafruit Motorshield v2 - DC Motor test!"); pinMode(leftSensor, INPUT); // set up distance sensor pins pinMode(rightSensor, INPUT); AFMS.begin(); // create with the default frequency 1.6KHz } void loop() { L_MOTOR->setSpeed(200); R_MOTOR->setSpeed(200); L_MOTOR->run(FORWARD); R_MOTOR->run(FORWARD); while (digitalRead(rightSensor) == LOW){ L_MOTOR->setSpeed(100); R_MOTOR->setSpeed(100); L_MOTOR->run(BACKWARD); R_MOTOR->run(RELEASE); } while (digitalRead(leftSensor) == LOW){ L_MOTOR->setSpeed(100); R_MOTOR->setSpeed(100); L_MOTOR->run(RELEASE); R_MOTOR->run(BACKWARD); } }
As you can see from the above code, there isn't a whole lot going on here. All we are doing is reading one of the distance sensors, and if it senses an object we reverse the opposite side motor until the object is no longer detected. We also slow things down quite a bit, as this little robot is so quick it loves to pop wheelies when it starts and stops quickly.
This is just the beginning of what you can do with distance sensing. The next steps are up to you. You can integrate this code into the bluetooth controller code to turn on and off auto mode with a button press. What other ideas can you think of?
Text editor powered by tinymce.