Project tutorial
Knock Knock Music Box

Knock Knock Music Box © GPL3+

This simple music box starts playing when you knock on it.

  • 1,898 views
  • 1 comment
  • 10 respects

Components and supplies

Apps and online services

About this project

How to Make

A great thing about a simple piezo element is that it can both produce and register sound. On this project I have combined the two: the Arduino Pro Mini sleeps until the piezo registers a knock. When it registers another one, the piezo will play a song.

The project is really simple to make:

  • 3 AAA batteries in series connected to VCC and GND.
  • The piezo is connected to pin 2 and GND.
  • One 1M Ohm resistor is connected between the same pin 2 and GND.

Hardware

To make it run longer on three batteries it helps to make some modifications to your Arduino Pro Mini. If you remove the LED and the regulator it will use an insanely tiny amount of current during sleep (in this case 0.006 mA!). See this great page for instructions: http://www.home-automation-community.com/arduino-low-power-how-to-run-atmega328p-for-a-year-on-coin-cell-battery/

Although these modifications are not necessary, an unmodified Arduino Pro Mini runs for about 35 days on these 800 Mah batteries (asleep); a modified one will run for years.

Software

To actually let it sleep, you will need to install a library: https://github.com/rocketscream/Low-Power

You can see the code below. You can change the knock interval or the melody. It's a bit of a pain to program the melody though. Probably the easiest way is to write a melody on a keyboard and then write down the notes and duration. A few things to keep in mind:

  • The tempo: the lower the number, the faster the melody.
  • Change the variable melody length if you want to use more or fewer notes in your melody.
  • The note durations correspond to the notes in the melody.

Code

Knock Knock musicboxC/C++
/*************************************************
   NOTE FREQUENCYS
   *************************************************/
#define B0  31
#define C1  33
#define CS1 35
#define D1  37
#define DS1 39
#define E1  41
#define F1  44
#define FS1 46
#define G1  49
#define GS1 52
#define A1  55
#define AS1 58
#define B1  62
#define C2  65
#define CS2 69
#define D2  73
#define DS2 78
#define E2  82
#define F2  87
#define FS2 93
#define G2  98
#define GS2 104
#define A2  110
#define AS2 117
#define B2  123
#define C3  131
#define CS3 139
#define D3  147
#define DS3 156
#define E3  165
#define F3  175
#define FS3 185
#define G3  196
#define GS3 208
#define A3  220
#define AS3 233
#define B3  247
#define C4  262
#define CS4 277
#define D4  294
#define DS4 311
#define E4  330
#define F4  349
#define FS4 370
#define G4  392
#define GS4 415
#define A4  440
#define AS4 466
#define B4  494
#define C5  523
#define CS5 554
#define D5  587
#define DS5 622
#define E5  659
#define F5  698
#define FS5 740
#define G5  784
#define GS5 831
#define A5  880
#define AS5 932
#define B5  988
#define C6  1047
#define CS6 1109
#define D6  1175
#define DS6 1245
#define E6  1319
#define F6  1397
#define FS6 1480
#define G6  1568
#define GS6 1661
#define A6  1760
#define AS6 1865
#define B6  1976
#define C7  2093
#define CS7 2217
#define D7  2349
#define DS7 2489
#define E7  2637
#define F7  2794
#define FS7 2960
#define G7  3136
#define GS7 3322
#define A7  3520
#define AS7 3729
#define B7  3951
#define C8  4186
#define CS8 4435
#define D8  4699
#define DS8 4978
#define Silence 0

#include <LowPower.h>


int val; // variable for analogread piezo
int knockPin = 2; //interrupt on pin 2 or 3
volatile int interruptSwitch = 0;

unsigned long countInterval = 1200; //max time between knocks
unsigned long startTime;
unsigned long endTime;


//melody
int tempo = 330;
int melodyLength = 145;

int melody[] = {
  C4, E4, G4, E4, G4, E4, C4, E4, G4, E4, G4, E4, B3, E4, G4, E4, G4, E4, B3, E4, G4, E4, G4, E4,
  A3, E4, G4, E4, G4, E4, A3, E4, G4, E4, G4, E4, B3, E4, G4, E4, G4, E4, B3, E4, G4, E4, G4, E4,
  C5, E4, G4, E4, G4, E4, C5, E4, G4, E4, G4, E4, B4, E4, G4, E4, G4, E4, B4, E4, G4, E4, G4, E4,
  A4, E4, G4, E4, G4, E4, A4, E4, G4, E4, G4, E4, B4, E4, G4, E4, G4, E4, B4, E4, G4, E4, G4, E4,
  C5, E5, G5, E4, G5, E5, C5, E5, G5, E4, G5, E5, B4, E5, G5, E4, G5, E5, B4, E5, G5, E4, G5, E5,
  A4, E5, G5, E4, G5, E5, A4, E5, G5, E4, G5, E5, B4, E5, G5, E4, G5, E5, B4, E5, G5, E4, G5, E5,
  C5,
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {

  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1,

};


// interrupt function
void Interrupt ()
{
  interruptSwitch = 1;
  detachInterrupt (knockPin);
}

void setup() {
  pinMode(knockPin, INPUT);
  Serial.begin (9600);
  attachInterrupt (digitalPinToInterrupt (knockPin), Interrupt, RISING);
}


void loop() {
  pinMode(knockPin, INPUT);
  val = digitalRead (knockPin);
  if (interruptSwitch == 1) {
    delay (200);
    startTime = millis ();
    endTime = millis ();
    while (endTime - countInterval < startTime) {
      val = digitalRead (knockPin);
      endTime = millis();
      if (val == HIGH) {
        for (int thisNote = 0; thisNote < melodyLength; thisNote++) {
          int noteDuration = tempo / noteDurations[thisNote];
          tone(2, melody[thisNote], noteDuration);
          int pauseBetweenNotes = noteDuration * 1.30;
          delay(pauseBetweenNotes);
          noTone(8);
        }
      }
      Serial.println (val);
      interruptSwitch = 0;
    }

  }
  else {
    LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
  }
}

Comments

Similar projects you might like

Interactive Toddler Music Box Toy

Project tutorial by Christopher Monreal and Nick Udell

  • 1,223 views
  • 0 comments
  • 2 respects

Electron Music Box Buzzer App

Project tutorial by Iain

  • 2,087 views
  • 0 comments
  • 4 respects

Control Music Volume and Ledbar Using Slider of 1Sheeld

Project tutorial by ahmed ismail

  • 1,952 views
  • 2 comments
  • 14 respects

Out of My League - Music on Arduino

Project tutorial by MrZmann

  • 1,261 views
  • 0 comments
  • 2 respects

How to make music with an Arduino

Project tutorial by Code_and_Make

  • 7,501 views
  • 0 comments
  • 16 respects
Add projectSign up / Login