Project tutorial
Keyboard Melodies

Keyboard Melodies

Our project is a microcontroller that plays 4 different melodies/songs depending on which button you press.

  • 3,064 views
  • 2 comments
  • 11 respects

Components and supplies

Apps and online services

About this project

Overview

Our project is a microcontroller that plays different melodies/songs depending on which button you press. Our microcontroller can play the “Jeopardy Theme”, “Mary Had a Little Lamb” and the choruses of “Lonely” by Akon and “All Star” by Smash Mouth. This project was made by Natalie Clark (15) and Ava Baker (14) for an introduction to engineering class. It uses a piezo/buzzer for sound, four resistors (all different ohms), four buzzers and lots of jumper wires. We used an Arduino Uno to make this. If you're into coding, this is the project for you! The breadboard itself is pretty simple but the code is extensive.

Customization

If you wish, you can also insert your own songs using the melody[] and beat[] functions, so this project is completely customizable. To change a song you have to write down all of the notes (transpose everything into c major) and their beat lengths which will be entered separately. Make sure that you've already defined all the notes that you're using (the equation for calculating note periods is commented into the code). You can also feel free to make the project more complicated, maybe add LEDs that light up when their button is pressed.

Finished Product

https://drive.google.com/file/d/1lXxYTiarZHrmGKzVT0rIs9rMB3h5xFKd/view?usp=sharing

Code

Keyboard MelodiesC/C++
Here is the code to help you complete the project.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// TONES
//Period = 1,000,000(1/frequency)
//letter=note #=octave b=flat
//       note,  period, &  frequency. 
#define  d3     6803    // 147 Hz
#define  c4     3830    // 261 Hz
#define  d4b    3610    // 277 Hz
#define  d4     3400    // 294 Hz
#define  e4b    3215    // 311 Hz
#define  e4     3038    // 329 Hz
#define  f4     2864    // 349 Hz
#define  g4b    2703    // 370 Hz
#define  g4     2550    // 392 Hz
#define  a4b    2410    // 415 Hz
#define  a4     2272    // 440 Hz
#define  b4b    2145    // 466 Hz
#define  b4     2028    // 493 Hz
#define  c5     1912    // 523 Hz
#define  d5b    1805    // 554 Hz
#define  d5     1703    // 587 Hz
#define  e5b    1607    // 622 Hz
#define  e5     1517    // 659 Hz
#define  f5     1433    // 698 Hz
#define  g5b    1351    // 740 Hz
#define  g5     1276    // 784 Hz
#define  a5b    1204    // 831 Hz
#define  a5     1136    // 880 Hz
#define  b5b    1073    // 932 Hz
#define  b5     1012    // 988 Hz
//A special note, 'R', to represent a rest
#define  R      0

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CORE VARIABLES
int speakerOut = 9; //Set up speaker on pin 9
long tempo = 10000; //Set overall tempo
int pause = 1000; //Length of pause between individual notes
int rest_count = 100;
int tone_ = 0;
int beat = 0;
long duration = 0;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MARY HAD A LITTLE LAMB
//Quarter note = 40
int melody1[] = { b4, a4, g4, a4, b4, b4, b4, R, a4, a4, a4, R, b4, d4, d4, R, b4, a4, g4, a4, b4, b4, b4, b4, a4, a4, b4, a4, g4 }; //Tones/notes
int beats1[] = { 40, 40, 40, 40, 40, 40, 40, 2, 40, 40, 40, 2, 40, 40, 40, 2, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 80 }; //Tone/note lengths
int MAX_COUNT1 = sizeof(melody1)/2; //Melody length for looping

// ALL STAR
//Quarter note = 100
int melody2[] = { b4b, g4b, g4b, e4, g4b, g4b, g4b, e4, g4b, g4b, g4b, b4b, R, b4b, d5b, b4, d5b, e5b, g5b, g5b, a5b, g5b, g4b, g4b, a4b, g4b, b4b, a4b, a4b, g4b, a4b, a4b, b4b, e4b, e4b }; //Tones/notes
int beats2[] = { 50, 100, 25, 25, 50, 100, 25, 25, 50, 100, 100, 100, 25, 100, 100, 50, 50, 50, 50, 50, 50, 100, 50, 50, 50, 50, 50, 100, 100, 100, 50, 50, 50, 200, 50 }; //Tone/note lengths
int MAX_COUNT2 = sizeof(melody2)/2; //Melody length for looping

// JEOPARDY
//Quarter note = 64
int melody3[] = { c5, f5, c5, f4, c5, f5, c5, c5, f5, c5, a5, g5, f5, e5, d5, d5b, c5, f5, c5, f4, c5, f5, c5, f5, d5, c5, b4b, a4, g4, f4 }; //Tones/notes
int beats3[] = { 64, 64, 64, 64, 64, 64, 128, 64, 64, 64, 96, 32, 32, 32, 32, 32, 64, 64, 64, 64, 64, 64, 128, 96, 32, 64, 64, 64, 64, 64 }; //Tone/note lengths
int MAX_COUNT3 = sizeof(melody3)/2; //Melody length for looping

