Description
Loops are a programming method that uses repetition, a fundamental concept in middle and high school programming curricula. Using loops can make writing and maintaining code more efficient and error-free.
This activity uses a simple loop to display a counter when the A button on the micro:bit is pressed. When the B button is pressed, the counter stops and is reset.
Programming Language
MakeCode Blocks
Difficulty Level
Beginner
Age Range
9 and up
Materials
Optional Materials include:
- BBC micro:bit V2
- USB to MicroUSB cord
Programming the micro:bit
Develop the problem statement
The problem statement is: Start and display a counter when the A button is pressed. When the B button is pressed, stop and reset the counter.
Write the algorithm
I want to keep my algorithm generic, so I am going to address the ‘button pressed’ events as signals:
- Set counter to 0
- When ‘start counting’ signaled:
- Repeat until ‘stop counting’ signaled:
- Add one to counter
- Display counter value
- End Repeat
- Repeat until ‘stop counting’ signaled:
- When ‘stop counting’ is signaled:
- Reset counter to 0
- Set loop to stop
Write the pseudocode
The Pseudocode is the version that is linked to the particular programming language we are using:
- Create variables ‘keeplooping’ and ‘counter’
- On Start:
- Set keeplooping to 0
- Set ‘counter’ to 0
- On Button A pressed:
- Set keeplooping to 1
- While keeplooping = 1
- Add 1 to counter
- Display counter
- End While
- On Button B pressed:
- Set Keeplooping to 0
- Set counter to 0
Write and test the code
Here is the block code from MakeCode:
Academic Connections
Programming Concepts
- Repetition (Looping)
- Variables
- Incrementing Variables
- Event Handling