MakeCode is Microsoft's ritzy drag-and-drop code editor. It makes it easy to get up and running with the Gemma M0.
Find the Gemma M0 code editor at maker.makecode.com
Here's my completed project. This code will light the center shamash pixel in red, then light the 8 individual candle pixels sequentially in gold each time the D0 capacitive touch pad is touched.
Click the Download link at the bottom, plug your Gemma M0 into your computer, click or double-click the reset button and drag the file onto the resulting GEMMABOOT drive.
Or, follow along below to create your own project from scratch.
Head to https://maker.makecode.com and select New Project. Choose the Gemma M0 board. Give your project a name. I called mine Hanukkah Sweater.
On Start Block
First we'll write the code that runs once, when the Gemma powers up. This is where we set up our variables and set up our pixel strip.
Click the LIGHT tab. Another tab titled MORE... will appear. Click on that one to find lots of options for attaching and controlling a NeoPixel strand soldered to the Gemma M0. Drag an instance of
set strip to create WS2812 strip on LED with 30 pixels out into your workspace and place it inside the
on start loop, which is usually there by default in a new project. (If it's not there, you can find it under the LOOPS tab). Change the pin to D1 instead of LED, and enter 9 pixels, one for each of our candles.
Note: NeoPixels are also called WS2812 pixels, and DotStars are also called APA102 -- make sure you've got the right ones selected.
Next, drag an instance of
strip set brightnessinto your
on start loop. Set the brightness to whatever you'd like. I want them as bright as possible, so I chose
Above the LIGHT tab there's another tab called PIXEL. Anything in here controls the onboard NeoPixel on the face of the Gemma M0. I want this turned off on my sweater -- no stray status lights for me, thank you -- so I dragged an instance of
set pixel color into my
on start loop and made it black. This will make sure the pixel stays off.
Go to the VARIABLES tab and create a new variable called
day. We'll use this to tell the Gemma which day of Hanukkah it is, so it knows how many candles to light.
From VARIABLES, drag an instance of
set day to 0 into your
on start loop. This will make sure we start with the first pixel, which is numbered 0.
Finally, let's make the middle shamash pixel red. From LIGHT > MORE... drag an instance of
set strip pixel color at 0 to red and change the pixel number to
4. (Yes, it's the 5th pixel in the strip, but remember we're counting from 0, not from 1).
Next we'll set up the main body of the code, in our FOREVER loop. Anything in this loop will run over and over, forever.
We can keep this part really simple.
Go back to the LIGHT > MORE... tab and drag an instance of
set strip pixel color at 0 to red into your
forever loop. Things in this loop will run forever, over and over. Change the 0 to your day variable, and change the red color to orange or gold or whatever you'd like your candles to be.
Now, all we need to do is set up our input. We want each pixel to light up in sequence whenever we touch our capacitive touch pad (D0).
You may have noticed that over on the left, a little NeoPixel emulator strip has appeared beneath your Gemma M0. This emulator will run whatever your code is doing. This is a really easy way to preview your code and see if it's working without having to download it every time.
Try clicking your D0 pad in the emulator a few times and see what happens.
It's working! Each candle turns sequentially gold when we click the pad.
However, it's not quite perfect. The shamash candle is at pixel 4, and this code overwrites the red color as it counts up. We can fix this by setting the pixel back to red each time the button is clicked.
Add an instance of
set strip pixel color at 4 to red into your
on touch loop. Try the emulator again and see what happens.
Closer! This code sets the pixel to orange but then sets it back to red again. This code accomplishes the goal.
We can go with this code, or we can get fancy and add one more step. We can use logic to get our
day variable to "skip" pixel 4, for a much smoother user experience.
Under the LOGIC tab, find an
if true then block and drag it into your
forever loop. Grab a
comparison block to replace the
true, then change the first
0 to our
day variable and the second
4. Then, add another
change day by 1 block inside the logic block.
Now, if the pixel number is 4, the variable day will change by 2 instead of changing by just 1.
Try this out on the emulator to see what happens.
This works great! We can stop here, or add one more fancy feature: we can code a reset at the end, so if we accidentally go too far (or just want to be able to play with the sweater indefinitely) the code will start over at the beginning.
Select your logic loop and copy/paste it into your workspace to get another instance.
Make it read
if day = 9 then
set day to 0. This resets our variable to the first candle.
Also drag (or copy/paste) another instance of
set strip all pixels and set it to
black. This will turn off all the pixels so we can light them again.
Try it out on your emulator. Once you're happy with the way it works, click the DOWNLOAD button to program your Gemma.
Plug your Gemma M0 into your computer using a USB cable. Click the tiny Reset button in the middle of the board and the onboard pixel will turn green. When the lights turn green, a new drive will appear on your computer called GEMMABOOT.
In MakeCode, click the Download button to download your code to a file. Drag this file onto the GEMMABOOT drive. It may also ask you if you'd like to pair the board to your computer. Feel free to follow the directions to do this (you may need to update the bootloader), or you can skip it and simply drag the downloaded file onto GEMMABOOT. Pairing the board makes it easier if you want to download and test again and again, since it will load the code automatically without you needing to drag the file each time.
If it's not working, here are a few things to check:
- Did you set your strip to pin D1 in the on start block? I seem to miss this step sometimes.
- Did you call the onboard pixel instead of the strip? These blocks are similar and easy to confuse. Make sure you're suing
set strip pixel colorand not
set pixel color
- If you just can't make it work, try starting with the completed code linked at the top of the page and working backwards
If your GEMMABOOT drive is not appearing, check out this guide for more troubleshooting tips.