// LONELY
//Quarter note = 84
int melody4[] = { d4, a4, g4b, a4, d4b, a4, g4b, g4, a4, d4b, g4, g4b, g4, a4, b4, a4, a5, a5 }; //Tones/notes
int beats4[] = { 84, 168, 63, 21, 84, 168, 32, 32, 32, 84, 63, 32, 32, 32, 32, 168, 84, 84 }; //Tone/note lengths
int MAX_COUNT4 = sizeof(melody4)/2; //Melody length for looping

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// PLAY TONE
void playTone() { //Pulse the speaker to play a tone for a particular duration (so that there is a small break between each note and they don't immediatly follow each other)
  long elapsed_time = 0;
  if (tone_ > 0) { //if this isn't a rest beat, while the tone has played less long than 'duration', pulse speaker HIGH and LOW
    while (elapsed_time < duration) {
      digitalWrite(speakerOut,HIGH);
      delayMicroseconds(tone_ / 2);
      digitalWrite(speakerOut, LOW);
      delayMicroseconds(tone_ / 2);
      elapsed_time += (tone_); //Keep track of how long we pulsed
    } 
  }
  else {
    for (int j = 0; j < rest_count; j++) {
      delayMicroseconds(duration);  
    }                                
  }                                 
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SETUP
void setup() {
 pinMode(speakerOut, OUTPUT); 
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// LOOP
void loop() {
  int keyVal = analogRead(A0);
  Serial.println(keyVal);
  // PIN ONE (MARY HAD A LITTLE LAMB)
  if(keyVal >= 1011){ //Only when the 1st button is pressed
    for (int i=0; i<MAX_COUNT1; i++) { //Set up a counter to pull from melody1[] and beats1[]
      tone_ = melody1[i]; //Defining which notes to play
      beat = beats1[i]; //Defining the note lengths to use
      duration = beat * tempo; //Set up timing
      playTone(); //Inserting/applying the code for a pause between notes 
      delayMicroseconds(pause);
    }
  }
  // PIN TWO (ALL STAR)
  else if(keyVal >= 990 && keyVal <= 1010){ //Only when the 2nd button is pressed
    for (int i=0; i<MAX_COUNT2; i++) { //Set up a counter to pull from melody2[] and beats2[]
      tone_ = melody2[i]; //Defining which notes to play
      beat = beats2[i]; //Defining the note lengths to use
      duration = beat * tempo; //Set up timing
      playTone(); //Inserting/applying the code for a pause between notes
      delayMicroseconds(pause);
    }
  }
  // PIN THREE (JEOPARDY)
  else if(keyVal >= 480 && keyVal <= 515){ //Only when the 3rd button is pressed
    for (int i=0; i<MAX_COUNT3; i++) { //Set up a counter to pull from melody3[] and beats3[]
      tone_ = melody3[i]; //Defining which notes to play
      beat = beats3[i]; //Defining the note lengths to use
      duration = beat * tempo; //Set up timing
      playTone(); //Inserting/applying the code for a pause between notes
      delayMicroseconds(pause);
    }
  }
  // PIN FOUR (LONELY)
  else if(keyVal >= 5 && keyVal <= 10){ //Only when the 4th button is pressed
    for (int i=0; i<MAX_COUNT4; i++) { //Set up a counter to pull from melody4[] and beats4[]
      tone_ = melody4[i]; //Defining which notes to play
      beat = beats4[i]; //Defining the note lengths to use
      duration = beat * tempo; //Set up timing
      playTone(); //Inserting/applying the code for a pause between notes
      delayMicroseconds(pause);
    }
  }
  // NONE
  else{
    noTone(8);
  }
}

Schematics

Breadboard
This is what our breadboard looked like.
Keyboardmelodiesbreadboard rqq39kp3gp
Fritzing Diagram
Here's a fritzing diagram of the breadboard and Arduino.
Keyboardmelodiesupdatedfritzing png zxaxjqjzzl

Comments

Similar projects you might like

Electronic Piano Keyboard With Preset Songs

Project tutorial by Lindsay Fox

  • 77,978 views
  • 60 comments
  • 150 respects

Arduino Keyboard

Project in progress by Gabriele Scordamaglia

  • 7,281 views
  • 16 comments
  • 15 respects

Keyboard V2.0

Project in progress by Gabriele Scordamaglia

  • 5,154 views
  • 18 comments
  • 14 respects

Arduino Programmed Keyboard Strikes

Project in progress by Sam

  • 855 views
  • 0 comments
  • 1 respect

Arduino Kitchen Timer

Project tutorial by Team I and myself

  • 54,448 views
  • 37 comments
  • 77 respects

Ultrasonic Sensor with Alarm, LCD and Temperature

Project tutorial by MichDragstar

  • 5,212 views
  • 7 comments
  • 19 respects
Add projectSign up / Login