Ava Baker -21Natalie Clark -20
Published

Keyboard Melodies

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

IntermediateFull instructions provided15 minutes4,077
Keyboard Melodies

Things used in this project

Story

Read more

Schematics

Breadboard

This is what our breadboard looked like.

Fritzing Diagram

Here's a fritzing diagram of the breadboard and Arduino.

Code

Keyboard Melodies

C/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);
  }
}

Credits

Ava Baker -21

Ava Baker -21

1 project • 1 follower
Natalie Clark -20

Natalie Clark -20

1 project • 2 followers

Comments