Now that you have your LED strand hooked up to your Gemma M0, it's easy to control the lights using Microsoft MakeCode! The version to use with Gemma M0 is called MakeCode Maker. It lets you tell Gemma how to use its pins, where to look for input, and where to send output.
If you've never used MakeCode, check out the getting started guide here. The MakeCode Maker version work the same as the regular version.
In the example program shown here, one touchpad makes the onboard light turn on and off. This is a good way to test that the code is running. Since it's an RGB DotStar LED, you can make it any color you want, or even create a pattern of different colors!
The other touchpad controls all the lights in the LED Strand, in unison. In this program, they turn on, then get dimmer and dimmer until they turn off. Again, you can create any pattern you wish.
The onscreen simulator lets you see what your code will do before you download it to the Gemma board. For this project, the strand of LED lights is represented by an upside-down green LED on the breadboard.
To check out the sample program shown here, click the button below to open MakeCode Maker. Then click Edit to test it out and add to or change the programming as you wish. An explanation of the code is below.
To download a MakeCode file to the Gemma M0:
- Connect the board to your computer with the USB cable.
- Press the reset button. You should see a green light and a red light on the board. This means it is in bootloader mode, and it's ready to receive code.
Downloadon the MakeCode page and save the file. It will have a .uf2 extension.
- Look for a new drive on your computer labeled GEMMABOOT. Drag and drop (or copy and paste) the program into the drive.
If you are downloading the sample "Gemma Strand 1" program, you'll know the code was downloaded when the onboard LEDs turn off.
Try touching the pad labeled D0. The onboard DotStar LED should turn purple. When you take your finger off, the light should turn off.
Then try touching the pad labeled D1. The strand of LED lights that you attached to the board should flick on, then get dimmer and dimmer until they go off again.
Once you understand what those chunks of code are doing, you can play around with them to see what other effects you can create!
on start block is found in the green Loops menu. Here, it is used to make sure the onboard LED and the strand are both off at the start.
Blocks from the blue PIXEL menu control the onboard LED. They give you different ways to adjust the color of the light. When you select black on the
set pixel color block, it turns the light off.
analog write pin block lets you control how much power is sent to the pin number you specify -- in this case, Pin A1. In this example, it is used like a dimmer switch to make the strand of lights dimmer or brighter, as well as turning them on or off.
Coding the Onboard LED
on touch blocks are found in the INPUT menu.
Selecting "down" means the touch pad indicated is registering a touch. "Up" means it is not being touched.
Here, they are used to make the D0 pad into an on/off switch that controls the onboard RGB light on and off.
So, when you touch pin D0, the light turns purple. Let go, and the light turns off!
Controlling the LED Strand
There are no premade blocks to control the LED strand, but you can use the red
PINS blocks to tell Pin A1 what to do. To find the PINS menu, you must first click on ADVANCED at the bottom of the column of menus. That reveals additional menus.
analog write pin block tells the pin you want to output a signal. ("Read" blocks tell the pin to look for a signal input.) Because it's analog, you can set it to different levels. (Digital controls essentially register only on or off. On the Gemma, some pins can be used for either analog (A) or digital (D), which is why they have both labels.)
To choose which pin you want to control with the block, click on the down arrow to open the drop-down menu.
To set the amount, type a number into the white oval, or use the slider that opens when you click on the oval. Setting it to zero turns the lights off.
By default, the
analog write pin block is set to the maximum, which is 1023.
In this case, the settings go down in four steps, from 500 to 100 to 50 to 0. This makes the light get gradually dimmer.
To control how long the light stays at each level of brightness, there are
pause blocks from the LOOPS menu inserted between each piece of code.
Again, you can type in a number, or choose from the drop-down menu. All times are in milliseconds (ms). One second is 1000 ms, so 500 ms is half a second.