Die Roll Activity
This project introduces the students to the selection condition IF...THEN...ELSE and how to control the individual LEDs.
Teacher GuideOpen Close
The students can:
- Use a gesture input to control the micro:bit.
- Create a randomly generated variable
- Use an IF...THEN...ELSE
- Display own patterns on the LEDs
Standards Alignment (United Kingdom: CAS)
- Designs simple algorithms using loops, and selection i.e. if statements. (AL)
- Declares and assigns variables. (AB)
- Uses a variable and relational operators within a loop to govern termination. (AL) (GE)
- Uses logical reasoning to predict outcomes. (AL)
- Detects and corrects errors i.e. debugging, in algorithms. (AL)
- Creates programs that implement algorithms to achieve given goals. (AL)
- Understands that programming bridges the gap between algorithmic solutions and computers.(AB)
- Uses nested selection statements. (AL)
Once on the workspace the “on” block can be changed to a number of gestures, such as tilt left or right. For this project the shake gesture is selected.
Now your program reacts to the shake input gesture we need to add how it responds. In the variables blocks, add a "set item to" block.
Rename the variable to something more suitable, such as “Roll”. A popup box will allow you to change the variable name.
Now that a variable called “Roll” has been created we needed to pick a random number between 0 and 5. This is 6 different values in total. From the block menu select the “Math” option and select “pick random 0 to 4”
Drag the “pick random” block onto the workspace and connect it to the “Set variable” block replacing the “0”. Now the program will pick a random number between 0 to 5 every time the micro:bit is shaken. The number is stored in the variable “Roll”.
Now the micro:bit needs to display a different result based upon the “roll” variable. This is known as selection. In this case we will be using the IF...THEN...ELSE section. To add selection to the micro:bit it can be found on the block menu under the Logic option.
Add a logic statement of “IF THEN ELSE” and use the cog to edit the logic to have multiple conditions.
As there are 6 possible options, 4 “else if” statements need to be added. Since there is the first “If” statement followed by 4 “else if” statements and the final “else” statement, that covers 6 options in total.
Now we need to create a logical statement for the IF statement to be based upon to allow for selection. Again from the “logic” option on the block menu select the “equals” block.
Once selected drag the “equals” block onto the “IF” statement block.
To ensure the micro:bit bases the IF statement on the random number, the value stored in the variable “Roll” needs to be checked. The “Roll” variable can be found from the variable option in the block menu.
The first of the 6 checks to carry out is if the variable “Roll” contains 0. Place the “Roll” variable within the “equal” block.
Now this process needs to be repeated for the other 4 “else if” statements. By right clicking on the “equals” block it can be duplicated and modified for the different possible values.
The final stage of the program is now to display a different pattern for the different die faces. From the basic option in the block menu, select the “show leds” block.
Drag the “Show leds” block onto the workspace and place under the first “IF” statement. By clicking on the light blue coloured squares, you can create the die pattern required. For this example, the pattern for 1 is completed below.
This block again can be duplicated 5 times for the other die faces until all options have been completed. The final program is shown below. Save the project and download to the micro:bit.
Try to add the following functionality:
- Add an animation of the die faces rolling.
- Allow the user to state the number of die faces i.e. 6, 8 or 12
The 'Truth or Dare' activity uses the gesture command, LED screen and introduces IF statements again, but runs two programming loops at the same time.