Lesson 3 Conditions and Line Following
Lesson 3 Conditions and Line Following
Following
●
Using “if” and “else”
●
Color Sensor
●
Color Decoder
●
Following lines
EV3 vs Spike Prime
●
We’ll be using the EV3 Classroom software for demonstration
●
Programming blocks in Spike Prime are not exactly the same, but
are very similar
●
I’m sure you’ll figure it out! Let us know if you need help.
“else” do this...
Try it out!
The Spike Prime will have a ●
Add a color sensor to your robot
different set of sound. Use ●
Run the program on your robot
whatever sound you like! ●
Try different colors and sounds
●
What did you observe?
Example 2
When the program starts...
Try it out!
Color Decoder
Robot
“Meow!” “Woof!” Stops
Note:
●
The colors and the distance between each color may be randomized.
●
You can choose whatever sound you like
What should the robot do?
●
If it sees red, play “meow”
●
If it sees blue, play “woof”
●
If it sees black, stop
●
Otherwise, it should move forward
●
Important: It should keep repeating these!
How to program?
●
Let’s think about it...
– Many conditions (eg. red, blue, black)
●
Need to use multiple “if / else”
– Need to keep checking the color
●
Need to use a loop
Color Decoder
Don’t forget to set the movement motors
Correct Wrong
Robot follows Robot should
edge of line not follow
center of line
Line Following
If robot sees white, it is too far If robot sees black, it is too far
to the right... to the left...
Try it out!
Problems
●
Problem:
– Movement is slow and jerky
●
Why?:
– Robot ONLY move left and right. It never goes
straight.
●
Can we have 3 states?
– Black, White, and Gray? (...there’s no “Gray”)
Solution
Check the intensity of the reflected light!
Important
Black should be close to 0%, but won’t be exactly 0%
White should be close to 100%, but won’t be exactly 100%
Pseudo Code
Turn Left
IF Value > 70:
Turn Left
Robot sees half white, 70 ELSE IF Value > 30:
half black (30% to 70%): Go Straight
Go Straight Go Straight ELSE
30 Turn Right
Turn Right
Robot sees
black (< 30%):
Turn Right 0 (Black)
3 States Line Follower
Light Sensor Value
100 (White)
Pseudo Code
Turn Left
IF Value > 70:
Turn Left
70 ELSE IF Value > 30:
Go Straight
Go Straight ELSE
30 Turn Right
Turn Right
Try it out!
0 (Black)
Pseudo Code
Light Sensor Value Pseudo Code
Looks like real programming
code, but it won’t actually
100 (White) run.
0 (Black)
Can we do better?
●
2-states
– Slow and Jerky
●
3-states
– Smoother, but still a little jerky
●
Can we have 5 states?
5 States Line Follower
Try it out! Light Sensor Value
●
Read the pseudo code
●
Try and write the actual 100 (White)
program using that Sharp Left
80 Pseudo Code
IF Value > 80:
Slight Left
Turn Sharp Left
60 ELSE IF Value > 60:
Turn Slight Left
Go Straight ELSE IF Value > 40:
40 Go Straight
ELSE IF Value > 20:
Slight Right Turn Slight Right
20 ELSE
Turn Right
Sharp Right
0 (Black)
Slight Left Sharp Left
Can we do even better?
●
You can make...
– 7 states, 9 states, etc
– ...but it’s probably not necessary
●
More advanced method:
– Proportional control
– Uses math equation instead of “if / else”
– Better than 5 states in some cases, but not all
– We’ll leave that for a future lesson...
Summary
●
Use “if / else” to choose what to do
●
“is color” block to check color
●
“reflected light intensity” block to
check brightness
Summary
●
Line Following:
Robot follows edge of line
●
Line Following:
Can use more states to make
movement smoother
Copyright
●
Created by A Posteriori LLP
●
Visit http://aposteriori.com.sg/ for more tips and
tutorials
●
This work is licensed under a Creative Commons
Attribution-ShareAlike 4.0 International License.