It's winter time again and the days are shortening. Soon enough, my bicycle commute will involve some night time riding. It's time to boost the lights on the bicycle by adding a 60 LED NeoPixel strip controlled by a BBC micro:bit powered by a dual USB plug battery pack. The code is written in JavaScript in https://pxt.microbit.org.
Installing the NeoPixel stripThe strip is attached under the frame using simple cable ties. The power cables have been soldered to a USB cable to easily plug it into the battery pack.
The battery sits under the seat secured with cable ties. The NeoPixel strip and micro:bit are both connected via separate USB plags. The control wire of the strip is connected to the P0 pin on the micro:bit.
The picture below shows the basic setup before being weatherized with a freezer bag.
To test the wiring, I simply display a rainbow on the Neopixel strip.
- Create a new project in https://pxt.microbit.org
- Add Neopixel through
More
->Add Package
->NeoPixel
- Copy this code in the JavaScript
const strip = neopixel.create(DigitalPin.P0, 60, NeoPixelMode.RGB)
strip.showRainbow()
The results look good. It's time to beef up the lighting!
The core animation is based rotating colors in 3 ranges of LEDs:
- back LEDs: a rotating red LED
- mid LEDs: a rotating rainbow strip
- head LEDs: a rotating white LED
const strip = neopixel.create(DigitalPin.P0, 60, NeoPixelMode.RGB)
// split into 3 sub-ranges
const back = strip.range(0, 10)
const mid = strip.range(10, 40)
const head = strip.range(50, 10)
// back: 1 red led
back.setPixelColor(0, NeoPixelColors.Red)
// mid: moving rainbow strip
mid.range(0, 20).showRainbow()
// head: 1 white led
head.setPixelColor(0, NeoPixelColors.White)
// loop
basic.forever(() => {
// rotate each range
back.rotate()
mid.rotate()
head.rotate()
// don't forget to show!
strip.show()
})
On/off with the micro:bit buttonsThe battery pack does not have an off button so it is quite handy to be able to shut down all the NeoPixel LEDs and the animation loop. Good news is that we have 2 buttons on the micro:bit to handle on/off!
input.onButtonPressed(Button.A, () => {
if(!running)
run() // launch rendering loop once
})
input.onButtonPressed(Button.B, () => {
running = false
// stopping handled in rendering loop
})
Comments