0% found this document useful (0 votes)
335 views26 pages

Lesson 3 Conditions and Line Following

The document provides instructions for programming line following and color detection behaviors using EV3 or Spike Prime robots. It discusses using conditional statements like "if/else" with sensor inputs to control robot actions. It presents examples starting with basic 2-state line following, then improving it to 3-state and 5-state versions for smoother movement. More advanced proportional control is also mentioned. Programming blocks are shown for EV3 software, while noting Spike Prime uses similar blocks.

Uploaded by

mhacuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
335 views26 pages

Lesson 3 Conditions and Line Following

The document provides instructions for programming line following and color detection behaviors using EV3 or Spike Prime robots. It discusses using conditional statements like "if/else" with sensor inputs to control robot actions. It presents examples starting with basic 2-state line following, then improving it to 3-state and 5-state versions for smoother movement. More advanced proportional control is also mentioned. Programming blocks are shown for EV3 software, while noting Spike Prime uses similar blocks.

Uploaded by

mhacuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Conditions and Line

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.

EV3 Classroom Spike Prime


Starting a New Project

Select “My Projects” to see


your past projects

Start a “New Project”


“if” and “else”
This space is for the condition

“if” the “condition” is true,


then do this...

“else” do this...

Find it under “Control”


Color Sensor
Set the port number correctly Choose which color you want
(...check where you plugged to test for
in your color sensor)

Notice the hexagonal shape


of this block?

This is a condition block. It is


either True or False.

It fits inside the condition space


of an “if / else” block.
Find it under “Sensors”
Example 1
When the program starts...

Check if the color sensor


is seeing red...

If it is, play “hello”...

“else”, play beep

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...

Check if the color sensor is seeing red...

If it is, play “hello”...

Check if the color sensor is seeing blue...

If it is, play “Woof!”...

“else”, play beep

You can combine “if / else” to


check for multiple conditions

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

Check if the color sensor is seeing red...

If it is, play “meow”...


Repeat
Forever Check if the color sensor is seeing blue...

If it is, play “Woof!”...

Check if the color sensor is seeing black...


If it is, stop moving

“else”, move forward

Notice we didn’t tell the robot how far


to move. We just tell it to move straight.
Try it out!
Line Following

Commonly used to guide robots

Uses color sensor and “if / else”

Line following book drop robot in Tampines Library


Line Following

Trick: The robot doesn’t follow the center of
the line, it follows the edge of the line

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...

...so it should turn left. ...so it should turn right


2 States Line Follower

Optional: Set the speed to 30%

Check if the color sensor is seeing black...

If it is, move right

“else”, move left

This is called a “2-states line follower” program,


because the robot only have 2 states;
“move left” and “move right”

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!

Comparison symbol: Limits


Set the port
< : Less than Black : Around 0%
= : Equal White : Around 100%
> : Greater than

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%

You’ll need to test it out to find the actual values


3 States Line Follower
Robot sees Light Sensor Value
white (> 70%):
Turn Left 100 (White)

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.

Pseudo Code Programmers use it to


Turn Left explain how to program
IF Value > 70:
Turn Left without providing the exact
70 ELSE IF Value > 30: details.
Go Straight
Go Straight ELSE As we progress, we’ll start
30 Turn Right providing more examples in
pseudo code without the
blocks code.
Turn Right

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.

You might also like