I Want Candy!
This instructable was created in fulfillment of the project requirement of the Makecourse at the University of South Florida (www.makecourse.com)
I Want Candy! is a candy dispensing game in which the player must replicate a sequence of 5 or 10 flashing LEDs using 5 pushbuttons. If the player is successful, they advance to the next level. If they fail, the game ends and candy is dispensed based on the highest level they completed. The higher the level, the more candy they get. The game dispenses two types of candy. Levels 0-10 win Candy #1 and 11-20 win candy #2. The idea is that candy #2 is better. Maybe candy #1 is kinda gross - its up to you!
For levels 0-10, the player has to replicate 5 LEDs and for levels 11-20 it's 10 LEDs. Sounds easy, right? Well, as the levels progress, the LEDs flash faster and faster increasing the difficulty! Ready to give it a try? Then read on!
Supplies
1 Arduino MEGA 2560
2 28BYJ-48 5V Stepper Motors
2 ULN2003 Stepper Motor Drivers
5 Pushbuttons
5 LEDs
1 5V Active Buzzer
1 5V regulated USB power supply
1 Rocker Switch
1 Short USB Cable for Arduino
1 2.1mmx5.5mm Female Jack
5 230 ohm Resistors
1 Proto Board
Wires
Various small length M2 and M3 screws
PLA Filament
Circuit Diagram
The Circuit Diagram shows how the project components connect to the Arduino and at what pins. Be careful to make sure that the buttons are wired to 5 of the MEGA's interrupt pins since these buttons use interrupts in the sketch. The buttons are wired to digital pins 2,3,19,20, and 21. The LEDs are wired to digital pins 37,39,41,43, and 45. The controller for motor #1 is wired to digital pins 5-8 and the controller for motor #2 is wired to pins 10-13. The 5V buzzer is wired to digital pin 48. See the diagram for the 5V and ground pins. The LEDs and pushbuttons share a ground. Be sure to wire carefully.
The Arduino lights up the LEDs by sending a high signal through the pins. The pushbuttons are set normally high with an internal pullup resistor. When the buttons are pressed, they make a circuit to ground and register low which triggers the ISPs in the sketch. The ULN 2003's each have a dedicated 5V and ground pin and 4 inputs each. They control the steps of the motors to get them to spin and dispense the candy. The 5V buzzer is controlled by varying the pin output frequency to create different tones.
3D Printed Components
The game is built using 10 3D printed components to house the electronics and and the candy augers. The parts are listed in the diagram. All components were designed using Autodesk Inventor and printed on a Creality Ender 3. The base enclosure is from a standard kit but can be 3D printed if needed. The inner component mount was designed to hold the two motor controllers, the power supply, and the Arduino MEGA. The MEGA was mounted vertically on its side to fit. The base lid serves as a mount for the main housing and has ports for the on/off switch, charging jack, and wiring harnesses. The main housing has two separate hoppers which fit two augers for pushing the candy out. The augers are rotated by two stepper motors mounted toward the back of the housing. The auger shafts terminate at the eye disks which extend through the housing wall. The disks are secured to the end of the augers with screws. This keeps the augers straight and adds spinning eyes to the design. On top of the main housing is the PCB housing which holds the soldered protoboard with the pushbuttons and LEDs. The top lid has holes for the pushbuttons and LEDs for a clean look.
Arduino Sketch
These pictures show the fully commented Arduino sketch for the game. At the end the interrupt service routines (ISR) for the pushbuttons and stepper motors are shown. The code uses flag variables to track the game status like level and tier. The main loop operates by generating a random sequence for the LEDs and then lighting them up. Then, the pushbuttons increase a counter until the right number of presses has occurred. Then a for loop with a nested if statement evaluates the button presses by comparing them the saved led sequence. If statements determine the outcome for the level and the candy dispensing. The led_on_delay and led_off_delay arrays determine the game difficulty and the motor_run_time array contains the values controlling the length of candy dispensing. Please read through the code comments for more detail.
Assembly
The printed parts and electronics are assembled as shown in the photos. The Arduino, Power supply, and ULN2003 stepper motor controllers are mounted on the inner component mount which is screwed inside the base enclosure. The stepper motors and augers are mounted within the main housing. The separate hoppers are filled with two types of candy in the photo. The pushbuttons and LEDs are mounted on a protoboard using solder. The protoboard is screwed inside the PCB housing and the lid is placed on top. The power supply is connected to the charging port using a male pigtail and the Arduino is powered via a USB cord plugged into the power supply. The USB cord 5V wire is spliced and connected to the on/off switch terminals. With the sketch loaded ton the Arduino and the soldering, wiring, and assembly done, the game is ready to be played! Watch the video for a demonstration!