Saturday 30 December 2023

Christmas Lights 2023 - Baby bouncer controlled lights

Back in 2020 I made some Christmas lights that could be controlled via Slack and WhatsApp so that Christmas spirit could cross the void created by Covid-19 and its lockdowns.

In subsequent years I added more features, such as morse code.

So now adding to my over-engineered Christmas lights has become something of its own tradition.

In early December this year, while setting up the decorations and mulling over what new silliness I was going to add to the lights, I was asked to fix my son's baby bouncer/cradle thing.

It's a Fisher Price Kick & Play Calming Vibrations bouncer, which has switches in the foot area which I believe are supposed to trigger lights and sounds.

This particular one was a hand-me-down which had been sat in storage, and the battery terminals are all corroded, making it non-functional.

Fixing it wouldn't really be a big deal, but I remembered that like most newborns, my son loves anything that's bright and shiny and blinky, like Christmas lights.


So obviously you can see where this is going...

The arch of the chair detaches, and you can see in one of the mounting points 4 pins.

A quick probe with the multimeter shows that these are two pins for each of the two 'pedal' switches.

To interface this with the existing light setup, I decided to again use the GPIO interface of the PCDuino.

Using the guide from before, I identified the GPIO identifier for two pins that I wanted to use, and enabled them as below. Pin 4 (232) is an input and pin 7 (233) is an output that is set to high simply provide a source, as I wanted to keep the main source pins (3v3 etc) free.

echo 232 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio232/direction
chown -R ant:ant /sys/class/gpio/gpio232
echo 233 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio233/direction
chown -R ant:ant /sys/class/gpio/gpio233/value
echo 1 > /sys/class/gpio/gpio233/value

The two pins were connected with a resistor to create a pull up effect. I had initially tried to have it as a pull down but it seemed to still have a lot of flicker in the output.

I picked the switch with the red foot as this tends to be more how Sam would sit. one side of the switch was connected to ground, and the other to pin 4.

The code

I added a thread to the main application which would read the value of the pin and change the lights accordingly.

Because of the way that /sys/class/gpio/gpio232/value is not actually a real file, it was not possible to hold the file open and continually read the input. Instead I had to create a loop which would constantly open the file and read the value. This does cause some overhead, but isn't too bad because the file is never more than one character long.

I had initially planned that when a change is detected, it would hook into the main code to change the lights, however I found this was causing a lot of deadlock issues. 

Instead, the new thread works very independently - it will pick one of the basic colors at random, and make a simple HTTP request to localhost to invoke the change.

The result

I'm not sure if he's aware of the causation between him moving and the lights changing, but like most babies there's twinkling, blinking and sparkly stuff, he's a fan, so he certainly seems to enjoy it.

No comments:

Post a Comment