Introduction: How to Make a Runaway Cookie Box

Eat too many cookies? These are some guidelines for how you can add a few security features to an ordinary box of cookies - or at least have a fun toy.

The runaway cookie box will roll away when objects approach it and sound an alarm when it is picked up.

What you need for the craft side of things:
-empty cookie box
-wheels (4)
-Legos (axles, gears, basic)
-hot glue
-electrical tape
-popsicle sticks, colorful straws, googly eyes, or whatever you want to give your box some character

What you need for the computing side of things:
-hacked servo
-tilt sensor
-distance sensor
-resistors (2x100K&, 1x100&) 
-wire strippers
-soldering iron

[This project was done for Olin College's Computing & Craft class, taught by Dr. Amon Millner.]

Step 1: The Circuitry

During this step, you'll set up all the circuitry required for the project.

First, connect the Arduino's 5V power and ground pins to the bus strips of the breadboard.

Second, connect the tilt sensor to the breadboard. Connect the wires on one side to the power strip and the wires on the other side to a 100K& resistor going to ground as well as to digital ins on the Arduino (in this case I used 3 and 4).

Third, connect the three distance sensor wires: red to power, black to ground, and yellow to an analog in on the Arduino (in this case, I used A0).

Fourth, connect one of the speaker's wires to ground and the other to a 100& resistor connected to a PWM digital in on the Arduino (in this case, I used 5).

Last, connect one of the hacked servo's wires to ground and the other to another digital in (in this case I used 9).

Step 2: The Box

During this step, you'll construct your moving cookie box monster that will hold all the circuitry from the last step.

For ease in assembling the project, you can cut a rectangular hole on the bottom of each side.

Hot glue the sides of the breadboard to the space just above this hole.

Hot glue the head of your hacked servo directly to a Lego wheel.

Now construct a Lego chassis for the box with a regular front axle and only one wheel connected in the back. One of the rear wheels is, as you may have noticed, currently attached to your servo. Tape down your Arduino and your servo so that the fourth wheel is oriented in the right direction.

Now you can hot glue your distance sensor to the top back of the box so that your box will spy its enemies coming from behind. You can let the speaker simply rest on top of the breadboard.

Last, attach whatever crafty materials you want! As you can see with my project, I gave it popsicle antenna eyes with a colorful straw mohawk.

Step 3: The Code

The last piece of your project is programming the Arduino so that your cookie box behaves the way you want it to.

First, initialize and define all your variables. Here's the code I used:

const int tiltPin1 = 3;
const int tiltPin2 = 4;
const int speakerPin = 5;
const int distancePin = A0;
const int motorPin = 9;

int tiltState1 = 0;
int tiltState2 = 0;

void setup() {
pinMode(speakerPin, OUTPUT);
pinMode(motorPin, OUTPUT);
pinMode(tiltPin1, INPUT);
pinMode(tiltPin2, INPUT);
pinMode(distancePin, INPUT);

Next, define the conditions and all behaviors. Here's how I did it:

void loop(){
  tiltState1 = digitalRead(tiltPin1);
  tiltState2 = digitalRead(tiltPin2);
  int val = analogRead(distancePin);
  if (val > 450) { // if an object is "close"
    digitalWrite(motorPin, HIGH); // turn motor on
  else {
    digitalWrite(motorPin, LOW);}
  if (tiltState1 == HIGH) { // if tilted (case 1)
    tone(speakerPin, 131, 125); // sound alarm
    delay(125); } 
  else {
    noTone(speakerPin); }
  if (tiltState2 == HIGH) { // if tilted (case 2)
tone(speakerPin, 131, 125); // sound alarm
delay(125); }
else {
noTone(speakerPin); }

Step 4: Enjoy!

Once you've plugged your Arduino in and uploaded the code, your cookie box should be good to go!

Some possible directions you may want to take this project in:

-Amplify the signal to the servo in the circuit and use a gear system to move the box faster
-Add a second servo and implement the ability to change direction of the box
-Modify the behavior of the box so that it could be a friendly cookie pet. When it sees an object in the distance, it approaches it - and plays nice music when its owner is kind enough to pick it up!

Here's a video of my cookie box in action!