EdPy Student
EdPy Student
The EdPy Lesson Plans Set by Brenton O’Brien, Kat Kennewell and Dr Sarah
Boyd is licensed under a Creative Commons Attribution-ShareAlike 4.0
International License.
Name_____________________________
Contents
Lesson 1: Worksheet 1.1 – Meet Edison ....................................................... 4
Lesson 1: Worksheet 1.2 – Barcode programming ........................................ 5
Lesson 1: Worksheet 1.3 – Meet the EdPy app............................................. 6
Lesson 1: Worksheet 1.4 – Downloading a test program ............................. 8
Lesson 2: Worksheet 2.1 – Drive the robot forward ..................................... 10
Lesson 2: Worksheet 2.2 – Drive the robot backwards ................................ 13
Lesson 2: Worksheet 2.3 – Forwards, then backwards ............................... 15
Lesson 2: Worksheet 2.4 – Expressions in Python ...................................... 17
Lesson 2: Worksheet 2.5 – Keypad activated driving .................................. 19
Lesson 2: Activity sheet 2.1......................................................................... 22
Lesson 3: Worksheet 3.1 – Turn right ......................................................... 23
Lesson 3: Worksheet 3.2 – Turn left 180° ................................................... 25
Lesson 3: Worksheet 3.3 – Right turn, then left turn .................................... 26
Lesson 3: Worksheet 3.4 – Mini maze ........................................................ 27
Lesson 3: Activity sheet 3.1 – Turning ......................................................... 29
Lesson 3: Activity sheet 3.2 – Mini maze..................................................... 30
Lesson 4: Worksheet 4.1 – Drive in a square .............................................. 31
Lesson 4: Worksheet 4.2 – Use a loop to drive in a square ......................... 32
Lesson 4: Worksheet 4.3 – Drive in a triangle and a hexagon ..................... 34
Lesson 4: Worksheet 4.4 – Challenge! Drive in a circle ............................... 35
Lesson 4: Activity sheet 4.1......................................................................... 36
Lesson 4: Activity sheet 4.2......................................................................... 36
Lesson 4: Activity sheet 4.3......................................................................... 38
Lesson 4: Activity sheet 4.4......................................................................... 39
Lesson 5: Worksheet 5.1 – Play tones ........................................................ 40
Lesson 5: Worksheet 5.2 – Make an alarm ................................................. 43
Lesson 5: Worksheet 5.3 – Play a tune ....................................................... 47
Lesson 5: Worksheet 5.4 – Make your robot dance .................................... 49
Lesson 5: Worksheet 5.5 – Challenge! Dance to music .............................. 51
Lesson 6: Worksheet 6.1 – Flash the LED in response to a clap ................. 53
Lesson 6: Worksheet 6.2 – Drive in response to a clap ............................... 57
Lesson 6: Worksheet 6.3 – Design your own function ................................. 59
Lesson 7: Activity sheet 7.1 – Calibrate obstacle detection ......................... 63
www.edpyapp.com www.meetedison.com
Name_____________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Look at the images below to become familiar with Edison’s sensors, buttons and switches.
Buzzer/clap detector
Record button
Stop button
Play button
Left light sensor
Left infrared LED
Left red LED
Right red LED
Right infrared LED
Right light sensor
www.edpyapp.com www.meetedison.com
Name_____________________________
___________________________
___________________________
___________________________
___________________________
Barcode - Clap controlled driving
___________________________
___________________________
___________________________
___________________________
Barcode – Avoid obstacles
___________________________
___________________________
___________________________
___________________________
___________________________
n
___________________________
___________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
To get familiar with the EdPy app and programming, try opening up some example
programs. Investigate how some of the functions work by searching in the ‘Documentation’
window. Everything you need to know about the EdPy app commands can be found in the
documentation section. Also, try using the ‘Line Help’ to discover more about each
Programming area
Documentation
Example programs
Compiler Output
Line Help
function.
Your turn:
1. What should you change the following line to if you are using an Edison Version 1?
2. How many input parameters does each of the following commands have?
Ed.PlayBeep() ______________
Ed.TimeWait() ______________
Ed.LeftLed() ______________
www.edpyapp.com www.meetedison.com
Name_____________________________
Ed.DriveRightMotor() ______________
3. You can change the Setup code to use inches instead of centimetres as your unit of
measurement throughout the program. If you did this, how should that line of the
Setup code be written?
________________________________________________________________________
________________________________________________________________________
4. If there are errors in your code after you have clicked the ‘Check Code’ button, in
which window will the errors appear?
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Edison looks at each line of the program one at a time, then does what the line says.
Look at line 2, which starts with a ‘#’ (hash) character. When a line starts with this
character, it is called a ‘comment line.’ Edison will ignore any characters that come after
the ‘#’ on a line and move onto the next line. In programming, we use comment lines to
document our code to help keep track of things and so that other people can understand
the program.
www.edpyapp.com www.meetedison.com
Name_____________________________
1. Turn Edison on, then press Edison’s record (round) button once
2. Connect Edison to the computer using the EdComm cable and confirm the
volume is turned up to full
3. Press the ‘Program Edison’ button in the upper right corner of the EdPy app
4. Follow the steps in the pop-up window, then press ‘Program Edison’
Once the program finishes downloading, unplug the EdComm cable. Press the play
(triangle) button once to run the program.
Your turn:
1. What did the robot do when you pressed the play button?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
2. Look at the Python commands in the program and think about what Edison did
when you played the program. Describe how they relate to each other.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. Explain how the program gets from the computer to the robot.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Step 1: Start writing the above program into the EdPy app by typing ‘Ed’ into line 13.
As you begin typing ‘Ed’ on line 13, you will see a prompt box pop up. The prompt box
shows a list of possible commands for you to select. This is a feature of the EdPy app
called ‘command line completion’, and it makes it quicker for you to program.
Step 2: Type ‘Ed.Drive(’ into line 13, and select the ‘Ed.Drive()’ function.
Ed.Drive() is a function in Python that’s being imported from the Edison ‘Ed’ library module
by the Setup code.
A function is a piece of code that performs a particular role or job, depending on which
parameters are input. All the functions that are imported from the ‘Ed’ library must start
with ‘Ed.’ This tells the program which library to go to in order to find that function.
When a function has input parameters, you need to enter a value for each one.
Different input parameters take different values. For example, ‘speed’ takes Ed.SPEED_ a
number from 1 to 10 (10 is the maximum).
www.edpyapp.com www.meetedison.com
Name_____________________________
The type of distance units is controlled by the constant that ‘Ed.DistanceUnits’ is set to in
the Setup code. There are three distance units that you can use:
• Centimetres, written as Ed.CM
• Inches, written as Ed.INCH
• Time, written as Ed.TIME
Note: If you have an Edison V1 robot, you must use time, so make sure that
Ed.DistanceUnits = Ed.TIME. The time distance unit is milliseconds, which means
that to drive for 2 seconds, you need to set the distance input parameter to 2000
(since 2000 milliseconds = 2 seconds).
Let’s take a closer look at the drive function and parameters in this program.
drive command
direction parameter
speed parameter
13 Ed.Drive(Ed.FORWARD,Ed.SPEED_5,8)
distance parameter
Once you have written the full program, click the ‘Check Code’ button and look at the
‘Compiler Output’ window to make sure you haven’t made any errors while typing.
Typo-style errors are called ‘syntax errors.’ If you type a word that isn’t a part of the EdPy
program’s language, also called syntax, then the EdPy compiler can’t understand what it is
meant to do. This creates a syntax error.
If there are any errors in your code, fix them, so your code matches the example.
Once you check that your code has no errors, download your program to your Edison.
1. Turn Edison on, then press Edison’s record (round) button once
2. Connect Edison to the computer using the EdComm cable and confirm the
volume is turned up to full
3. Press the ‘Program Edison’ button in the upper right corner of the EdPy app
4. Follow the steps in the pop-up window, then press ‘Program Edison’
Once the program downloads, unplug the EdComm cable. Use activity sheet 2.1 or
coloured tape to mark ‘start’ and ‘finish’ lines on a desk or the floor as a test area for your
www.edpyapp.com www.meetedison.com
Name_____________________________
program. Press the play (triangle) button once to run the program and watch what
happens.
Measure the distance between your start line and finish line. Try to modify your program to
make your Edison robot finish driving just before the finish line. Experiment to see what
works.
Your turn:
1. What constant did you have ‘Ed.DistanceUnits’ set to in the Setup code?
__________
2. What number did you need to enter as the distance input parameter to make
Edison drive from the start to the finish line?
__________
3. Experiment with driving the Edison robot at different speeds. What does the
robot do when you drive Edison at speed 10? Do you notice any changes in the
accuracy of Edison when it is driving at speed 10?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Whenever you write a program for Edison in EdPy, you always follow the same basic
steps:
• Step 1: Check the Setup code is using the constants you want.
• Step 2: Write the program, selecting the functions you want to use, and filling in the
input parameters with the values you want.
• Step 3: Check your program for errors using the ‘Check Code’ button.
• Step 4: Download and test your program using your Edison robot.
Remember, if you have Edison V1 robot, make sure that Ed.DistanceUnits = Ed.TIME. The
distance parameter for Ed.TIME is in milliseconds.
Your turn:
Use activity sheet 2.1 or coloured tape to mark ‘start’ and ‘finish’ lines on a desk or the
floor as a test area for your program. Experiment to see if you can get your Edison to drive
backwards from the start to the finish line.
There are multiple ways of programming your Edison to drive forward and backwards.
Another way is to use ‘Ed.DISTANCE_UNLIMITED’ for the distance parameter. This
constant turns both of Edison’s drive motors on.
Unlike when you use a number value for the distance parameter,
Ed.DISTANCE_UNLIMITED doesn’t specify an exact value after which the motors will
stop. Instead, it turns the motors on and then moves on to the next line of code. A stop of
the motors will be needed later in the code.
www.edpyapp.com www.meetedison.com
Name_____________________________
Using Ed.DISTANCE_UNLIMITED can be useful when you want to write a program where
the motors only stop once some other event occurs, for example, when an obstacle is
detected.
This program turns on the Edison motors to drive backwards, then waits for 200
milliseconds before turning the motors off.
1. Set the speed in your program to Ed.SPEED_6. How many milliseconds do you
need to input to the TimeWait() function to make Edison drive backwards from the
finish to the start line?
__________
2. Experiment with different speed and TimeWait() input parameters. What are the
fastest and the slowest times you can make Edison drive backwards from the finish
to the start line?
www.edpyapp.com www.meetedison.com
Name_____________________________
Remember to follow the four basic programming steps for Edison using EdPy:
• Step 1: Check the Setup code is using the constants you want.
• Step 2: Write the program, selecting the functions you want to use, and filling in the
input parameters to the values you want.
• Step 3: Check your program for errors using the ‘Check Code’ button.
• Step 4: Download and test your program using your Edison robot.
Remember, if you have Edison V1 robot, make sure that Ed.DistanceUnits = Ed.TIME. The
distance parameter for Ed.TIME is in milliseconds.
Your turn:
Use activity sheet 2.1 or coloured tape to mark ‘start’ and ‘finish’ lines on a desk or the
floor as a test area for your program.
1. What are the correct distance input parameter values so that Edison drives forward,
then backwards between the start and the finish lines?
Task 2: Write a new program that makes Edison drive forward, then backwards between
the start and the finish lines. This time, use the Ed.DISTANCE_UNLIMITED parameter
and Ed.TimeWait() functions. Use Activity sheet 2.1 or coloured tape to mark ‘start’ and
‘finish’ lines on a desk or the floor as a test area for your program.
www.edpyapp.com www.meetedison.com
Name_____________________________
2. What does your new program look like? Write your code down below.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
In the Setup code, you have seen the = notation being used. For example,
Ed.DistanceUnits = Ed.CM. Using the ‘A = B’ notation means ‘set A to the same value as
B.’
Expressions also use notations. These are some of the basic notations in expressions we
can use in Python:
Expression Meaning
A == B Is A the same as B?
A != B Is A not equal to B?
A>B Is A greater than B?
A >= B Is A greater than or equal to B?
A<B Is A less than B?
A <= B Is A less than or equal to B?
Expressions compare the left side to the right side of the notation in the expression.
You can replace the ‘A’ and ‘B’ in the list of expressions above with any value or function
that returns a value. You can also do maths to those values. For example, ‘(A + 2) > B’
means ‘Is A plus 2 greater than B?’
In code, expressions work in a specific order. When your expression includes maths to a
value or calls a function, the expression will resolve the maths or function first. It will then
compare the left side of the expression to the right side and resolve to either ‘true’ or
‘false.’
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Practice resolving expressions. First, write out what each expression means, then resolve
it to either true or false.
If A = 2 and B = 4, what does each of the following expressions mean and what does each
resolve to (true or false)?
1. (A*2) == B
Meaning: ________________________________________
2. A >= B
Meaning: ________________________________________
3. (A+A) != B
Meaning: ________________________________________
Meaning: ________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
While the expression evaluates to TRUE, the program repeats the commands in the loop.
When the expression evaluates to FALSE, the program moves on to the next line of code
outside the loop.
Using indentation
Python uses indentation to group statements or commands together.
In Python, all the statements indented by the same number of character spaces are
considered to be a single block of code.
Look at line 14 of the program. Because ‘pass’ is indented, it is inside of the loop. Line 15
in the program is not indented, however, so line 15 is not inside of the loop.
www.edpyapp.com www.meetedison.com
Name_____________________________
This function does not work for the square button. That’s because the square button is
designed only to be used to stop a program. The square button will always stop any
program running when it is pressed.
In this program, we want to make sure the Ed.ReadKeyPad() in the while loop will wait
until a button has been pressed and not consider any button presses that happen before
the while loop. That’s why we put Ed.ReadKeyPad() into the line above the while loop (line
12). This will clear any previous key presses before the loop.
You should always follow this process when using a ‘read’ function inside a loop.
Your turn:
Write the program.
Be sure that when you write the while loop, you include the colon and that you indent
correctly:
while loop command state of the keypad
Download the program and press the triangle button to start the program. Wait a bit, then
try pressing either the triangle or round button.
1. What did the robot do when you pressed the triangle or round button a few seconds
after starting the program?
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
2. Run the program again and try pressing the square button instead of the round or
triangle button. What happened? Why did this happen? Explain.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. Now try adding some more code at the end of the program. The new code you write
should make Edison drive backward after you press either the round or the triangle
button again. In other words, your program should tell Edison to drive forward the
first time a button is pressed, then backwards when a button is pressed again.
Remember to include the colon and to indent your code inside the while loop. What
does your new program look like? Write your code down below.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
FINISH LINE
START LINE
www.edpyapp.com www.meetedison.com
Name_____________________________
Look at line 13 of the program. Remember that the Ed.Drive function has three input
parameters: direction, speed, and distance. In this program, the distance parameter is not
a number but is ‘degreesToTurn’.
This is a variable.
In Python, variables are reserved memory locations to store values. This means that when
you create a variable, you reserve some space in the program’s memory.
Look at line 12 of the program. This is where the value of ‘degreesToTurn’ is being set.
This is called assigning a value to a variable. In Python, the equal sign (=) is used to
assign values to variables.
You can use variables to store values that are used in several places throughout a
program. This can be very helpful, especially if the value of a variable changes. By using
variables, you only need to make the change in one line of the code to adjust the value
everywhere that variable is being used in your program.
Your turn:
Write the program, then download and run the program with your Edison. Use activity
sheet 3.1 or coloured tape to mark ‘start’ and ‘end’ angle marks on a desk or the floor as a
test area for your program.
1. Describe what the robot does and why it does this when you run the program.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Add a new line to the end of your program (after line 13) and add the following code to
your program:
Ed.Drive(Ed.SPIN_LEFT,Ed.SPEED_6,degreesToTurn)
Download and run the program with your Edison in your test area.
2. Describe what the robot does and why it does this when you run the updated
program.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Now edit your program so that your Edison will first turn 180 degrees to the right, then 180
degrees to the left.
Hint: Remember you can change the value of the degreesToTurn variable.
Download and run your updated program with your Edison in your test area to see if your
change was successful.
3. What line or lines in your program did you change? Write down the updated line or
lines.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Variable names must follow certain rules in Python. For example, no special characters
like # or $ are allowed. Try changing the name of the variable ‘degreesToTurn’.
Experiment with different possible names and use the ‘Check Code’ button to find which
names are allowed and which are not.
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write a program that makes your Edison robot turn left exactly 180°.
Hint: Try using the program you used in worksheet 3.1 as a starting point.
Download your program and test it using activity sheet 3.1 or coloured tape to mark ‘start’
and ‘end’ angle marks on a desk or the floor as a test area for your program. Remember to
experiment with your program. If your Edison doesn’t turn exactly 180°, try adjusting your
input parameters and test again.
1. What are the input parameters you used to make the robot turn exactly 180°? If you
used a variable, include what value you assigned to that variable.
________________________________________________________________________
________________________________________________________________________
_______________________________________________________________________
EdPy has a command called ‘Ed.DriveRightMotor()’ which makes only Edison’s right motor
move. If only the right motor moves, which way will Edison turn? Hold Edison in your
hands and imitate what will happen if only the right motor moves.
Search for the Ed.DriveRightMotor() command in the Documentation window of the EdPy
app to see how this function works.
Then write a program that makes your robot to turn left 180° using the
Ed.DriveRightMotor() function.
2. What are the input parameters you used to make the robot turn left exactly 180°
using the Ed.DriveRightMotor() command?
________________________________________________________________________
________________________________________________________________________
_______________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Download your program and test it using activity sheet 3.1 or coloured tape to mark ‘start’
and ‘end’ angle marks on a desk or the floor as a test area for your program.
Your turn:
Write a program to make the robot turn right exactly 90° when the triangle button is
pressed once, then turn left exactly 270° when the triangle button is pressed a second
time.
Remember to put Ed.ReadKeyPad() into the line above each ‘while’ loop to clear any
previous key presses before the loop.
1. What does your program look like? Write your code down below.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write a program so that your Edison robot will drive through the mini maze on activity
sheet 3.2 when you hit the play (triangle) button.
Use the robot programming knowledge that you’ve gained so far to write a program which
uses multiple functions to allow Edison to make it through the maze’s turns.
1. Describe the sequence of moves your robot did to complete the maze.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Challenge 1: Race!
Who can get through the maze the fastest, without cheating?
Remember: your robot must start from behind the start line, stop after the finish line and
must not drive over any border lines to win.
www.edpyapp.com www.meetedison.com
Name_____________________________
Competitor: _______________________
Winner: _______________________
_________
Remember: your robot must start from behind the start line, stop after the finish line and
must not drive over any border lines to win.
www.edpyapp.com www.meetedison.com
Name_____________________________
START 0°
Left Right →
END 90°
END 180°
www.edpyapp.com www.meetedison.com
Name_____________________________
FINISH LINE
START LINE
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write a program so that when your Edison robot drives, it makes a square. Use the
commands you have already learned, including Ed.Drive() and a variable. Be sure your
program ends with your Edison in the same spot it started.
Download your program and test it using activity sheet 4.1, placing your Edison at the
‘start’ point and following the lines. You can also make a larger square using coloured tape
to mark the lines and a ‘start’ point on a desk or the floor.
1. What does your program look like? Write your code down below.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. Do you have any duplicate lines of code in your program? If so, what are they and
how many times did you use each?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Using worksheet 4.1, you wrote a program that used the same commands multiple times.
You needed to use Ed.Drive() with a direction parameter of Ed.FORWARD four times,
once for each side of the square. You also needed to use Ed.Drive() with a direction
parameter of Ed.SPIN_LEFT four times, to turn each corner.
Did you find writing the same commands many times a bit boring?
Repeating the same commands over and over is no problem for a computer, but writing
out a program this way isn’t very efficient. Instead, it is better to use a loop structure.
We can write a program to make Edison drive in a square with less code by using a ‘for’
loop. This will make writing the program more efficient. Since we will need to use fewer
lines of code, using the ‘for’ loop will also help reduce the likelihood of mistyping and
having a syntax error in the program.
Using a ‘for’ loop allows you to repeat (also called ‘iterate over’) a block of statements as
many times as you like.
The ‘for’ loop often goes together with the ‘range()’ function in Python.
In EdPy, range() only has one input parameter. That input parameter determines the upper
limit of the set and the lower limit is always 0.
Example:
www.edpyapp.com www.meetedison.com
Name_____________________________
play a beep
13 for i in range(4):
14 Ed.PlayBeep()
15 Ed.TimeWait(1,Ed.TIME_SECONDS)
In this example, the ‘for’ loop iterates four times, causing the variable ‘i’ to have the values
0, 1, 2 and 3. Each time it iterates, the loop executes the statement block consisting of
Ed.PlayBeep() and Ed.TimeWait().
The result? The beep is played four times with a one-second delay in between each beep.
Your turn:
Write a program using the ‘for’ loop and the ‘range()’ function so that when your Edison
robot drives, it makes a square. You should be able to complete the program using just
two Ed.Drive() functions, one for forward and one for spin.
Don’t forget to include a colon and proper indentation inside your loop.
Download your program and test it using activity sheet 4.1, placing your Edison at the
‘start’ point and following the lines. Be sure your program ends with your Edison in the
same spot it started. You can also make a larger square using coloured tape to mark the
lines and a ‘start’ point on a desk or the floor.
1. What does your program look like? Write your code down below.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write a program so that when your Edison robot drives, it makes a triangle.
Download your program and test it using activity sheet 4.2, placing your Edison at the
‘start’ point and following the lines. You can also make a larger triangle using coloured
tape to mark the lines and a ‘start’ point on a desk or the floor.
1. How many times did your ‘for’ loop execute for your triangle shape?
___________________
Write a program so that when your Edison robot drives, it makes a hexagon.
Download your program and test it using activity sheet 4.3, placing your Edison at the
‘start’ point and following the lines. You can also make a larger hexagon using coloured
tape to mark the lines and a ‘start’ point on a desk or the floor.
2. How many times did your ‘for’ loop execute for your hexagon shape?
_________________________
3. You should see a pattern emerging between the number of sides of the shape and
the number of times the ‘for’ loop executes. Describe that pattern.
________________________________________________________________________
________________________________________________________________________
4. How many times you would need the ‘for’ loop to execute to draw a regular
(meaning that all sides are equal) 12-sided shape?
____________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write a program where your Edison robot drives in a circle. Your Edison needs to drive in
the shape of a circle, not just spin in one spot.
Download your program and test it using activity sheet 4.4, placing your Edison at the
‘start’ point and following the line. You can also make your robot drive around any circular
object, like a round rubbish bin or a round table.
Hint: A shape with many hundreds of very small sides can closely approximate a circle.
1. How many times does your loop execute to make your shape a circle?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
2. How far does your robot travel each time you execute your loop?
________________________________________________________________________
________________________________________________________________________
3. Does your robot drive in a perfect circle? If not, can you think of a reason why not?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Starting point
www.edpyapp.com www.meetedison.com
Name_____________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Starting point
www.edpyapp.com www.meetedison.com
Name_____________________________
You can play individual musical notes through Edison’s small speaker using the
Ed.PlayTone() function in EdPy.
The Ed.PlayTone() function takes two input parameters: the note and the duration. The
note determines what note to play and the duration determines the given length of time the
note should be played.
note duration
Parameter input options Plays musical note Parameter input options Plays note for
Ed.NOTE_A_6 low A Ed.NOTE_SIXTEENTH 125 milliseconds
Ed.NOTE_A_SHARP_6 low A sharp Ed.NOTE_EIGHTH 250 milliseconds
Ed.NOTE_B_6 low B Ed.NOTE_QUARTER 500 milliseconds
Ed.NOTE_C_7 C Ed.NOTE_HALF 1,000 milliseconds
Ed.NOTE_C_SHARP_7 C sharp Ed.NOTE_WHOLE 2,000 milliseconds
Ed.NOTE_D_7 D
Ed.NOTE_D_SHARP_7 D sharp
Ed.NOTE_E_7 E
Ed.NOTE_F_7 F
Ed.NOTE_F_SHARP_7 F sharp
Ed.NOTE_G_7 G
Ed.NOTE_G_SHARP_7 G sharp
Ed.NOTE_A_7 A
Ed.NOTE_A_SHARP_7 A sharp
Ed.NOTE_B_7 B
Ed.NOTE_C_8 high C
Ed.NOTE_REST rest
note parameter
duration parameter
13 Ed.PlayTone(Ed.NOTE_B_6,Ed.NOTE_HALF)
Using the parameter values tables as a reference, can you work out what the program will
do?
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
When Edison plays sounds, it does this in the background. This means that as soon as
Edison starts playing the sound, the program will move onto the next line of code. The
sound will keep playing ‘in the background’ while Edison continues on with the program.
If you want Edison to wait for the sound to finish, you need to use the Ed.ReadMusicEnd()
function in a ‘while’ loop.
www.edpyapp.com www.meetedison.com
Name_____________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
2. Look at line 13 and 14 of the program. Remember that expressions compare the left
side to the right side of the notation in the expression. What is this loop doing?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
4. Why did this program behave differently than the last program?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Using the Ed.PlayTone() function, you can customise the exact frequency of the sound
that Edison’s speaker produces by using numbers and variables.
Frequency in acoustics
As you may know, sound travels in waves called sound waves. Acoustics, the branch of
physics that deals with sound and sound waves, looks at everything to do with sound,
including how to measure it.
One way to measure sound is by measuring frequency. Frequency is the number of waves
passing a point in a certain period of time.
Frequency is most often measured in cycles per second (cycle/sec). The base unit for
frequency is hertz, abbreviated Hz.
A period is how long it takes an acoustic wave to complete a full cycle. Since we are using
hertz, we measure frequency in cycles per second.
www.edpyapp.com www.meetedison.com
Name_____________________________
To convert a frequency into a period, divide the number 8,000,000 by the desired
frequency. For example, to play a 1kHz (1000 cycles per second) sound:
8000000
= 8000
1000
Your turn:
13 Ed.PlayTone(8000,2000)
To make the alarm program, you will need to use a ‘for’ loop, variables and the range()
function. You also need to nest a ‘while’ loop into the program.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Tracing involves stepping through the program line by line, recording important values. It is
often done to help find errors or ‘bugs’ in code, but it is also useful when you just need to
understand what is happening in a program.
Try to ‘trace’ through what is happening in the program and answer the following questions
about the program.
2. Fill in the following table by calculating the period parameter for each given value of
‘i’ in the above code. The first value is filled in for you.
___________________________
4. What is the maximum value of the period parameter input to the PlayTone()
function?
__________________________
____________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Try it!
The application of acoustics in technology is called acoustical engineering.
Try some acoustical engineering of your own. Experiment with modifying the parameters
to PlayTone() to make the program play a different combination of sounds.
www.edpyapp.com www.meetedison.com
Name_____________________________
You can get Edison to play a tune using the Ed.PlayTune() function and a special type of
input called a ‘string.’
In the EdPy app, we need to use a string to play a musical tune. We call this a ‘tune string.’
Tune strings are a special string of characters that represent particular tunes. Tune strings
are made up of notes and duration inputs, which are represented by single characters.
A tune string looks like this: “ndndndndnd…ndz” where n is a note from the notes table
and d is duration from the duration table:
To create a tune string, you need to call the function Ed.TuneString(), which has two input
parameters. The size of the string (in other words, the number of characters in the string)
is the first parameter, and the actual string you want to play is the second parameter.
You can change the speed your tune plays by changing the Ed.Tempo variable in the
Setup code.
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write the following code to play the tune ‘Mary Had a Little Lamb’:
“e4d4c4d4e4e4e2d4d4d2e4g4g4e4d4c4d4e4e4e4e4d4d4e4d4c1z”
Hint: Remember you can use the autocomplete feature in the EdPy. Try typing
‘Ed.TEMPO’ and see all the possible values for Ed.TEMPO the autocomplete brings up.
________________________________________________________________________
________________________________________________________________________
2. Which Ed.TEMPO value will make the tune play the fastest?
______________________________________________________________________
3. Modify your program to only play some part of the tune. Describe the changes you
had to make to your program to only play a part of the tune.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
In most good dance performances, there are some moves or actions which are repeated.
You can make your Edison repeat actions in a dance routine by using the ‘for’ loop.
The ‘shimmy’ is a dance move where you hold your body still and quickly move your
shoulders back and forth.
Look at the following program which will make your Edison robot do a version of a shimmy:
This program uses variables so that it will be easy to change the turning speed, the
number of twists in the dance and the degrees Edison will turn.
Both lines 13 and 18 start with ‘#’ which means these lines are comment code lines added
to make it easier for us to read the program. Remember, Edison will skip any line that
starts with ‘#’.
Look at lines 19 and 23. In these lines, we are doing a mathematical calculation in our
code to make Edison turn only half the number of degrees.
Your turn:
Download the program to your Edison and run it to see the dance in action.
www.edpyapp.com www.meetedison.com
Name_____________________________
___________________________
___________________________
3. The first turn to the right is only half the distance of all the turns inside the ‘for’ loop
because this line has the input parameter ‘degreesToTurn/2’. Why do want this line
in the program? Try removing the maths (the /2) and run the program again. What
do you notice? (Hint: look at how far Edison moves left compared to the start point.)
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Try it!
Experiment with the program. Try changing the variables to change the way Edison
dances. Change the number of degrees Edison will turn, the speed Edison will turn, the
number of twists in the dance or all three!
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write and run the following program that combines a ‘shimmy’ dance with some tones:
Now design your own dance for your Edison, adding some tones or using a tune string.
Can you synchronise it so Edison dances in time with the music?
1. Describe your robot’s dance moves. Is there anything in your program you really
liked? If so, describe it.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
2. What combination of tones or notes did you play along with your dance?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Flowcharts in programming
Professional programmers usually plan out their program before they start writing their
code. Using flowchart diagrams is a way that programmers can organise and plan out their
programs.
The idea of a flowchart is to graphically summarise what happens in the code without
needing to go into all of the detail. Flowcharts allow a programmer to visualise and
communicate how the ‘flow’ of the program will work.
In a flowchart, a program is represented using different shapes and arrows. Each shape
represents a different element in the program, and the arrows show how the elements
work together.
A parallelogram represents an
Input/output
input or an output
More complicated flowcharts may also use additional shapes with different meanings.
When making a flowchart to plan a program, words are often added inside the shapes or
next to the arrows. These words are short summaries of the process or decision.
www.edpyapp.com www.meetedison.com
Name_____________________________
Here is a flowchart for a program that will tell your Edison robot to wait for a clap, then
flash the left LED:
Start
Clap No
detected?
Yes
Left LED on
Wait
Wait
This program will use Edison’s sound-detecting sensor to determine whether or not a clap
has occurred. The result determines how the code flows next.
When you look at this flowchart, you may notice that it doesn’t have an ‘end’ terminator.
That is because this program uses a ‘while’ loop set up in a way to make the program
continue indefinitely.
You can use an infinite loop to make the program planned out in the flowchart.
www.edpyapp.com www.meetedison.com
Name_____________________________
‘While’ loops always need a condition. The loop will repeat any indented code while that
condition resolves to ‘true’.
If we want the ‘while’ loop to repeat infinitely, instead of giving a condition the program
must evaluate, we replace the condition with ‘True’.
‘True’ always resolves to ‘true’. By setting the condition to ‘True’, we have hardcoded the
condition of our ‘while’ loop to be ‘true’.
By using ‘True’ as the condition for the ‘while’ loop in our program, the condition of the loop
can never be false and will repeat infinitely.
Your turn:
Write the above code to program your Edison robot to make the left LED flash when you
clap. Download it and test to see how it works.
1. What is the furthest distance away from your Edison that you can be and still have
the robot sense when you clap?
__________
www.edpyapp.com www.meetedison.com
Name_____________________________
2. What is the purpose of having the TimeWait() function calls in the code? What
would happen if we didn’t have them? Hint: Try running the program without the
TimeWait() function calls.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. Look at the program and the flowchart to compare how the code in the program and
the flowchart relate to each other. Explain what happens in the code when the
outcome of the decision represented in the flowchart is ‘no’.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Try it!
Can you change the program so both the LEDs come on when you clap?
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
1. Why is it necessary to perform an initial read of the clap sensor in line 12? What is
this doing? Hint: Look back at worksheet 2.5.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
The Edison robot’s sound sensor is not just sensitive to claps. The sensors can respond to
any loud sound detected, which is why you can tap near the speaker on the robot to trigger
the sound sensor.
Edison’s motors, gears and wheels all make sounds as they turn, which can trigger the
sound sensor. To prevent the sound of the robot driving from triggering the sound sensor,
you need to alter the program.
You will need to add a TimeWait() function call with an input parameter of about 350
milliseconds to give the robot’s motors time to stop.
www.edpyapp.com www.meetedison.com
Name_____________________________
2. Usually, we write a flowchart to help us plan out our code. For practice, write a
flowchart to match the code you just wrote. Draw your flowchart below or try to use
a program like Google Slides to make your flowchart.
www.edpyapp.com www.meetedison.com
Name_____________________________
As you know, a function is a piece of code that performs a particular role or job in the
program depending on which input parameters are used.
But you might not have realised that all of the functions you have used so far have actually
executed multiple lines of code when run by the program.
That’s because a function is a block of organised, reusable code that is used to perform a
single, related action.
Functions are very helpful because they allow us to program is a more modular way, using
the same block of code at various points throughout the program. To get your program to
run all of those lines of code you only need to type one line: calling that function.
Functions make reusing code easier and more efficient when we program.
Watch a short video by code.org explaining why functions are so useful in programming:
https://youtu.be/8T5acEwfJbw
Anything indented is a part of the function. Anything not indented is not a part of the
function, but the next line of code in the program.
It’s important to note that the functions you create are no different from the functions
you’ve used from the Ed library so far.
www.edpyapp.com www.meetedison.com
Name_____________________________
When you call your function (with or without parameters), the program jumps from the call
to the code in the function (the indented code). The program then runs this code before
returning to the line where you made the call.
If you set your function to return a value, when the program returns to the line where you
made the call, the function call gets replaced by the value that the function has returned.
This is important because the program will always resolve functions, then maths orders
then expressions in this order.
This is so your code is nice and neat. By organising your code this way, all your functions,
whether you are using your own or calling functions from a library, can be written in the
main part of your program in a visually clean, organised way. Your function definitions can
sit outside of this, further down at the bottom of the program.
Your turn:
www.edpyapp.com www.meetedison.com
Name_____________________________
Line 17 in the program is the function call. Lines 21 to 25 are defining the function.
Remember, the convention is to define your function at the bottom of your program to keep
everything neat and organised.
Write the program and download it to your Edison robot. Run the program to see how it
works.
1. We could write a function to make the Edison robot drive in a square shape. Fill in
the missing words in the function below to finish writing this function.
def driveInaSquare():
for i in range(___):
Ed.Drive(_____________,_____________,_____________)
Ed.Drive(_____________,_____________,_____________)
2. Write the syntax for the code to call this function. In other words, what would you
type in your program to call this function?
________________________________________________________________________
Write a program that will drive Edison in multiple squares. You will need to define the
‘driveInaSquare’ function, and your program will need to call this function more than once.
Download and run the program to see how it works.
3. Does the program do what you expected it to do? If not, describe what you
expected and what the program actually did. Describe any issues you had getting
your program to work.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Write your own function which will have Edison do something when you clap. You could
make Edison dance, flash an LED, turn in a circle or anything else you like!
www.edpyapp.com www.meetedison.com
Name_____________________________
Step 1: Design
First, write a flowchart which summarises your program graphically. Either make your
flowchart by drawing it out on paper or use a program such as Google Slides. Be sure to
use the correct shapes in your flowchart to represent the program’s start, processes,
decision points and flow.
The shapes you will need will depend on your flowchart. At a minimum, you will need the
oval start shape, the rectangle process shape and the diamond decision shape in your
flowchart.
Step 2: Code
Translate your idea into code in the EdPy app. Use your flowchart as your guide and code
your function to include each step you laid out in your flowchart.
Step 3: Test
Write a test program that includes your function and additional code to ‘exercise’ your
function. (Exercising a function means calling it in your code.) See if your function works
as you planned and expected it to work. If not, revisit your flowchart and code, adjusting as
needed. Experiment to see what works!
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
After scanning the barcode, set Edison down on a table or desk and remove any obstacles
in front of Edison. Then press the play (triangle) button. Edison is now in calibration mode.
1. Repeatedly press the play (triangle) button, which increases sensitivity, until the red
LED on the left is flickering.
2. Repeatedly press the record (round) button, which decreases the sensitivity, until
the LED completely stops flickering.
3. Press the stop (square) button to switch over to calibrate the right side.
4. Repeatedly press the play (triangle) button until the right red LED is flickering. Now
repeatedly press the record (round) button until the LED completely stops flickering.
5. Press the stop button to complete the calibration.
It is possible to set the distance that obstacles are detected. To do this, scan the ‘calibrate
obstacle detection’ barcode, place an obstacle in front of Edison at the distance you want
Edison to detect obstacles, press the play button and then repeat steps 1 through 5.
www.edpyapp.com www.meetedison.com
Name_____________________________
Did you know? Even though people cannot see it, infrared is a type of light.
Therefore, it will work in the dark. That’s why you can turn on a TV set using a
remote control even if there are no lights on in the room!
Edison’s infrared system is made up of two IR light emitter diodes (or LEDs) on the front.
One is on the left, and one is on the right. Edison also has an IR sensor on the front,
directly in the middle.
For Edison to detect obstacles, infrared light is emitted forward from the left and right IR
LEDs. If the IR light encounters an obstacle, such as a wall, it is reflected back towards
Edison. Edison’s IR sensor then detects the reflected light.
IR light emitter
IR light detector
IR light emitter
Edison emits IR light (shown in red) from both the robot’s left and right IR LEDs. Reflected
IR light (shown in blue) bounces off obstacles and is detected by Edison’s IR sensor.
In this picture, there is an obstacle in front of Edison’s left side, but not the right side.
That’s why only IR light from the left emitter is reflected.
From the received signal Edison can determine that there is an obstacle to the left, but no
obstacle to the right.
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
1. Draw the emitted IR light and reflected IR light for this obstacle.
IR light emitter
IR light detector
IR light emitter
www.edpyapp.com www.meetedison.com
Name_____________________________
Look at line 13 of the program. This line turns Edison’s obstacle detection beam to ‘on’.
Whenever you want to use Edison’s obstacle detection beam in an EdPy program, you
always need to turn the beam to ‘on’ before the beam is used in the program.
Now, look at line 15 of the program. This line sets the speed to 5 in this program. When
using obstacle detection, you need to use a slightly lower speed to allow the robot to
detect an obstacle before colliding with it. If the speed is too fast, the robot will crash into
obstacles before being able to detect them.
Your turn:
You need to choose good obstacles to use with Edison. If an obstacle is too small or
doesn’t reflect enough infrared light, Edison cannot detect it. Select an object that is
opaque but not too dark (e.g. not black) and at least as tall as Edison.
For this program, the wall of the room would be a good obstacle.
www.edpyapp.com www.meetedison.com
Name_____________________________
When you want to run a program using obstacle detection, it is a good idea to check that
your Edison robot’s obstacle detection is calibrated to the distance you want. Use activity
sheet 7.1 to calibrate your Edison. You may need to calibrate your robot with custom
sensitivity to make sure your robot can detect and stop at your obstacle.
Write the program using the EdPy app and download it to your Edison robot. Then run the
program to see how it works.
Experiment using different obstacles to see what Edison can and cannot detect. You can
also try setting Edison’s obstacle detection calibration to different distances to see what
happens.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
2. Think about where you have seen this type of invisible detection before in the real
world. Describe an example.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. Where else do you think this type of detection technology could be used? Write
down at least one idea of how you could use this technology.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
This program tells Edison to drive forward until an obstacle is detected. Once Edison
detects an obstacle, the program tells Edison to turn 180° and drive away for 10 cm.
Your turn:
Write the program using the EdPy app and download it to your Edison robot. Then run the
program to see how it works.
After you have run the program, look at it again and think about how you could change the
program. Try to modify the code so that the robot behaves differently when it detects an
obstacle ahead. (Hint: try using sound and lights.)
1. Think about how you could improve the original program. What could you change
so that the program does more than just turn and drive away after encountering one
obstacle?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Programmers often make ‘typo’ like errors, called syntax errors. It is important to be good
at spotting your own syntax errors so you can correct your code.
You can also get another type of error, called a logical error, when you program. In coding,
any error that is not a syntax error is a logical error.
When there is a logical error in a program, the code doesn’t behave the way that the
programmer expects it to. In EdPy, if you have a logical error in your program, the program
will usually still download to Edison. When you run the program, however, it won’t behave
the way you think it should.
A logical error can be as simple as using the wrong function or leaving out a function. An
example of a logical error would be writing a program that uses obstacle detection but not
turning the obstacle detection beam on.
The program is meant to make the Edison robot drive until it encounters an obstacle, then
turn 135° and drive away from the obstacle for 20 cm.
There are five errors in the program. Can you identify all five errors in the program and
determine if they are syntax errors or logical errors? Fill in your answers in the table on the
next page.
www.edpyapp.com www.meetedison.com
Name_____________________________
Hint: You can write this program in EdPy and use the ‘Check Code’ button to help you find
the syntax errors.
www.edpyapp.com www.meetedison.com
Name_____________________________
Many programming languages, including Python, allow programmers to create code which
can respond to some set of events. This type of programming is called ‘event driven
programming’.
Whenever you write a program which is event driven, you will also need to write code
which handles these events. There are two main ways to do this, either by having the
program wait in a loop or by using interrupts.
You can also affect the way a program runs by using an ‘interrupt’.
An interrupt is a section of code that pauses the main program and runs itself. Once the
interrupt code is complete, the program returns to wherever it left off in the main program.
Interrupts are always functions defined somewhere in the program. They are generally
short sections of code designed to perform a specific task without causing a major
disruption to the flow of the main program.
Programmers use interrupts because interrupts allow a program to react to an event at any
time while the program is running. In other words, by using interrupts, a programmer
doesn’t have to predict exactly when during a program the event will occur.
To use an event handler, you first need to set up, or ‘register’, the event handler in your
code. Once registered, the event handler constantly monitors for its given event.
Whenever that event occurs, the event handler triggers the interrupt, which calls and runs
the function, then returns to the main program.
www.edpyapp.com www.meetedison.com
Name_____________________________
The ‘Ed.RegisterEventHandler’ function has two parameters. The first parameter is the
event that is going to occur and the second parameter is the function that will be called
whenever that event occurs.
Look at line 13 of the program. This line is registering an event handler so that whenever
‘EVENT_OBSTACLE_AHEAD’ occurs, the ‘avoidObstacle’ function will be called.
Your turn:
Write the program using the EdPy app and download it to your Edison robot. Then run the
program to see how it works.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
2. In this program, when the robot detects an obstacle ahead, which lines of code will
be executed? Why does the program run these lines?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Try it!
What else could Edison do when it detects an obstacle ahead? Try to modify the code so
that Edison performs a different behaviour when it detects an obstacle ahead. Experiment
with the program to see what works and what does not.
www.edpyapp.com www.meetedison.com
Name_____________________________
If statements
An important part of coding is making decisions. The most common way to do this is to
use an ‘if statement’.
An ‘if statement’ asks whether a condition is true or false. If the result is true, then the
program executes the block of statements following the if statement. If the result is false,
the program ignores the statements inside the if statement and moves to the next line of
code outside of the if statement.
This program uses an if statement to give the robot the ability to make decisions without
human guidance. When a robot can make decisions on its own in this way, it is called an
autonomous robot.
Your turn:
Write the above program using the EdPy app, download it to your Edison robot and run the
program. Then try moving an obstacle, like your hand, in and out of Edison’s obstacle
detection beam to see what happens.
www.edpyapp.com www.meetedison.com
Name_____________________________
1. Edison can now react differently to different stimuli, making a ‘decision’ as to what
to do. Does this mean Edison has intelligence? Hint: You may want to do a bit of
research on artificial intelligence to help you decide.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
In addition to telling a program what to do when an if statement condition is true, you can
also tell a program what to do if that condition is false.
If, else
Using if statements with ‘else’ allows us to write programs making more complicated
decisions. If/else statements are basically a way of making a decision between two things.
Watch Bill Gates, founder of Microsoft, explain if/else statements and decision making in
programming: https://youtu.be/fVUL-vzrIcM
if expression:
statement(s)
else:
statement(s)
The program moves sequentially from the top down, starting with the if condition. If the if
statement is true, the program runs the indented code for the if expression and skips the
else. If the if statement is false, however, the program skips that section of indented code
and runs the ‘else’ indented code instead.
www.edpyapp.com www.meetedison.com
Name_____________________________
Download and run the program. Try moving an obstacle in and out of Edison’s obstacle
detection beam to see what happens.
This program has two pathways: one for if an obstacle is detected and one for if no
obstacle is detected.
if expression:
statement(s)
elif expression:
statement(s)
else:
statement(s)
‘Elif’ is how you say ‘else if’ in Python. You can use elif to write a program with multiple if
conditions.
A program using if/elif/else still moves sequentially from the top down. Once the program
runs any indented code inside any part of the if statement structure, it will skip the rest of
the structure and move on to the next line of code outside the structure.
This means that if the if statement at the top is true, the program runs the indented code
for the if expression and skips any elif sections as well as the else section if there is one. If
the if statement is false, however, the program skips that section of indented code and
moves to the first elif section.
www.edpyapp.com www.meetedison.com
Name_____________________________
Again, if the first elif condition is true, the program runs its indented code and skips
everything below it in the if statement structure (any other elifs and the else condition if
there is one). If this elif condition is false, the program moves to the next part of the if
statement structure and so on.
This program has three different paths that it can take when an obstacle is detected based
on where the detected obstacle is in relation to Edison.
Write the program using the EdPy app and download it to your Edison robot. Then run the
program to see how it works.
2. When running this program, explain in your own words what the robot does when:
Phototransistor
LED
Surface
The line tracking sensor’s LED shines light onto the surface that the Edison robot is driving
on.
Your turn:
When more light is reflected back to Edison’s phototransistor, it gives a higher light
reading.
Experiment to see whether a white or a black surface will reflect back more light.
Use activity sheet 8.1 or one piece of white and one piece of black paper. Turn Edison on
and press the round button twice so that the line tracking LED comes on. Lift Edison up
from the paper slightly and have a close look at the round spot of light that the LED shines
on the surface. Compare how bright the spot of light appears when placed on a black
surface and then on a white surface.
1. Does the LED light spot appear brighter when placed on a black or a white surface?
________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
As you saw in task 1, there is more light reflected from a white surface than from a black
surface. That’s why the light appears brighter on a white surface.
When the phototransistor is on a white surface, it gives a higher light reading than when it
is on a black surface. A black surface is, therefore, considered to be ‘non-reflective’ and a
white surface is considered to be ‘reflective’.
2. Think about how the line tracker would respond to each of the following surface
colours. Would the line tracker see each colour as reflective or non-reflective?
Remember, Edison’s line tracker LED emits red light. (Hint: you can test your
answers using activity sheet 8.1.)
Watch the video Humans Need Not Apply to see more about what the widespread
uptake of robots might mean for the future of humans at work:
https://www.youtube.com/watch?v=7Pq-S557XQU (15 minutes)
www.edpyapp.com www.meetedison.com
Name_____________________________
Look at line 13. This line calls the function Ed.LineTrackerLed() and turns the state to ‘on’.
Just like with Edison’s obstacle detection beam, to use the line tracking sensor in a
program, you must first turn the sensor on. Turning the line tracking sensor on will also
activate the line tracker’s red LED.
Now, look at line 19. This line calls the Ed.PlayBeep() function. This line doesn’t affect the
way the line tracking program works. Instead, this line’s purpose is for debugging.
Debugging
Debugging is the process of finding ‘bugs’ or errors in your program. Often, programmers
will put lines like line 19 in this program into their code to keep track of the program’s flow.
Let’s say you run your program, but the robot does not stop at the black line. There are
two possible reasons: (1) the robot may not be detecting the black line or (2) there could
be a mistake in the final Ed.Drive() command.
If we hear the beep played, we would know that the black line was detected. Therefore, we
know that the error was in the next command. This extra debugging code helps us to
determine the error more easily.
Other functions could also be used for debugging, such as the Ed.LeftLed() command. For
example, you could use this command to turn the left LED on to indicate that a certain
point in the program has been reached.
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Write the program using the EdPy app and download it to your Edison robot. Use the black
line on activity sheet 8.1 to test the program. You can also draw a black line on a piece of
white paper or use black electrical tape on a white desk.
Note: Whenever you use Edison’s line tracking sensor in a program, always start
the robot on the white (reflective) surface – never the black (non-reflective) surface.
Put Edison on the white surface and drive the robot towards the black line.
Then try running the program again using each of the three coloured lines on activity sheet
8.1 one at a time. Drive Edison towards each coloured line to test whether or not the robot
will detect the line and stop.
1. Are there colours that Edison can’t detect very well? If so, which colour(s)?
______________________________
2. Why do you think you got the answer you did for question number 1? Why can’t
Edison detect that colour(s)?
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. Pretend you were programming your Edison robot to drive a slalom course with
three slalom flags. Describe how you could use the Ed.PlayBeep(), the Ed.LeftLed()
or Ed.RightLed() functions in a program for debugging purposes and what they
would do.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Pseudocode
Planning out your program before you begin coding is an important and useful skill in
programming.
One way to do this is by using a flowchart to summarise your program flow, which you
learned about in lesson 6. Pseudocode is another way to represent your program before
you begin coding.
When you plan out your program using pseudocode, you should indent in the same way
you would in the programming language so that the pseudocode is easy to read and
understand.
Here is an example of some pseudocode describing a program which makes the Edison
robot stay within a non-reflective border using the line tracking sensor:
Compare the pseudocode to the program. Do you see how the pseudocode translated into
the Python program?
www.edpyapp.com www.meetedison.com
Name_____________________________
Your turn:
Download the program to your Edison robot and run it to see how it works.
Use activity sheet 8.2 as a border to test the program. You can also create your own
border using a large piece of paper and a thick black marker or use black electrical tape on
a white desk or the floor to create a large border.
________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
What is an algorithm?
Let’s say you want to teach your friends how to make fruit pies. If you know that all of your
friends have apples, you can just write down one recipe for apple pie.
Not all of your friends might have apples, however. What if one of your friends has
blueberries, another has cherries, and a third has apples? They cannot all follow the apple
pie recipe. You would need to write a separate recipe for each different fruit.
What if you don’t know what fruit each of your friends has? How could you teach them to
make fruit pies?
No matter what fruit they have, all of your friends need to follow the same basic
instructions: make the dough, fill the pie with the fruit, then bake the pie.
An algorithm is a broad set of instructions to solve a set of problems. An algorithm lays out
a process or set of rules to be followed in order to solve any problem in the set.
Algorithms in programming
In programming, we often have sets of problems we want to solve.
In this activity, for example, we want to be able to have Edison follow any black line. Our
set of problems, therefore, is ‘follow any black line’. Any specific line you make for Edison
to follow is a new problem inside this set.
Let’s say you draw a black line for Edison to follow. To get Edison to follow your line, you
could write a program which makes Edison drive the exact path of the line. If you make a
new line, however, you will need to write a whole new program for that new line.
The algorithm produces a program which will work for all of the problems in the set. This
way, a whole new program isn’t needed for each new problem.
To solve our set of problems, we need the algorithm to produce a set of instructions that
will work for any black line.
You can plan out an algorithm using pseudocode or a flowchart, just like you do when you
plan out a program.
www.edpyapp.com www.meetedison.com
Name_____________________________
Here is an algorithm in pseudocode that will allow Edison to follow any black line:
Notice that no speeds or distances are mentioned in the pseudocode. Those sorts of
details are usually left to the coding stage.
Your turn:
Translate the pseudocode algorithm into a Python program to make your Edison robot
follow any black line. Experiment with different speeds and distances to achieve the
smoothest line tracking. Download your code into Edison and test it using the track on
activity sheet 8.2.
1. What was the best combination of speed and distance to achieve smooth line
tracking you found?
________________________________________________________________________
2. What did your Python program look like? Write your code here.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
Try it!
www.edpyapp.com www.meetedison.com
Name_____________________________
Make your own line with a black marker on white paper or black tape. Can Edison follow
your line?
www.edpyapp.com www.meetedison.com
Name_____________________________
STOP HERE
www.edpyapp.com www.meetedison.com
Name_____________________________
Remember!
Always start the
Lesson 8: Activity sheet 8.2 robot next to the
line, not on it.
www.edpyapp.com www.meetedison.com
Name_____________________________
In a program, we can have these sensors read the amount of light detected and return this
as a digital value.
This program uses the Ed.ReadLeftLightLevel() function in line 13. This function tells the
program to read the light level from the left light sensor on Edison and return a value
between 0 and 1023.
When the value returned is greater than (>) 100, the program exits the first loop and goes
to the next loop, which sounds an alarm continuously.
Your turn:
Write the program and download it to your Edison robot. Place the Edison robot in the dark
before you press the play button.
Once you have the lights off or have blocked out Edison’s left light sensor, press the play
button. When the lights are turned on, or whatever is blocking Edison’s left light sensor is
removed, the robot will raise the alarm.
www.edpyapp.com www.meetedison.com
Name_____________________________
1. Think of a real-life situation where this type of alarm would be useful. Describe the
situation and how the alarm could be used.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
_______________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
In this program, we are using the ‘less than’ (<) symbol to determine the path that the
program will take.
If the value returned from the Ed.ReadLeftLightLevel() function is less than 100, then the
activateBothLights() function is called with the input parameter of Ed.ON. Otherwise, the
program moves to the ‘else’ part of the if statement, which also calls the
activateBothLights() function, but with the input parameter of Ed.OFF.
Your turn:
Write the program and download it to your Edison robot. You will need to create some
‘tunnels’ for Edison to drive through which will block out the light in order to see Edison’s
front lights come on.
Run the program with Edison driving in and out of tunnels to see how it works.
Then try experimenting with the value (100) in the if statement in line 15 to see what
happens when you run the program with a larger or smaller number.
www.edpyapp.com www.meetedison.com
Name_____________________________
1. What happens when you make the value in the if statement higher?
________________________________________________________________________
________________________________________________________________________
Try it!
Will it make a difference if you use the ReadRightLightLevel() function instead of the
ReadLeftLightLevel()? Modify your program with this change and test it to see.
www.edpyapp.com www.meetedison.com
Name_____________________________
This program compares the light level between the right light sensor and the left light
sensor to determine the flow of the program.
The presence of the torch on either the left or right of the robot will cause the robot to read
a higher light level on that side of the robot. The logic of this program says that when the
right light level minus the left light level is less than zero, the robot drives left towards the
higher source of light, else the robot drives to the right.
Your turn:
In worksheet 5.2, you learned about ‘tracing’ a program. When there are lots of calculation
and different possible values in a program, it can be useful to trace the program. This
allows you to understand the different values that can occur and predict the associated
behaviour.
1. Fill out the following tracing table for this program. The expected behaviour should
either be ‘drive forward right’ or ‘drive forward left’.
www.edpyapp.com www.meetedison.com
Name_____________________________
Write the program and download it to your Edison robot. After you press the play button,
shine a torch at Edison. The robot will drive towards the light. Use the torch to control
where Edison drives.
2. What happens if you change the ‘less than’ symbol (<) to a greater than symbol (>)
in this program?
________________________________________________________________________
________________________________________________________________________
Is a moth which is attracted to light intelligent? What about a robot with the same
behaviour?
www.edpyapp.com www.meetedison.com
Name_____________________________
To turn your Edison into a vampire robot, we will use object-oriented code.
An object is a collection of functions and variables which are related to each other. Objects
are defined by a ‘class’.
A class (also called a class definition) is like a blueprint that describes how to make an
object.
In a program, you can create lots of different objects from a class and then manipulate
these objects individually in your program for different purposes. All of the objects created
from the class will have the same ‘blueprint’ when they are created, but you can then do
different things to different objects throughout your program.
All shapes, like squares, triangles and hexagons, have common attributes. For example,
all shapes have some number of sides and some number of vertices. There are also
common things you do with shapes, like find the area or the perimeter of the shape.
We can use these common elements to create a class for Shape in Python. By creating
the class, we are setting up a blueprint with just the known, common elements. We will
then be able to create many different objects from that class, such as ‘square’ and
‘triangle’.
The syntax for defining a class in Python is ‘class’ and then the name you want to give that
class, followed by a colon (:). For example:
class Shape:
Anything you include as indented code in the lines following this is inside the class
definition.
www.edpyapp.com www.meetedison.com
Name_____________________________
class Shape:
def __init__(self,x,y):
self.x = x
self.y = y
def area(self):
return self.x * self.y
def perimeter(self):
return 2 * self.x + 2 * self.y
This class says that the blueprint for any object created from class Shape will have two
input parameters (x and y). The class also defines three functions: ‘init’, ‘area’ and
‘perimeter’.
The __init__ function is start-up code. The term ‘init’ stands for ‘initialisation’.
When the program needs to create an object, the program goes into the class definition
and runs the __init__ function. This function contains code which sets up the object, which
typically means setting the start values of the object variables.
Functions in a class must always have ‘self’ as their first parameter. This tells the created
object to use its own functions and variables.
In Python, when you want to create an object from a class, you use the syntax
object_name = class_name(parameters).
rectangle = Shape(100,45)
In a program, this calls the __init__ function, which runs, creating an object ‘rectangle’
using the blueprint from the Shape class definition. The object ‘rectangle’ is set to have
100 be the ‘x’ input value and 45 as its ‘y’ input value. (You don’t need to put in ‘self’ –
that’s only a reference for the program to know where to look.)
Once the program creates the object, it can access that object’s functions as defined by
the class.
www.edpyapp.com www.meetedison.com
Name_____________________________
For example:
rectangle.area()
In this example, the rectangle.area() function will return the value of the rectangle object’s
‘x’ input parameter multiplied by its ‘y’ input parameter.
One way to use this particular function would be to store this value in a variable in your
program. For example:
AreaOfRectangle = rectangle.area()
This class definition will allow you to create different Vampire objects.
You can then use these Vampire object in a program for your Edison robot.
www.edpyapp.com www.meetedison.com
Name_____________________________
Write the following code and check that you understand what is happening in the program:
Your turn:
Now it is time to design some more interesting behaviours for your Vampire robot. Be
creative! You can make your robot react to button pushes, claps and obstacles. Consider
using event handling in your program. You can make multiple vampire objects with
whatever ages you want. You can also modify the base program so that your objects have
multiple input parameters.
Think about what you want to include in your program, then work through your program
design by first creating flowcharts and then pseudocode based off of the flowcharts.
www.edpyapp.com www.meetedison.com
Name_____________________________
Step 1: Flowcharts
Draw flowcharts for the daytime and nighttime behaviours, either by hand or using a
program like Google Slides or a similar drawing program. Include your flowcharts here.
Use extra paper if you need.
www.edpyapp.com www.meetedison.com
Name_____________________________
Step 2: Pseudocode
Translate your flowcharts into pseudocode. Write your pseudocode here. Use extra paper
if you need.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Translate your pseudocode into Python code using the EdPy app. Download it to your
Edison robot and run your program to test it out.
Task 3: Problem-solving
In programming, it is very common not to have everything work out the way you think it will
the first time. No sweat! Remember this: Thomas Edison famously failed 10,000 times
before he successfully invented the light bulb. So, keep trying!
We learn from the problems we encounter, so it is a good habit to take time to think about
what went wrong. Describe any problems you had, what ideas you came up with to fix
them, and the solutions you found in later attempts. Use extra paper, if you need.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
www.edpyapp.com www.meetedison.com
Name_____________________________
Choose three different programming structures you used in your program. Describe what
each one does below.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Demonstrate your Vampire robot to your partner, group or class. Talk about your ideas,
the program, the problems that you encountered and how you solved them.
www.edpyapp.com www.meetedison.com