Maureen ThomasMaxwell Krolak
Published © GPL3+

Interactive Children's Jack-In-The-Box

Transform your mediocre Jack-In-The-Box into a fun mechatronics project with an Arduino, Rotary Encoder, Servo Motor, and more!

IntermediateFull instructions provided12 hours2,641
Interactive Children's Jack-In-The-Box

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
rotary encoder
×1
SparkFun Triple Axis Accelerometer Breakout - ADXL335
SparkFun Triple Axis Accelerometer Breakout - ADXL335
×1
Slide Pot - Medium (10k Linear Taper)
×1
Pushbutton 33mm - Green
×1
Metal Pushbutton - Latching (16mm, Green)
×1
Jumper wires (generic)
Jumper wires (generic)
×30
Breadboard (generic)
Breadboard (generic)
×1
Buzzer
Buzzer
×1
Servos (Tower Pro MG996R)
×1
LED (generic)
LED (generic)
×2
Resistor 221 ohm
Resistor 221 ohm
×1
9V battery (generic)
9V battery (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

DeWalt Drill
Dremel Tool
Metal Cutting Clippers
Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Jack in the Box Schematic

A fritzing representation of the electronics we made to use as a reference when plugging in all of the wires.

Code

kidsToy.ino

Arduino
We used it as a way to retrieving information from the sensors and use the information to make the box more intractable.
//Library for accelerometer
//https://github.com/sparkfun/SparkFun_MMA8452Q_Arduino_Library/blob/master/src/SparkFun_MMA8452Q.h
#include <SparkFun_MMA8452Q.h>
#include <Servo.h>


//Variables for notes. The values of the ints represent the amount of Hz
//All definitions referenced from https://pages.mtu.edu/~suits/notefreqs.html
int b4 = 493;
int f5 = 698;
int c1 = 138;
int c4= 261;
int f4 = 394;
int g4 = 392;
int a5 = 440;
int c5 = 523;
int d5= 587;
int e5 = 659;
int g5 = 783;


//Song "Pop Goes The Weazel" performed by a buzzer
int song[] = {c5,c5,d5,d5,e5,g5,e5,c5,g5,c5,c5,d5,d5,e5,c5,c5,c5,d5,d5,e5,g5,e5,c5,a5,d5,d5,f5,e5,c5};


Servo myServo;
MMA8452Q sensor;

//Temp values for the force experinced by the accelerometer
float tempX;
float tempY;
float tempZ;

//
int tempPos;
int encoderPos;

//Pins for rotary encoder
int pinA = 5;
int pinB = 4;

//Variable representing the position in the song
int i = 0;

void setup(){
  
  Serial.begin(9600); 

  //Initilizing pins
  pinMode(pinA,INPUT);
  pinMode(pinB,INPUT);
  pinMode(3,OUTPUT);

  //Initilizing Servo to default position
  myServo.attach(3);
  myServo.write(30);

  //Initilizing the sensor and the temp values for the force
  sensor.init();
  tempX = sensor.cx;  
  tempY = sensor.cy;
  tempZ = sensor.cz;

  //Setting value for tempPos to be the starting position of the encoder
  tempPos = digitalRead(pinA);
  
}

void loop(){ 
  
  int dir = digitalRead(pinA);

  //Testing if the encoder is turing the correct way
  if(tempPos != dir&&digitalRead(pinB)!=dir){
    //Stoping the buzzer if the not is 0 and continuing if not
    if(song[i]==0){
      noTone(A1);
    }else{
      tone(A1,song[i]);
    }

    //Delaying to keep tempo
    delay(500);
    //Looping through all values of the array
    i=(i+1)%(sizeof(song)/sizeof(song[1]));
    
  }else if(sensor.available()){
    sensor.read();
    //Reading sensor values and comparing them to the prevous values
    //If the change is force is greater than 1g of force continue
    if(abs(sensor.cx - tempX) > 1 || abs(sensor.cz - tempZ) > 1 || abs(sensor.cy - tempY) > 1){
      
      //Choosing a random note from the song and playing it
      tone(A1,song[(int)random(0,(sizeof(song)/sizeof(song[1])))]);
      delay(250);

      //Setting current sensor readings to temp values
      tempX = sensor.cx;  
      tempY = sensor.cy;
      tempZ = sensor.cz;
    }
  }

  
  //If the song is over move the servo to open the door
  if(i == (sizeof(song)/sizeof(song[1]) -1 )){
    myServo.write(70);
    
  }else{

    myServo.write(30);
  }

   tempPos = dir;
   noTone(A1);
  
}

Credits

Maureen Thomas

Maureen Thomas

3 projects • 4 followers
Maxwell Krolak

Maxwell Krolak

3 projects • 6 followers
Lane Tech Junior
Thanks to Adam Burdett.

Comments