Moving code around with branches
This is a handout for a workshop at the Milton Keynes Raspberry Jam on 10 November 2018. Code for the workshop is on Github.
In this session, you'll use a Raspberry Pi to program a Micro:bit, and the Micro:bit will control a NeoPixel strip and make it a one-dimensional firework.
Connecting the Micro:bit and Neopixel strip
- Connect the black lead on the Neopixel strip to the GND hole on the Microbit.
- Connect the red lead on the Neopixel to the 3V hole on the Microbit.
- Connect the white lead on the Neopixel to the 0 hole on the Microbit.
- Use the USB cable to connect the Microbit to the Raspberry Pi
Starting the Mu editor
Find the "Start" menu → Programming → mu
Once you've opened Mu, press the Mode button and press "BBC micro:bit"
Program 1: launching the firework
Type this program into the Mu editor. It will do the animation for a firework launching.
Note that Python is really picky about
- spacing and indentation
- upper and lower case letters
- round and square brackets
- colons at the end of some lines
- the difference between zero and oh, and one and ell.
When you've typed it in, press "Save" button to save your file then press the "Flash" button to put the program on the Microbit.
When you press the A button on the Microbit, you should see a little light shoot along the strip.
Program 2: making the launch motion-sensitive
Pressing the button is OK, but let's make the firework launch if you shake the Microbit.
Make the changes indicated to your program. You don't need to type the '# Add this line' comments: that's just to show you what to do.
Again, save and flash the program. Now try shaking the Microbit and see if it launches a firework.
Program 3: Exploding fireworks
Now to make the firework explode at the top!
Again, save and flash your program. You should now have explosions!
What you've got is OK, but let's add some animation to the explosion. Let's make it start small and rapidly grow, and then fade over a bit of time.
Again, save and flash the program. Cool animation!
Program 5: different colour explosions
Always having the same colour explosion is a bit boring. Let's make every explosion a different colour.
Again, save and flash the program. Even cooler animation!
Program 6: speeding up the animation
The animation's a bit slow. We can speed things up by only updating the pixels that change, rather than all of them.
Program 7: Microbit display
The Microbit has a small display. Let's use that to show the firework is ready.
Program 8: Radio control
The Micro:bit has a radio. Let's use that to make one firework (sometimes) set off another firework.