I used Python all the time but I’m normally a bit skeptical about microPython for similar reasons as I am skeptical about Arduino – both of them seem too much like doing hardware from a software approach. Nonetheless it seemed like a reasonable tool for the job in this case.
For my new room I invested in a pair of nice wifi speakers and I want visuals to match so I decided to build an LED controller. Of course, I can buy off the shelf LED controllers, but I want to program my own light patterns and also it’s more interesting to build my own.
I still haven’t got round to shipping all of my equipment from the UK – until this week I didn’t have a multi-meter and I still don’t have my oscilloscope, so I’ll put off talking about hardware for a bit.
I decided to use a BBC microBit which is most easily programmed in microPython. It seems suitable for the job because my lighting is something that I might want to tinker with from time to time, and Python (and so presumably microPython) is quick and easy to iterate. I could build my own microcontroller circuit and program it in C, but really what new would I learn?
The software for this project is pretty simple. It is divided into two parts, the microBit on my wall which I call the “switch” and the microBit connected to the LEDs which I call the “driver”. The two talk via radio to allow me to turn on and off the lights and also to change between light patterns (which I call “channels”).
The switch’s job is keep track of which channel I select using the buttons and transmit that to the driver. The drivers job is to receive the channel and display the appropriate pattern on the LEDs.
My first attempt went like this:
Switch: ButtonPress -> Decrement/increment channel -> Transmit new channel
Driver: Receive new channel -> Update Channel
But I found that often the driver may miss a command resulting in no reaction which was a bad experience.
My solution was instead to have the switch keep track of the desired channel and also which channel the driver was on. This way, when the desired channel did not match the driver channel, the switch would repeatedly transmit the desired channel until it received an update from the driver. The driver would only send an update when it received a command from the switch.
This way, the radio on the switch only needs to be active when a channel change is initiated, and if an update gets lost the switch will keep transmitting and the driver will keep responding until everything is up to date. This results in a pleasing responsiveness.