Lesson 3 Programming a LED animation

A Code for Fun Enrichment Programme
Microcontroller Track – Basic Level
Block-Based Programming
TRAINER GUIDE
A COMPUTATIONAL THINKING PROGRAMME BY ZENITANT PTE LTD
Preface
This programme is a set of hands-on-mind-on lessons that reinforce the
learning of STEM concepts in the classroom. The use of Microcontrollers
and Robotics in learning enables the student to “see, feel, touch, control,
and imagine”.
The microcontroller training can be an exciting and dynamic educational
tool that helps students understand complex mechanical designing and
programming concepts. It aids in the understanding of spatial concepts,
construction, failure analysis, design improvements, teamwork, program
management and presentation skills.
The Code for Fun Enrichment Programme
 To increase students’ exposure to coding and computational thinking.
 Learn programming concepts with open-sourced graphical block-based
program, 3D-Tronics
 All lessons will have fun games/challenges, group work, and hands-on
application.
 Curriculum is aligned with STEM.
 Benefits for Students:
 Develop computational thinking skills
 Gain basic understanding and appreciation of coding
 Enhance logical thinking and analytical skills
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|2
Microcontroller Track, Block-Based, Basic Level
 The training plan should be designed for users with no prior experience
in block-based programming;
 The training should demonstrate that an algorithm is a sequential
instruction to execute a task and the process of compiling it into
programs on computing devices;
 The scope of training should cover sequence, repetition, logical
reasoning to explain algorithms and predict the behaviour of simple
programs and;
 The training should train users to design and write simple programs to
