Thanks everyone for your input, especially Pauly because you link to the program really helped. The next bit of advice I would like is on my programming. With a bit of help from my father, I was able to create a program that makes my robot search for a line and follow it. Here’s the program:
#define LED 13 //pin connected to LED
#define LDR 14 //pin connected to LDR
#define BUTTON 9 //pin connected to BUTTON
#define DG_TURN90 99 //no. of milliseconds it takes for the robot to turn 90 degrees
#define DG_TURN180 198 //no. of milliseconds it takes for the robot to turn 180 degrees
#define ON_THE_LINE 20 //voltage LDR recieves when on the line
#define TRUE 1 //define logical TRUE as the value 1
#define FALSE 0 //define logical FALSE as the value 0
int val = 0; //I have setup an integer variale known as val
#include <AFMotor.h> //include motor shield’s functions and definitions (using DC MOTORS)
AF_DCMotor left_motor (1, MOTOR12_8KHZ);
AF_DCMotor right_motor (2, MOTOR12_8KHZ);
void setup () //initial setup function (called once).
{
Serial.begin(9600);
pinMode (LED, OUTPUT); //Arduino board now recognises LED pin as output
pinMode (LDR, INPUT); //Arduino board now recognises LDR pin as input
pinMode (BUTTON, INPUT);
left_motor.setSpeed (200);
right_motor.setSpeed (200);
digitalWrite (LED, HIGH); //turn on sensor LED
}
void loop () //core programming executed repeatedly
{
val = digitalRead (BUTTON); //The current state of the button, on (1) or off (0), is now stored as val.
if (val == HIGH) // If the button has been pressed begin searching for the line, if not stop.
{
if (on_the_line()) //If you are on the line, call forward function. If not, search for the line.
{
forward();
}
else
{
searchforline();
}
}
else
{
return;
}
}
void forward () //makes robot go forward
{
left_motor.run (FORWARD);
right_motor.run (FORWARD);
}
void searchforline () //search for line protocol. If the line isn’t found, the robot stops
{
unsigned long startTime = 0; //start the clock
stop_motor(); //stop motors
left_motor.run (FORWARD); //Start turning left.
startTime = millis (); //remembers the time the robot started turning left
while (!on_the_line() && (millis() - startTime) < DG_TURN90) //while not on the line and turned less than 90 degrees, keep turning left.
{
;
}
stop_motor(); //stop motors
if (on_the_line()) // If on the line, return to loop function
{
return;
}
right_motor.run (FORWARD); //start turning right
startTime = millis (); //remembers the time the robot started turning right
while (!on_the_line() && (millis() - startTime) < DG_TURN180) //while the robot is not on the line and has turned less than 180 degrees, keep turning right.
{
;
}
if (!on_the_line()) //If the line still hasn’t been found, stop.
{
stop_motor();
}
}
void stop_motor () //stop motors
{
left_motor.run (RELEASE);
right_motor.run (RELEASE);
}
int on_the_line () //Determines if robot is on the line. TRUE = on line, FALSE = not on line
{
int val = 0;
val = analogRead (LDR);
if (val >= ON_THE_LINE) //Read LDR and comapre with ON-THE-LINE constant. Is it greater than or equal to the constant ON-THE-LINE?
{
return TRUE;
}
else
{
return FALSE;
}
}
However, I currently a problem with the program. I think I’m missing a few bits of code, but I can’t tell where.