Coding external lights is a little different than programming the onboard NeoPixel LEDs. The LED Sequins don't change color, but you can make them flash on and off and even fade them in and out. To do that, you will be changing the voltage output from the pins you attached them to.
You can view, edit, and run the test code at the top of the page by going to this link, or click on the button below.
Then keep reading to find out how to put together your own external lights program in MakeCode!
To start, open up MakeCode, click on ADVANCED in the menu list, and the menu for PINS will appear.
When you click on PINS, you will see an assortment of blocks to choose from. Here's how to program a simple on/off action:
To simply turn an LED (or row of LEDs attached to the same finger) on and off, click on digital write pin [A0] to [LOW]
and drag it into the work space.
When you "write" to a pin, you are giving it instructions. Digital instructions have only two options, off and on. In this case, off is labeled as "low" and on is called "high."
Click on the pin number and select the pin you want to program -- in this case, A2, which controls the light on the pinky finger.
Next, click on LOW
to change it to HIGH
. This will make the LED turn on.
Now drag and drop a pause [100] ms
block into the stack.
Change the number of milliseconds (ms) to make the light stay on as long as you like. In this case, it is set to 500 ms, or half a second.
Finally, add another digital write pin
block to turn the light off.
For testing purposes, add any play sound block. Then download and test your code to make sure the LED Sequin lights up!
If the first pin is working, repeat the process with the other pins, adding pause
blocks to control when they turn on and off.
To set an external LED somewhere between "on" and "off" you can use an analog write pin
block.
In the old days, you could change the amount of voltage just by turning a knob. With digital technology, the same effect is usually created by pulsing the voltage on and off repeatedly at a certain rate of speed. This is called pulse width modulation, or PWM.
The analog write pin
block gives you settings between 0 (all the way off) and 1023 (all the way on). Use it the same way as the digital write pin
block, but move the slider for the setting you want. For example, to make an LED gradually get brighter, create a stack of blocks and move the slider up each time, with pauses in between. To make it get gradually dimmer, move the slider down each time.
Note that the analog write pin
block only lets you use pins A0, A1, and A2. The A0 is connected to the speaker, so don't use that if you want to add sound to your program. (It's probably best to avoid that pin anyway.)
In the example project here, the A1 pin was used for the touch control, so only the LED on pin A2 can be programmed to fade in and out.
Troubleshooting
Test your soft circuit before inserting it into the masking tape hand. If an LED is not lighting up, check that:
- you set the correct pin number on the
digital write
oranalog write
block. - the LED is making a good connection with the conductive tape. Try pressing on it while the code is running to see if that's the problem. You can add more tape if needed. Or try moving the LED to a different spot.
- there are no short circuits between pieces of tape (or loose threads coming off the tape). Also check that the tape on the CPX board is only touching the pin or pad it is supposed to touch, and nothing else!
Bonus Code!
If you're interested in seeing how to use more advanced programming concepts like functions, variables, and CONTROL blocks to make LEDs turn on and off and fade while the theme from The Addams Family plays, click on this link or the button below.
Text editor powered by tinymce.