Build Programming for Line Follower
The Arduino
is based around the C/C++ language in the computer world. Most of the coding is
predefined and does not need to be made from scratch (which will make this
build a lot easier). Here is a list of commands that we will be using:
-
#define
-
int
-
void
setup()
-
void
loop()
-
pinMode()
-
analogRead()
-
analogWrite()
-
delay()
-
if()
-
else
-
//, /*
…*/
Each of the
commands has their own functions and can be used in a number of combinations.
#define:
The
“#define” command allows you to set a certain digital pin to a specific name
(the analog pins do not need to be), such as “#define LED 13.” This will set
the LED to be used on digital pin 13 of the Arduino.
Int:
“int” is
used to hold a variable (whole numbers for this particular command) and will be
used to hold our sensor readings and move the robot around. Such as:
-
int i =
0”
voids:
“void setup”
and “void loop” are the main sections of the code that are required to run the
Arduino program. The “setup” predefines what each pin is going to be used as
(for digital pins only). Such as “pinMode (LED, OUTPUT)” will mean that it will
apply 5V to digital pin 13 defined earlier. It can also be set as “INPUT” to
read a sensor that returns a “0” or “1” value. “void loop” is the main function
of the code that will run what you write. In this case, we want it to read the
sensors and then turn on the motors and either speed up or slow down the motors
accordingly.
Analogs:
“analogRead”
is used for reading the inputs from the analog pins on the Arduino (we will be
using 2 of these pins for the robot). The value that will be returned will
range from 0-1023, since the Arduino use a 10 bit analog to digital
converter. “analogWrite” is used to
write a Pulse Width Modulation signal (PWM) to the specified digital pin. A PWM
signal ranges from 0 to 255 and can be used to vary the speed of the motor to
that pin. To write one of these analog commands you do the following:
-
analogRead(0)
The 0 means that it will read
from pin 0 of the analog inputs.
-
analogWrite(LED,255)
This will write a PWM signal of
255 (which is the highest value) to pin 3 of the digital pins. This also means
that the power output from digital pin 3 will be a full 5V.
NOTE: There are only
certain pins that are PWM and are labeled on the Arduino board face.
Pulse Width Modulation (PWM):
How a PWM
signal works is a square wave is generated with one high point and one low
point (0v=low and 5v=high). This acts as a throttle for the motor controllers
to change the speed; for the Arduino, the values that are used for the PWM
signal is from 0-255. Here is a problem, we have a sensor that can read a value
up to 1023, but we can only write a signal at a max of 255. To compensate for
the huge range, we divide the sensor signal by 4 to closely get the 255 PWM
value we need.
Delay:
The “delay(
)” command pauses the code for a certain amount of time before continuing. This
is based in milliseconds, so to stop the code for 1 second, we write:
-
delay(1000);
ifs and elses:
“if” and
“else” statements are comparison commands. The “if” statement checks to see if
a certain range or behavior has been met. Such as:
-
int i=0
-
if
(i==0)
-
{
-
Then do
this
-
}
There is a
BIG difference between “=” and “==”. The single “=” means that the particular
“int” will equal that number. The double “==” means that the “int” must equal
to the number specified in the “if (…)” statement.
The “else”
statement means that if the “if” statement is not satisfied, then you do this
other command. Such as:
-
int i=1
-
if
(i==0)
-
{
-
Then do
this
-
}
-
else
-
{
-
Do this
instead
-
}
The “{ }”
are needed when you want to only do this set of commands at one time (this
particularly deals with multiple actions or a “for” loop (this will not used in
the code covered)).
Commenting (or leaving out):
To have
certain parts of a code not work (but you don’t want to delete it yet), you use
two different forms of commenting. One is “//”; this will “comment out” a part
of one line so that the Arduino knows to skip that part of a code.
NOTE: This comment
form can be used in two ways; 1) you can comment a whole entire line out so it
will not be used or 2) you can comment a section of the line out but still use
the other that was not commented out.
Ex:
1)
//pinMode(LED,OUTPUT);
a.
This
will comment the whole line out and will not be used
2) pinMode(LED,OUTPUT); // This will set LED pin
as output
a.
This
form will compute the pinMode action but will skip over the comment section to
the right of the backslashes
“ // ” form only comments out
anything to the right of the backslashes.
The second
commenting form is “/*…*/ where “…” is the code sectioned out. This form only
comments out the code in between the “/*” and the “*/”. Such as:
-
pinMode(/*LED,OUTPUT*/);
This will
comment out the section so that the Arduino skips over the code within the
parentheses and moves on to the rest of the code. This method can be used to
comment out entire sections of code.
Layout order of code:
Now here is how to organize the
code to work properly:
-
#define
___
-
int ___;
-
void
setup( )
-
{
-
pinMode(
, );
-
}
-
void
loop( )
-
{
-
Commands;
-
}
The semicolons are needed for
anything that does not do some type of specific or main function. Such as
“int,” “pinMode,” etc. This will be explained in the workshop or can be
understood more at “arduino.cc.”
Uploading the code to the Microcontroller:
After making your code, go to “Tools >
Boards” and select which microcontroller you have. Then under “Tools >
Serial” choose which port your microcontroller is set to (you may have to keep
selecting and uploading until the correct one is selected if there is more than
one selected). Along the toolbar, the button with the arrow pointing right
towards 2 sets of vertical dotted lines is the “Upload” button; this will
transfer the code you made to the microcontrollers memory. If all goes well, it
will indicate that the code has been uploaded successfully; otherwise it will
give an error and a small description on what the error is.
EXTRA: Serial Connection (reading sensor’s feedback)
The following line of code can be
added to look at the numbers being returned from the sensors. The communication
used is called Serial. There are 2 parts that have to be used in order to see
the numbers being fed from the sensors to the Arduino.
First is in the void setup(). Somewhere
in the {…}, write out “Serial.begin(9600);”. This will let the Arduino know
that data will be sent back to the computer (in this case, numbers will be sent
from the sensors, travel through the serial of the Arduino and be displayed on
the computer screen).
Second is in the void loop(). In the {…}, near the end before the delay, there
are 2 command lines that have to be used to properly see the numbers. One is
“Serial.print(…)” and the other is “Serial.println (…)”. The difference between
the two codes is that “println” will start a new line after it prints the line
of code in between “(…)”. “print” will just keep printing until the code
specifies to start a new line.
To set up the serial monitor to view
the numbers, first we need to code lines that will allow to print on the
computer screen. To start off, we will use a variable from the code below – for
now, we will use the L_sens variable. Type out “Serial.print(“LM”);” – this
will print out the words “LM” because the quotations mean it will print out
text (space also counts as text). Next, type “Serial.print(L_sens);” – without
the quotations, the code will now search for that exact variable from the rest
of the code to see if there is a number associated with it. If so, the number
will be printed out. Now to create a tab as if you pressed tab in Microsoft
Word, type “Serial.print(“\t”);” – this will create a space similar to a tab
space. Continue to add printing codes until you are ready to print on another
line (Serial.pring(“”);).
To see any of the text or values
after you upload the code, you need to open the Serial Monitor built into the
Arduino program. In the main Arduino window, along the top there is a tool bar,
select the icon that looks like a box hanging on a string and pulley. This is
the Serial Monitor that will allow you to see the numbers the sensors are
returning.
Here is the extra code that is not
necessary, but can be helpful to determine a better value for the code to work
off of:
void setup()
{…
Serial.begin (9600);
}
void loop()
{…
Serial.print("LM");
Serial.print(L_sens);
Serial.print("\t");
Serial.print("RM");
Serial.print(R_sens);
Serial.print("\t");
Serial.println("");
delay(100);
}
The delay(100)
is just a slower speed to see the values better on the monitor when they print
out, or else the printing is going so fast that the numbers are not readable.
The code:
Here is the code for the
Hammerhead style to be used in the robot:
#define Left_mot 3 // Left
motor PWM
#define Right_mot 6 // Right
motor PWM
int L_sens; // Left Sensor
int R_sens; // Right Sensor
int state, prev_state; //which
state the robot is in (turning left, turning right, straight)
void setup(){ // Will apply 5V
to each of these pins when needed
pinMode (Left_mot, OUTPUT);
pinMode (Right_mot, OUTPUT);
}
void loop() // start of main
function and will keep repeating
{
state = 1; //state will equal 1 and bot will
go straight
//-----------------------------------------------------------------------------------
while (state ==1) //state will make robot go
straight or continue with the last state it was in
{
if (state == 1)
{
analogWrite(Left_mot,70);
analogWrite(Right_mot,70);
L_sens = analogRead(0)/4; // Reads the left
sensor
R_sens = analogRead(2)/4; // Reads the right
sensor
if (R_sens < 120) // Compares the right
sensor to a certain range
{
state = 2;
}
else if (L_sens < 120) // Compares the
left sensor to a certain range
{
state = 3;
}
}
else if (prev_state == 2) //looks at previous
state before change of surface
{
analogWrite(Right_mot,15); // Writes a PWM
signal to the Right motor pin
analogWrite(Left_mot,100); // Writes a PWM
signal to the Left motor pin
if (R_sens < 120) // Compares the right
sensor to a certain range
{
state = 2;
}
}
else if (prev_state == 3) //looks at previous
state before change of surface
{
analogWrite(Right_mot,100); // Writes a PWM
signal to the Right motor pin
analogWrite(Left_mot,15); // Writes a PWM
signal to the Left motor pin
if (L_sens < 120) // Compares the left
sensor to a certain range
{
state = 3;
}
}
delay(50); // pauses the code for a
certain time
}
//-----------------------------------------------------------------------------------
while (state == 2) // state where robot is
turning right
{
L_sens = analogRead(0)/4; // Reads the left
sensor
R_sens = analogRead(2)/4; // Reads the
right sensor
analogWrite(Right_mot,15); // Writes a PWM
signal to the Left motor pin
analogWrite(Left_mot,100); // Writes a PWM
signal to the Right motor pin
if (L_sens < 120) // Compares the right
sensor to a certain range
{
prev_state = state;
state = 3;
}
else if (R_sens < 120 && L_sens
< 120)
{
prev_state = state;
state = 1;
}
delay(50); // pauses the code for a
certain time
}
//---------------------------------------------------
while (state == 3) // state where robot is
turning left
{
L_sens = analogRead(0)/4; // Reads the left
sensor
R_sens = analogRead(2)/4; // Reads the
right sensor
analogWrite(Right_mot,100); // Writes a PWM
signal to the Right motor pin
analogWrite(Left_mot,15); // Writes a PWM
signal to the Left motor pin
if (R_sens < 120) // Compares the right
sensor to a certain range
{
state = 2;
}
else if (R_sens < 120 && L_sens
< 120)
{
prev_state = state;
state = 1;
}
delay(50); // pauses the code for a
certain time
}
}
Here is the code for the
Inverse style to be used in the robot:
#define Left_mot 3 // Left
motor PWM
#define Right_mot 6 // Right
motor PWM
int L_sens; // Left Sensor
int R_sens; // Right Sensor
int state, prev_state; //which
state the robot is in (turning left, turning right, straight)
void setup(){ // Will apply 5V
to each of these pins when needed
pinMode (Left_mot, OUTPUT);
pinMode (Right_mot, OUTPUT);
}
void loop() // start of main
function and will keep repeating
{
state = 1; //state will equal 1 and bot will
go straight
prev_state = 0;
//-----------------------------------------------------------------------------------
while (state ==1) //state will make robot go
straight or continue with the last state it was in
{
if (state == 1 && prev_state == 0)
{
analogWrite(Left_mot,70);
analogWrite(Right_mot,70);
L_sens = analogRead(0)/4; // Reads the left
sensor
R_sens = analogRead(2)/4; // Reads the right
sensor
if (R_sens > 120) // Compares the right
sensor to a certain range
{
state = 3; // goes to while loop state ==
3 below
}
else if (L_sens > 120) // Compares the
left sensor to a certain range
{
state = 2; // goes to while loop state == 2 below
}
}
else if (prev_state == 2) //looks at previous
state before change of surface
{
analogWrite(Right_mot,15); // Writes a PWM
signal to the Right motor pin
analogWrite(Left_mot,100); // Writes a PWM
signal to the Left motor pin
if (L_sens < 120) // Compares the left
sensor to a certain range
{
prev_state = 0; // goes to while loop
state ==1 && prev_state == 0 above
}
}
else if (prev_state == 3) //looks at previous
state before change of surface
{
analogWrite(Right_mot,100); // Writes a PWM
signal to the Right motor pin
analogWrite(Left_mot,15); // Writes a PWM
signal to the Left motor pin
if (R_sens < 120) // Compares the right
sensor to a certain range
{
prev_state = 0; // goes to while loop
state ==1 && prev_state == 0 above
}
}
delay(50); // pauses the code for a
certain time
}
//-----------------------------------------------------------------------------------
while (state == 2) // state where robot is
turning right
{
L_sens = analogRead(0)/4; // Reads the left
sensor
R_sens = analogRead(2)/4; // Reads the
right sensor
analogWrite(Right_mot,15); // Writes a PWM
signal to the Left motor pin
analogWrite(Left_mot,100); // Writes a PWM
signal to the Right motor pin
if (R_sens > 120) // Compares the right
sensor to a certain range
{
prev_state = state;
state = 3;
}
else if (R_sens > 120 && L_sens
> 120 || R_sens < 120 && L_sens < 120)
{
prev_state = state;
state = 1;
}
delay(50); // pauses the code for a
certain time
}
//---------------------------------------------------
while (state == 3) // state where robot is
turning left
{
L_sens = analogRead(0)/4; // Reads the left
sensor
R_sens = analogRead(2)/4; // Reads the
right sensor
analogWrite(Right_mot,100); // Writes a PWM
signal to the Right motor pin
analogWrite(Left_mot,15); // Writes a PWM
signal to the Left motor pin
if (R_sens < 120 && L_sens <
120 || R_sens > 120 && L_sens > 120)
{
prev_state = state;
state = 1;
}
delay(50); // pauses the code for a
certain time
}
}
(Sumber:https://www.google.co.id/url?sa=t&rct=j&q=&esrc=s&source=web&cd=13&cad=rja&uact=8&ved=0CFcQFjAM&url=https%3A%2F%2Fwww.cpp.edu%2F~cpprc%2Fdownloads%2FBuilding%2520a%2520Line%2520Follower%2520Robot.doc&ei=uSU2Vc2UCOXBmwXL34CgDA&usg=AFQjCNGJRbmWxn6eTkA7fMg3BSq1XIUxXg&bvm=bv.91071109,d.dGY)
Tidak ada komentar:
Posting Komentar