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!

Circuit Playground Express Input / Output “Snacks”

These snacks are best attempted after you have completed the Hello World and Basic Input /  Output recipes. The snacks are designed to open up the many possibilities of the sensors built into the CPE. Instead of walking you through each one, I’ll just present a description and the code for you to try. Please mess with / tweak / and expand on the given code – that’s where the fun and the learning happen!

There are some new blocks here, blocks we didn’t cover in the first recipes in this series. Don’t worry, all you need to do is find which set of blocks they belong to (by matching the colors) and set them up the way they are in the picture. Before

Snack 1

This code will use the built-in microphone to react when it hears a “loud sound”.

Clap_Back_-_Microsoft_MakeCode.png

https://makecode.com/_WxJEJzYD1Rge

 

Snack 2

Buttons A and B will rotate and reverse the direction of light pattern on the built in neopixels.

rotate_lights_button_-_Adafruit_Circuit_Playground_Express.png

https://makecode.com/_VqrJJ8099d3z

 

Snack 3

We will use the built in accelerometer as a “tilt sensor” to control some of the Neopixels. We are going to use some nested if else blocks, so I’ll show you how to build it in steps.

  1. Nest 2 if else blocks together inside of the forever loop. Everything else will end up inside of them.
    Screen Shot 2018-01-25 at 10.19.16 AM.png
  2. From the Logic blocks add the inequality statement to the first if statement checking to see if the x accelerometer is greater than 100 (it ranges from 0 when the board is flat to 1023 when it is tilted 90 degrees to the ground.  Then add a show ring block, and light up the right lights to show it is tilting right.
    Screen Shot 2018-01-25 at 10.23.00 AM.png
  3. Finish up by setting the second if opposite of the first (inequality and number) and set the lights opposite as well. Add one more show ring and set all of the lights to neutral to show that the board is flat.
    Screen Shot 2018-01-25 at 10.23.43 AM.png
  4. Upload and you are done!

 

Snack Mix!

Create your own snack! Mix together the skills from all of the snacks to create your own interactive project. Add sounds, animations, and different combinations of sensors. Can you make it do something when the board drops? What about making the lights go on when it gets dark? Play and have fun!

Circuit Playground Express “Hello World”

“Hello World” refers to the first program you write on a platform or in a programming language you have never used before. This recipe is a Hello World using the Neopixel LEDs built into the Circuit Playground Express, with a few extra garnishes thrown in.

Ingredients

 

Directions

  1. Go to http://makecode.adafruit.com and start a new project.
  2. On the workspace, you should find a green forever block. Anything you place inside of it will repeat as long as the CPE is powered on.
  3. From the “Lights” set of blocks, select show animation and place it inside of the forever block. Select which of the six pre-set animations you want.
    Untitled_-_Adafruit_Circuit_Playground_Express.png
  4. On the left side of the screen there is a simulation of how your code will run on the real CPE. Check it out, and change the “ms” setting from 500 milliseconds to some other number to see what it does to the animation.
  5. Name the file. Look for the field in the bottom center with the floppy disk image next to it. You can call it “HelloWorld” or whatever else you like. After typing, click the floppy disk Save icon.
  6. Finally, the most technical part – sending your code into the actual CPE. The good news is that you will repeat this part each time you change or write new code so it will become rote and simple for you soon!
    1. Click the big Download button at the bottom left of the screen.
    2. Your code will download, probably into your “Downloads” folder. In Google Chrome (and other browsers) you can click the triangle next to the file you downloaded and choose “Show in Finder” to find it in your downloads folder.
    3. Plug the Micro USB cable into your CPE and into your computer.
    4. Click the reset button on the CPE. The lights should turn red then stay green, and the board will be in programming mode. Your computer will think it is a USB Drive named CPLAYBOOT.
    5. Drag the downloaded file (it will have whatever you named it in the file name) to the CPLAYBOOT disk. The file will transfer and then your program should start running on the CPE!

 

Garnishes

What else can you throw into the forever loop? Try some things from below, following step 6 each time to transfer your updated code to the CPE board for testing.

  • There are other options in the “Lights” blocks. You can change colors in the show ring block by clicking a new color from the center and then clicking an individual light to change its color. Alternating show ring blocks and pause blocks from the “Loops” set will let you create your own animations.
  • Check out the sounds and notes available in the “Music” set of blocks. Try adding one or more of the available sounds to your loop.

 

Circuit Playground Express Basic Input-Output

The real power of microcontrollers is in using them to give outputs to lights, speakers, motors, or pretty much any other electronic device based on inputs given to them from buttons, keyboards, sensors, etc…  In this recipe, we will experiment with basic inputs and outputs on the Circuit Playground Express (CPE) using only its onboard sensors, lights and speakers. Check out the Additional Input/Output Recipes for working with devices outside of the board.

Ingredients

 

Directions

  1. Go to http://makecode.adafruit.com and start a new project.
  2. We are going to assign each button to change the lights to a different color. Begin by dragging the on button A click block out of the “Inputs” group of blocks. Unlike the first recipe, you aren’t putting this inside of the forever loop, just out onto the workplace by itself. Whatever you put inside of it will be triggered whenever the A button on the board is pressed.
  3. From the “Lights” Group, drag out a show ring block and place it inside of the on button A click block. To make things simple, set all of the lights in the ring yellow. If you click the “A” Button on the simulator the lights on the board should all turn yellow.
    Untitled_-_Adafruit_Circuit_Playground_Express.png
  4. We want to do the same thing with button B, but have it switch the color of the lights. An easy way to do this is to duplicate the button A chunk of code and change it so the lights turn another color (blue in our example) when you press button B.
    ezgif.com-video-to-gif.gif
  5. Just for fun, repeat step 4, this time changing the drop-down menu to “Buttons A+B” and setting whichever color you like.
  6. Download your code and send it over to the CPE board. If you forget how you can review from Step 6 in the “Hello World” Recipe.

 

Garnishes

  • Try mixing some of the sounds in with the show ring blocks to change what happens when you press the button.
  • Play with the other “Input” blocks ( on shake, on loud sound, on light, and on switch moved) which work the same way as the on blocks you just used. Can you figure out how they work and make them do things as well?
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.