INTERMEDIATE
PROGRAMMING
LESSON
CO LO R LINE FO LLOW ER MY B LO C K W ITH INPU TS:
MOV E FO R DISTANC E
By Sanjay and Arvind Seshan
Lesson Objectives
1. Learn how to write a line follower that takes multiple inputs
2. Learn how to write a line follower that stops after a certain number of degrees
3. Practice making a useful My Block
Prerequisites: My Blocks with Inputs & Outputs, Variables, Loops, Switches.
The code uses Blue Comment Blocks. Make sure you are running the most recent
version of the EV3 Software. EV3Lessons has Quick Guides to help you.
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
My Block Line Follower with Inputs
• Making a My Block out of your line follower reduces the length of your
code and makes it reusable
• Learning to write a line follower that takes multiple inputs (power,
degrees and color) can be very useful
• Every time you want a line follower that goes a different distance, you just
need to change the input!
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
Tips to Succeed
You will need to know how to make a Simple Color Line Follower program and how
to make a My Block with inputs
Since you will use your EV3 Color Sensor in Color Mode, you will not have to
Calibrate your color sensor for this lesson
Check which ports you have your color sensor connected to and adjust the code as
needed
You may have to adjust the speed or direction to work for your robot. Make sure
that the the color sensor is in front of the wheels in the direction of travel.
Make sure you place the robot on the side of the line that you are following. The
most common mistake is placing the robot on the wrong side of the line to begin
with.
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
New Block
In this lesson, you will need to reset your
motor’s rotation sensor
Since you want to line follow only for a
certain distance, you have to first reset
the value to 0.
Motors B or C are your drive motors so
pick either of them.
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
Color Follower for Distance
STEP 1: Create a My Block with three inputs Challenge: Write a line follower My Block
that follows a black line and stops after
STEP 2: Define the My Block to line follow moving a certain number of degrees. The
with the exit condition of the loop as line follower should take three inputs
degrees (degrees, speed and color to follow).
STEP 3: Use the My Block to follow a Black
Line for 500 degrees
Goal: Stop after
500 degrees
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
Step 1: Create the My Block
Add three Inputs and Labels
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
Step 2: Define the My Block
Drag variables as indicated
Reset the rotation sensor
Repeat the loop until the rotation sensor
reads a certain number of degrees
If the color sensor reads the color of the
line you want to follow, turn right, else,
turn left
Stop motors
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
Step 3: Use and Reuse the My Block
Line follow a Black Line for 500 degrees
Note: You have to Color Code:
enter a number 0 - No Color
into the parameter 1 - Black
for the color. You 2 - Blue
cannot just spell 3 - Green
out the color. The 4 - Yellow
numbers are not 5 - Red
very clearly 6 - White
defined in EV3 7 - Brown Line follow a Black Line for 500 degrees
Classroom. and then a Red Line for 700 Degrees
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019
Credits
This tutorial was created by Sanjay Seshan and Arvind Seshan
More lessons are available at www.ev3lessons.com
This work is licensed under a Creative Commons Attribution-
NonCommercial-ShareAlike 4.0 International License.
© 2020 EV3LESSONS.COM, LAST EDIT 12/24/2019