accomplish specific goals and to work on simple forms of inputs and
outputs.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|3
Individual Lesson Deliverables for Students
Project 1
Lesson 1 – Introduction to micro:bit
1) About the microcontroller hardware and the graphical block-based programming
environment
2) How to give instructions using the block-based programming environment
3) How the process of compiling the instructions into programs is done
4) How to upload the program onto the microcontroller
Lesson 2 – Programming LED
1) How to give sequential instructions to program a LED
2) How to program a LED sequence with repetition using conditional loops
3) What is Morse code, how it is used and how the algorithm can be demonstrated by
programming a LED sequence
4) How to design and write a simple program to accomplish the challenge
Lesson 3 – Programming a LED animation
1) How to program a LED animation using functions and conditional loops
2) How the algorithm can be demonstrated by programming a LED animation
3) How to design and write a simple program to accomplish the challenge
Project 2
Lesson 4 – Programming the buttons
1) How can a push button be used as a form of input
2) How to use the LED grid to display values by pressing a button
3) How to program a push button to demonstrate logical reasoning using an if else
statement to do conditional checks and then perform the necessary action
4) How to design and write a simple program to accomplish the challenge
Lesson 5 – Controlling the LED using the buttons
1) To define the simple forms of input, process and output
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|4
2) How to program LED to demonstrate logical reasoning using an if else statement to
do conditional checks and sequence repetition using functions and conditional loops
3) How to design and write a simple program using the buttons and LED grid combined
to accomplish the challenge
Project 3
Lesson 6 – Understanding random numbers as a program behaviour
1) How can a random number be used as a form of input
2) How to use the LED grid to display the randomly generated number when button is
pressed
3) How to program the LED grid to demonstrate logical reasoning using an if else
statement to do conditional checks and then perform the necessary action
4) How to design and write a simple program to accomplish the challenge
Lesson 7 – Programming a simple game using random numbers
1) How to program a simple game using variables, functions, conditional loops and
random numbers
2) How the algorithm can be demonstrated by programming a LED animation
3) How to design and write a simple program to accomplish the challenge
Project 4
Lesson 8 – Understanding how the accelerometer works
1) How can an accelerometer be used as a form of input
2) How to use the LED grid to display the X, Y & Z values of the accelerometer
3) How to demonstrate logical reasoning using an if else statement to do conditional
checks and then perform the necessary action
4) How to design and write a simple program to accomplish the challenge
Lesson 9 – Programming the accelerometer
1) How can an accelerometer be used as a form of input
2) How to use the LED grid to display the strength of each shake
3) How to demonstrate logical reasoning using an if else statement to do conditional
checks and then perform the necessary action
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|5
4) How to design and write a simple program to accomplish the challenge
Lesson 10 – Build and Code a Step Tracker
1) How to extract key characteristics and predict the behaviour of the program step
tracker
2) How to design and build the step tracker according the key characteristics
3) How to write simple program and code the step tracker behaviour using simple
forms of inputs and outputs
4) How to debug the programs after testing the step tracker
5) How to work in groups to accomplish the project goals
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|6
Lesson 1
Introduction to micro:bit
_________________________________________________________________________________________________________________________
Theory
micro:bit
What is a micro:bit?
The micro:bit is a handheld, fully programmable _____microcontroller_____
with a lot of built-in features.
D
B
B
A
C
A
25 red LED (light emitting diodes) that can flash shapes, numbers
and messages. The LEDs form a 5x5 grid that can be set to turn on or
off and the _____brightness_____ can be controlled.
B
Buttons A & B are a form of _____input_____. They can detect when
either of the buttons are being pressed and released.
C
An accelerometer on the micro:bit can detect changes in the
micro:bit’s _____speed_____ by converting analogue information into
digital form. The micro:bit can also detect a few standard actions e.g.
shake, tilt and free-fall.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|7
A compass on the micro:bit detects _____magnetic_____ fields such as
the Earth’s magnetic field. As such it is possible to detect the
direction it is facing or moving in in degrees.
D
A low energy Bluetooth connection that can be used to
_____interact_____ with other Bluetooth enabled devices and the
Internet
Block-Based Programming Environment
1. Using Google Chrome, key in the following website: microbit.org/code/
2. Find and click on [Start with this editor] button under JavaScript Blocks
Editor
View Results
© Zenitant Pte Ltd
Code Menu
Program Menu
Programming Area
Microcontroller Block Based – Basic Lessons
|8
Compiling and Uploading Programs
1. Connect the small end of the USB cable to the micro USB port on your
BBC micro:bit and the other end to a USB port on your computer. Your
computer should recognize your micro:bit as a new drive and will
appear as MICROBIT a drive under Devices and drives.
2. Click on the [DOWNLOAD] button to compile your script/code. It will
generate a hex file which will appear as a download at the bottom of the
browser.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
|9
3. You can copy and paste the.hex file from your download folder to the
MICROBIT drive. The LED on the micro:bit will flash quickly during the
transfer (which should only take a few seconds).
4. Once transferred, the code will run automatically on your micro:bit. To
rerun your program, press the reset button on the back of your micro:bit.
5. By copying the hex file onto the MICROBIT drive, you have programmed
it into the flash memory on the micro:bit, which means even after you
unplug the micro:bit, your script will still run if the micro:bit is powered
by battery.
Troubleshooting Note:
1. You cannot drag and drop more than one hex file at once onto your
micro:bit. If you try to drag and drop a second hex file onto your
micro:bit before the first file has finished downloading, then the second
file may fail in different ways.
2. When the first hex file has been written to the micro:bit, the drive will
disengage. If you drag and drop a second hex file at this point it may not
find the drive and the second write will fail.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 10
Task
Program the micro:bit to display LED image.
STEP 
Write the following program to display a LED image.
1. Click on the Basic menu and drag the [SHOW
LEDS] block to the programming area.
2. Choose the following LEDS to display
3. Look at the view results area and see if it displays a square.
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Create your own second and third shape that will show after the first one.
You can add pauses before you create and show the images
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 11
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 12
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 13
Lesson 2
Programming LED
_________________________________________________________________________________________________________________________
Theory
Conditional For Loops
What is a conditional for loop?
A conditional for loop is a set of programming instructions that is
performed at only _____1_____ point of the program repeatedly the counter
number is reached.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 14
Morse Code
Samuel Morse invented the Morse Code, which is a method for transmitting
telegraphic information, using standardized sequences of _____short_____
and _____long_____ signals to represent the letters, numerals, punctuation
and special characters of a message.
Task
Program the LED to perform short and long blinks
STEP 
Write the program for the LED to perform short and long blinks.
Short blink
Long blink
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 15
Challenge
Program the LED to show Morse code message, SOS
S
O S
… --- …
Morse Code Rules
1. A dot takes 1 unit of time.
2. A dash takes 3 units of time.
3. The pause between parts of the letter takes 1 unit of time.
4. The pause between letters takes 3 units of time.
5. The pause between words takes 7 units of time.
S
1
3
1
3
1
1
1
1
1
1
Microcontroller Block Based – Basic Lessons
Pause
3
.
Pause
1
.
Pause
3
.
Pause
© Zenitant Pte Ltd
3
Pause
1
-
Pause
1
-
S
Pause
1
-
Pause
1
Pause
1
.
Pause
.
Pause
1
Pause
.
O
7
| 16
Segment 1
Segment 2
Segment 3
Combine the segments together
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 17
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 18
Lesson 3
Programming a LED animation
_________________________________________________________________________________________________________________________
Theory
Conditional While Loops and Variables
What is a conditional while loop?
A conditional while loop is a set of programming instructions that is
performed at only _____1_____ point of the program repeatedly until a
_____condition_____ is satisfied.
What is a variable?
A variables is a virtual storage location paired with an associated name
which is used to store _____values_____ for subsequent use by the program.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 19
X & Y Axis of the LED Grid
The X axis is the horizontal axis on the LED grid. The Y axis is the vertical
axis on the LED grid. The values of X & Y start from 0 and ends with 4
Task
Program a LED animation to fill up the LED grid row by row from left to
right, bottom to top
STEP 
Write the program to fill up the LED grid row by row from left to right,
bottom to top.
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 20
Challenge
Program a LED animation that shows chasing LED lights looping forever
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 21
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 22
Lesson 4
Programming the buttons
_________________________________________________________________________________________________________________________
Theory
Push Button
What is a push button?
It is an _____input_____ sensor that is pushed to operate an electrical device.
Typical uses include calculators, door bells and emergency stop buttons.
If . . . else Statements
If...else statement allows the programmer to execute _____2_____ sets of
instructions when _____certain_____ conditions are satisfied.
For example, if the input condition is 1, the program will execute
Instructions A and if the input condition is not 1, the program will execute
Instructions B.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 23
Task
Program the entire LED grid to light up when button A is pressed and to
switch off when button A is released
STEP 
Write the program for the entire LED grid to light up when button A is
pressed and switch off when button A is released.
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Program the entire LED grid to light up when button A is pressed and
released and to switch off when button A is pressed and released again
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 24
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 25
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 26
Lesson 5
Controlling the LED using the buttons
_________________________________________________________________________________________________________________________
Theory
Define Input, Process and Output
Component
Input/Process/Output
__________Input__________
Button
__________Output__________
LED Grid
__________Process__________
micro:bit
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 27
Task
Program the LED grid to display a number. When button A is pressed, the
number will reduce by 1. When button B is pressed, the number will
increase by 1. The minimum value is 0 and the maximum value is 9.
STEP 
Write the program for the above task.
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Using the codes from the Task, program the LED grid to countdown to 0,
decreasing by 1 every second when buttons A+B is pressed.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 28
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 29
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 30
Lesson 6
Understanding random numbers as a
program behaviour
_________________________________________________________________________________________________________________________
Theory
Random Numbers
Random numbers are numbers generated by _____computer_____
_____programs_____ to perform tasks in a random manner.
Most computers generated random numbers using algorithms that
can automatically create long runs of numbers with good random
properties.
The series of values generated by such _____algorithms_____ are
generally determined by a fixed number called a seed. The seed often
make use of the computer’s real time clock which measures in
_____milliseconds_____. This provides enough randomness for simple
computer programs, for example video games.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 31
Task
Program the LED grid to display the randomly generated number (0-9)
when button A is pressed
STEP 
Write the program for the above task.
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Program the LED grid to display random letters (A-Z) when button A is
pressed.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 32
(Random MCQ answer generator)
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 33
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 34
Lesson 7
Programming a simple game using random
numbers
_________________________________________________________________________________________________________________________
Theory
What is a variable?
A variables is a virtual storage location paired with an associated name
which is used to store _____values_____ for subsequent use by the program.
Task
Program a simple math game to display a simple math addition question
using the random generated number (0-3), the “+” sign and another
random generated number (1-3) when button A is pressed.
STEP 
Write the program for the above task.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 35
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Using the codes from the Task, program the simple math game to repeat
the math question when button A is pressed again. User will then press
button B ‘X’ number of times to answer the math question and press button
A+B to confirm the answer. If the answer is right, display a string “OK”, else
display the right answer
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 36
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 37
Lesson 8
Understanding how the accelerometer
works
_________________________________________________________________________________________________________________________
Theory
Accelerometer
What is an accelerometer?
An accelerometer is a device used to measure _____force_____ and
_____acceleration_____. It is useful for sensing vibrations and orientation.
What are the values reported by the accelerometer?
The accelerometer reports values that describe the changes in acceleration
along the _____3_____ axes of the coordinate system (X, Y & Z axis).
Task
Program the LED grid to display the X, Y & Z values of the accelerometer
when button A is pressed
STEP 
Write the program for the above task.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 38
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Program the LED grid to continuously display the [STRENGTH] of each
shake using a graphical format
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 39
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 40
Lesson 9
Programming the accelerometer
_________________________________________________________________________________________________________________________
Task
Program the LED grid to display the strength of each shake in numbers
when the micro:bit is shaken
STEP 
Write the program for the above task.
STEP 
Load the new program code to your micro:bit. (Refer to page 9 for instructions)
Challenge
Program the LED grid to display a number. When the micro:bit is shaken,
the number will increase by 1.
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 41
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 42
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 43
Lesson 10
Build and Code a Step Tracker
_________________________________________________________________________________________________________________________
Theory
Identify and Define Characteristics
What are the characteristics of a Step Tracker?
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 44
Draw out how you want the Step Tracker to look like and list down what
components will you be using.
Drawings:
Functional Requirements:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
Components and Building Materials:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 45
Building & Coding
Using the building materials provided and any recycled materials available,
build a prototype of a Step Tracker according to your concept design above.
Prototyping is the early sample of a product/solution created to test a
concept. It is essential for confirming and clarifying _____requirements_____
and _____functionalities_____ for the final real working solution.
Reflection
Reflect on the difficulties encountered, if any, and decide on the next course
of action for more efficient learning.
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
_________________________________________________________________________________
© Zenitant Pte Ltd
Microcontroller Block Based – Basic Lessons
| 46