Here is TableCrawler

This code will have your Ringo crawling around your tabletop, sensing the edges, and re-directing his travel. You can use the edge sensor program I previously uploaded to verify the limits in this program will work. The table surface, of course, needs to be smooth and clean or the motor spindles can get stuck. An appropriate board about one foot square is really all you need, but it will need to be raised off the table surface a bit to make the edge sensors work properly.

// Table Crawler is a very simple program that has your Ringo drive slowly across a surface until it detects
// that it has come to an edge. That edge will usually be detected by one antenna or the other. Ringo will then
// back away from that edge and turn, the direction of the turn depending on which antenna saw the edge.

// NOTE: This program works best if Ringo is on a smooth table surface with right angle edges, as opposed to
// rounded or shaped edges.

// The serial monitor cannot be used with this program because the I/O pins that would be used by the serial
// communications are needed to control the motor.

// Here we define the pin numbers for setting the motor directions
#define LEFTPIN 0 // This is the I/O pin that sets the direction of the left motor
#define RIGHTPIN 1 // This is the I/O pin that sets the direction of the right motor

// Next we set the definitions of forward and backward
#define FOREWARD 1 //
#define BACKWARD 0 //

// Here we are going to define the speed that the motors will go
// If Ringo goes too fast he may not stop in time when he senses an edge
// If Ringo goes to slow it is more boring. So you can experiment with speed here.
// Maximum speed is 255. Stopped is 0.
#define SPEED 150

// Now comes the touchy thing. We need to set a threshhold for the IR sensors. Above that number means
// the sensor is seeing reflection from the table top. Below that number means no reflection, and edge.
// You can use “AntennaTailSensors” to discover the proper number for your table surface.
#define THRESHHOLD 200

// Prototypes, these define subroutines the compiler will find later
void DriveStop(int RtDir, int LftDir, int RtSpeed, int LeftSpeed, int Duration);
void Drive(int RtDir, int LftDir, int RtSpeed, int LeftSpeed);

void setup()
{
// We are just going to set the analog pins as analog inputs
pinMode(A1, INPUT); // Right Antenna Sensor
pinMode(A2, INPUT); // Left Antenna Sensor

// The IR sensor LEDs are not on by default, they have to be turned on.
pinMode(8, OUTPUT); // This output can turn on the IR sensor emitters, generating the IR lights
digitalWrite(8,HIGH); // Here we actually turn on the IR emitters

pinMode(0, OUTPUT); // Set the direction pins to be outputs
pinMode(1, OUTPUT); // Set the direction pins to be outputs
}

void loop()
{
int RightSensor, LeftSensor;
Drive(FOREWARD, FOREWARD, SPEED, SPEED); // Start Ringo moving forward

// This next “while” statement will be executed over and over until a sensor reading comes back low.
while((analogRead(A1) > THRESHHOLD) && (analogRead(A2) > THRESHHOLD));
Drive(FOREWARD, FOREWARD, 0, 0); // Panic stop! We are at an edge.

// Lets collect both antenna sensor values
RightSensor = analogRead(A1); // Right Antenna Sensor
LeftSensor = analogRead(A2); // Left Antenna Sensor

// Now let’s back up away from the edge
DriveStop(BACKWARD, BACKWARD, SPEED, SPEED, 200);

if(RightSensor <= THRESHHOLD)
DriveStop(FOREWARD, BACKWARD, SPEED, SPEED, 200);
else
DriveStop(BACKWARD, FOREWARD, SPEED, SPEED, 200);
}

void DriveStop(int RtDir, int LftDir, int RtSpeed, int LeftSpeed, int Duration)
{
// Set the directions for both motors
digitalWrite(LEFTPIN, LftDir);
digitalWrite(RIGHTPIN, RtDir);

// The analog input pins of the Arduino can be used as digital I/O pins. That is what is happening here.
analogWrite(6, LeftSpeed); // Speed control for the left motor
analogWrite(5, RtSpeed); // Speed control for the right motor
delay(Duration);
}

void Drive(int RtDir, int LftDir, int RtSpeed, int LeftSpeed)
{
// Set the directions for both motors
digitalWrite(LEFTPIN,LftDir);
digitalWrite(RIGHTPIN,RtDir);

// The analog input pins of the Arduino can be used as digital I/O pins. That is what is happening here.
analogWrite(6,LeftSpeed); // Speed control for the left motor
analogWrite(5,RtSpeed); // Speed control for the right motor
}

1 Like