Micro:bit Neopixel Numberline Recipe: Basic +/-

Vn Start this recipe by creating your own copy of MBnumberline Engine 1 by clicking on the link below. It will open in a new tab and you can switch back and forth to these instructions.

https://makecode.microbit.org/_2A5fviDXmeLg

Think of this code as a basic function to display whatever you place in the variable NumberLineValue on your number line. Now you just need to add your own code to make create math problems or other functions.

  1. Once you open the link above, click the big pink “Edit” button at the top to start adding your own code.
  2. For our this first example, we will program the four buttons available to you (Button A, B, pin 1 and pin 2) to add and subtract numbers as well as clear the number line back to 0. To begin, we will create an On Button A Pressed event.
    1. From the Input blocks, pull an On Button A Pressed block out to the code area. We want the B button (because it is on the right side) to add 1 to the number line, so change the A in the block you pulled out to a B.
    2. To add 1 to the number line, go to the Variables blocks and pull out Change #### Value by 1.  You will then set the variable to NumberLineValue. Test it in the simulator on the right, then upload it to test on the number line.
    3. When testing, see what happens when you keep pressing the B button more than 20 times. How will we fix this?
    4. We need to add an if-then block to test if the number is over 20. If it is, we will just change the number back to 20 so it doesn’t go off of the number line. From the Logic blocks, drag an if-then block out and place it under the Change value block. Also from the Logic blocks you will need a < conditional block to be placed in the spot right after “if“.  Change the < to a >, from Variables drag a NumberLineValue block and place it before the >, and change the “0” to a 20 after the >. The If statement will now read” If NumberLineValue > 20.
    5. In the space after then, go to the Variables blocks and grab a Set NumberLineValue to block and change the value to 20. Now if you go over 20, your variable will just go back to 20. Download and test!
      Number_Line_Engine_v2_-_makecode_microbit_org.png
  3. Now that you can add 1, wouldn’t it be nice to be able to quickly clear back to 0 and reset everything? We will code your number line so that pressing A&B at the same time will set the NumberLineValue variable right to 0. This one is pretty easy:
    1. From the Input blocks, pull an On Button A Pressed block out to the code area. Change A to A&B.
    2. From Variables grab a set NumberLineValue to block and leave the value to zero, placing it in the On Button A&B Pressed event.
    3. Download and test!
      Number_Line_Engine_v2_-_makecode_microbit_org.png
  4. To finish up, we need to program the A, 1, and 2 buttons so that:
    1. The A button will subtract 1
    2. The 1 button will subtract 5
    3. The 2 button will add 5

      This will be simplified by copying your first chunk of code (The on button B pressed code) and making the few, small changes needed to get it working. To copy the chunk of code, just right-click on the pink on button pressed event and chose duplicate. Than make changes needed, starting with which button you are coding, to make it work properly. Good luck!

Leave a comment

Mintz Family iLab Project Blog

Things I've made and helped others make. Stories about creating and messing up.

Kris Swanson dot Blog

Things I've made and helped others make. Stories about creating and messing up.