Candy Locker

Introduction: Candy Locker

Someone stealing your candy? Keep it safe from greedy hands with this color recognition lock and dispenser. Using a set of 5 distinct color images, you’ll be able to set up a pattern that will activate and dispense candy. In this tutorial, you will learn how to use the Intel® Edison and a webcam for color recognition. After assembling the electronics and housing, you’ll train the color recognition software using the image cards.

Step 3: Preparing the Intel® Edison

This example requires you to have a webcam recognized by Intel® Edison. Fortunately, with the latest system installed (ww05-15), most webcams should be recognized by default.

Other than that, we need to install a few python libraries: OpenCV, Numpy, and updated mraa.

Get the Extra Repositories:

The python libraries needed are not in the standard repository so you must add a few new repositories. The following commands will add the new repositories, and update the existing ones. After that you’ll be able to install the necessary libraries with ease.

SSH to your Intel® Edison and type the following command in the terminal:

Step 4: Install Python Libraries

Numpy is a python library for working with data. To install it, type the following command in the terminal:

opkg install python-numpy

The newest version of system actually has OpenCV installed, but only with C++. Here we need a Python binding to get it working. To do this, type the following command in the terminal:

opkg install python-opencv

Step 5: Move Script to Intel® Edison

On your computer, download “CandyLock.zip” from the attachment of this tutorial and unzip it. Go into the parent folder of the unzipped folder. Copy it over by typing the following command in the terminal:

scp -r ./CandyLock root@your-edison-ip:/home/root/

Adjust focus for the camera

Unfortunately the webcam doesn’t handle auto-focus very well when it comes to OpenCV. Based on this tutorial and we developed our own method of calibration.

Open the camera casing and place it within the construction. After following the instructions, this is already done. It may be a bit inconvenient to reopen some panels and work in a confined space. We do the calibration within the construction so the focal length is fixed.

Run the script TestCamera.py by typing the following into the terminal:

python /home/root/CandyLock/TestCamera.py

The script will take a picture and save it to a local file whenever you hit enter. Take one picture by hitting key once.

Copy the picture over to the current directory by typing the following into the terminal:

scp root@your-edison-ip:/home/root/CandyLock/camTest.jpg ./

If the image is not sharp, adjust the webcam lens manually by twisting the lens a bit to change its focal length. Repeat this process until the image is clear and sharp.

Step 6: Run the Candy Locker

First, you need to save a 4-token sequence for the locker. To start the training sequence, type the following command in the terminal:

python /home/root/CandyLock/combiLock_train.py

Decide on your sequence and then place the first token inside the machine. You’ll see the green LED blink a few times. Shortly after, the picture is taken. Now take the token out, and place the next one in. Repeat until all 4 tokens are trained. The Python script will end when the sequence is recorded.

Next, it’s time to get your candies out. To start the dispensing sequence, type the following command in the terminal:

python /home/root/CandyLock/combiLock.py

Place the first token of the sequence inside the machine. Wait until the green LED finishes blinking and then place the next one. Do this until all 4 tokens are registered.

If the sequence is correct,, the green LED will blink quickly a few times and a candy will drop out. If the sequence is incorrect, the red LED will turn on for a few seconds and nothing is dispensed. Wait until the red LED turns off before making another try.

Step 9: Assemble the Candy Locker Pt1

Cut about 40 cm of enamel wire and cover about 5 cm of the ends with solder

Solder the wires to the pad of the SMD LEDs. Clip off any excess

Test connections with a coin battery

Cover with hot glue to secure SMD LEDs to plexiglas ring

Note: Enamel wire has a thin plastic coating which needs to be removed. To do this, get a scrap piece of MDF and place the end of the wire on it. Get a bead of solder and press the iron on the enamel wire, about 5 cm from the end. Slowly pull the wire, drawing it through the solder bead

This coats only the top surface of the wire, so flip it over and repeat

Bend and glue dispenser plate.

Heat Part B3 along dashed line and bend slightly

Glue Parts B2 and B4 to B3

Cut a piece of plastic and place some copper tape on it

Insert through the slit and tape it on the underside

Assemble dispenser servo

Use contact glue or spray adhesive to attach Part D1 to D2

Attach to servo arm and secure with screws

Insert into Part A5

Secure with Part A12

Step 10: Assemble the Candy Locker Pt2

Disassemble the body of webcam

Attach to Part A11, with the engraved recesses facing the camera

Hot glue the LED ring to the underside of Part A11

Assemble main body frame

Attach Part A4, A5, A6, and B3 to Part A10. Put the servo cable through hole on Part B3

Attach Part A1 to assembly

Attach Part A7 to assembly

Secure wires along notched edge

Attach electronics and make token switch

Mount the Intel® Edison onto Part A3

Attach the prototype shield

Insert wire into Part B3 and tape down with copper tape

Attach switch wires to the nodes using copper tape

Plug in the webcam, microUSB, and power cables

Push the LEDs through the holes on Part A2

Step 11: Assemble the Candy Locker Pt3

Close bottom, back, and front

Attach Parts A3 to the bottom, A8 to the back, and Part A9 to the front