Friday, October 31, 2025

The Candyinator

Background

Every year since my kids were born, we have picked a theme for Halloween and found or made costumes for the entire family.  We have done Peter Pan, Toy Story, Star Wars, Pac-Man, and Little House on the Prairie among others.  When we decided to participate in our church's Trunk or Treat, I decided I wanted to come up with a creative way to distribute candy.

After toying around with a few ideas, a lightbulb went off.  I decided to build a candy machine based on one of Dr. Heinz Doofenshmirtz' inventions.  We dressed up as characters from Phineas and Ferb and I built...The Candyinator! (Said in my best Doofenschmirtz voice)

This project combined two of my hobbies: woodworking and coding.  The idea I came up with was something that would look similar to an old arcade game but would distribute candy from the bottom.


Overview

My concept was to have some flashing buttons of different colors.  A child would press any of the buttons and a short animation would play.  Then it would distribute the candy to them.  All of this would be controlled by a Raspberry Pi.



Technical Details

Python Web Socket

One of the first pieces of code I worked on was the web service for communication between the web page and the user interface.  This would notify the frontend when a button was pressed and the backend when the animation was done and to dispense the candy.


Surprisingly, I had a hard time finding a good Python implementation of a web socket server, so I ended up writing my own.

Web Page

For the animation, I took some images of various candy brands and wrote a JavaScript function to randomly place them on the screen.  So when a button was pressed, it looks something like this:



Sound

I ended up using 4 colored buttons: red, blue, green, and yellow.  For each button, I assigned a classic arcade sound:

Red - Super Mario Bros

Blue - Sonic 

Green - Frogger

Yellow - Pac-Man






Kiosk Mode

Another challenge was putting this whole thing into "kiosk mode".  The web page would need to load up automatically.  It would need to hide the mouse cursor.  And because I wanted to use sound, it had to be able to play the sounds without required user gesture (since autoplay has been disabled for a while now).

To do this, I ended up with a line in my Raspberry Pi config that looked like this:

@chromium-browser --start-fullscreen --start-maximized --kiosk --noerrdialogs \
--disable-infobars --autoplay-policy=no-user-gesture-required \
http://localhost/candyinator/

To hide the mouse, I used the unclutter app that was called at boot time.

Candy Dispenser

It took me a little while to figure out the best way to dispense the candy.  After watching a couple of YouTube videos for inspiration, I came up with an idea.  I made a circular bay for the candy divided into four sections.  Beneath it was a stepper motor to control it.  When it was time to drop the candy, the Python controller would send a command to the stepper motor to turn 90°.



Final Thoughts

This was such a fun project to work on.  It gave me a chance to dust off my unused electrical engineering degree and produce an integrated product from inception to completion.  There were many prototypes and challenges along the way, but the end product came out pretty good.  Are there things I would do differently?  Absolutely.  But seeing the enthusiasm of the kids play with it all night was worth it.

If you'd like to check out the source code, it's available on github: https://github.com/clarmond/the-candyinator




0 comments:

Post a Comment

 
Blogger Templates