Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
zaffaroby
Published © CC BY

Seven Note Keyboard

Play and listen to your performance

IntermediateFull instructions provided264
Seven Note Keyboard

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Resistor 221 ohm
Resistor 221 ohm
×4
Resistor 100 ohm
Resistor 100 ohm
replace with 560 ohm
×1
Resistor 4.75k ohm
Resistor 4.75k ohm
×1
Resistor 10k ohm
Resistor 10k ohm
×4
Resistor 1M ohm
Resistor 1M ohm
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Buzzer, Piezo
Buzzer, Piezo
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Photo resistor
Photo resistor
×1
Tactile Switch, Top Actuated
Tactile Switch, Top Actuated
×7

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Circuit Diagram

Code

Seven Note Keyboard

C/C++
/*
  Seven notes keyboard + 4 songs + rec and play functions
  Hacking of Arduino Starter Kit example Project 7 - Keyboard created 13 Sep 2012 by Scott Fitzgerald

  created February 2022
  by Roberto Zaffanella

*/

// create an array of notes
int notes[] = {0, 262, 294, 330, 349, 392, 440, 494, 196};
//                DO4  RE4  MI4  FA4  SOL4 LA4  SI4  SOL3 

int rgb[9][3] = {{0,0,0}, {0,0,255}, {255,0,140}, {255,0,0}, {0,255,0}, {255,165,0}, {255,92,203}, {255,255,0}, {255,165,0}};
//                OFF       BLUE       PURPLE       RED        GREEN      ORANGE       PINK          YELLOW       ORANGE

// Oh! When the saints go marchinig'in (first digit is the note , the second one is the note duration)
int song_1[32][2] = {{1,1}, {3,1}, {4,1}, {5,5}, {1,1}, {3,1}, {4,1}, {5,5}, 
                     {1,1}, {3,1}, {4,1}, {5,2}, {3,2}, {1,2}, {3,2}, {2,5},
                     {3,1}, {3,1}, {2,1}, {1,3}, {1,1}, {3,2},
                     {5,2}, {5,1}, {4,5}, 
                     {3,1}, {4,1}, {5,2}, {3,2}, {1,2}, {2,2}, {1,5}};

// Jingle Bells
int song_2[30][2] = {{3,1}, {3,1}, {3,2}, {3,1}, {3,1}, {3,2}, 
                     {3,1}, {5,1}, {1,1}, {2,1}, {3,4},
                     {4,1}, {4,1}, {4,1}, {4,1}, {4,1}, {3,1}, {3,1}, {3,1},
                     {3,1}, {2,1}, {2,1}, {3,1}, {2,2}, {5,2}, 
                     {5,1}, {5,1}, {4,1}, {2,1}, {1,2}};                                                      

// Oh! Susanna
int song_3[52][2] = {{1,1}, {3,1}, {5,1}, {5,1}, {6,1}, {5,1}, {3,1}, {1,1}, {2,1}, {3,1}, {3,1}, {2,1}, {1,1}, {2,3},
                     {1,1}, {3,1}, {5,1}, {5,1}, {6,1}, {5,1}, {3,1}, {1,1}, {2,1}, {3,1}, {3,1}, {2,1}, {2,1}, {1,4}, 
                     {4,2}, {4,2}, {6,1}, {6,2}, {6,1}, {5,1}, {5,1}, {3,1}, {1,1}, {2,3}, 
                     {1,1}, {3,1}, {5,1}, {5,1}, {6,1}, {5,1}, {3,1}, {1,1}, {2,1}, {3,1}, {3,1}, {2,1}, {2,1}, {1,4}};

// Ode to Joy (require SOL3 more than basic notes so SOL3 has been added as exception to notes list and also in rgb list color as orange like SOL4)
int song_4[62][2] = {{3,2}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2},
                   {1,2}, {1,2}, {2,2}, {3,2}, {3,3}, {2,1}, {2,4},
                   {3,2}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2},
                   {1,2}, {1,2}, {2,2}, {3,2}, {2,3}, {1,1}, {1,4},
                   {2,2}, {2,2}, {3,2}, {1,2}, {2,2}, {3,1}, {4,1}, {3,2}, {1,2},
                   {2,2}, {3,1}, {4,1}, {3,2}, {2,2}, {1,2}, {2,2}, {8,2},
                   {3,4}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2},
                   {1,2}, {1,2}, {2,2}, {3,2}, {2,3}, {1,1}, {1,4}};

int rec[110][2] = {};  //first digit is the note (0 for nothing played) and second digit is the duration
const int greenLEDPin = 10;
const int redLEDPin = 11;
const int blueLEDPin = 9;
const int photoresistor = A3;
int btn = 0; // 0 = no buttons are pressed
int j = 0; // recording index
int current_btn = 0;

void setup() {
  pinMode(2,INPUT); // Button 7
  pinMode(greenLEDPin,OUTPUT);
  pinMode(redLEDPin,OUTPUT);
  pinMode(blueLEDPin,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  btn = read_btn();
  delay (50); //without this delay first note not sound good
  // store the duration of pressed button and also the pauses between notes
  if (current_btn != btn && btn<8){
    current_btn = btn;
    rec[j][0] = btn; rec[j][1] = millis();
    j++;
    if (j>110){j=0; rec[110][2] = {};} // reset recording buffer if full
  }
  if (btn==8){led_off();noTone(8);delay(2000);play_song_1();btn=0;} 
  if (btn==9){led_off();noTone(8);delay(2000);play_song_2();btn=0;}  
  if (btn==10){led_off();noTone(8);delay(2000);play_song_3();btn=0;} 
  if (btn==11){led_off();noTone(8);delay(2000);play_song_4();btn=0;} 
  //if (btn==12){delay(1000); Serial.println(j); j=0; btn=0;}  //  button 12 can be used for further needs  
  //if (btn==13){led_off();noTone(8);delay(2000); play_rec();btn=0;}  //  button 13 can be used for further needs  
  if (btn==14){led_off();noTone(8);delay(2000); play_rec();btn=0;}  // play the performance played on keyboard
  play_note();
  btn=0;
}

int read_btn(){
  int keyVal = analogRead(A0);
  //recognize which button has been pressed
  if (keyVal == 1023) {btn=1;} 
  if (keyVal >= 990 && keyVal <= 1010) {btn=2;} 
  if (keyVal >= 965 && keyVal <= 975) {btn=3;} 
  if (keyVal >= 690 && keyVal <= 700) {btn=4;}
  if (keyVal >= 505 && keyVal <= 515) {btn=5;} 
  if (keyVal >= 5 && keyVal <= 12) {btn=6;}
  if (digitalRead(2) == HIGH && btn==0) {btn =7;}
  if (digitalRead(2) == HIGH && btn==1) {btn =8;} // for button 8, press both button 1 and 7
  if (digitalRead(2) == HIGH && btn==2) {btn =9;}  // for button 9, press both button 2 and 7 
  if (digitalRead(2) == HIGH && btn==3) {btn =10;}  // for button 10, press both button 3 and 7
  if (digitalRead(2) == HIGH && btn==4) {btn =11;}  // for button 11, press both button 4 and 7
  if (digitalRead(2) == HIGH && btn==5) {btn =12;}  // for button 12, press both button 5 and 7
  if (digitalRead(2) == HIGH && btn==6) {btn =13;}  // for button 13, press both button 6 and 7
  if (analogRead(photoresistor) < 11) {btn =14;}   // for button 14, cover the photoresistor
  //Serial.println (analogRead(playPin));
  //delay (300);
  return (btn);
}

void play_note(){
  if (btn==0){
    noTone(8);
    led_off();
  }
  else {
    led_on (btn);
    tone(8, notes[btn]);
  } 
}

int play_note_song(int note_num, int duration){
  led_on (note_num);
  tone(8, notes[note_num], duration);
  delay (duration);
  led_off();
}

int led_on (int btn){
  analogWrite(redLEDPin, rgb[btn][0]);
  analogWrite(greenLEDPin, rgb[btn][1]);
  analogWrite(blueLEDPin, rgb[btn][2]);
}

void led_off(){
  analogWrite(redLEDPin, rgb[0][0]);
  analogWrite(greenLEDPin, rgb[0][1]);
  analogWrite(blueLEDPin, rgb[0][2]);
}

void play_song_1(){
  for (int i=0;i<32; i++) {
      play_note_song (song_1[i][0], song_1[i][1]*200);
      delay (100); //pause between a note and the other
  }
}

void play_song_2(){
  for (int i=0;i<30; i++) {
      play_note_song (song_2[i][0], song_2[i][1]*200);
      delay (100); //pause between a note and the other
  }
}

void play_song_3(){
  for (int i=0;i<52; i++) {
      play_note_song (song_3[i][0], song_3[i][1]*200);
      delay (100); //pause between a note and the other
  }
}

void play_song_4(){
  for (int i=0;i<62; i++) {
      play_note_song (song_4[i][0], song_4[i][1]*200);
      delay (100); //pause between a note and the other
  }
}

void play_rec(){
  int duration;
  
  for (int i=0; i<j; i++){
    duration = rec[i+1][1] - rec[i][1];   
    if (duration>1000){duration=777;}
    if (duration<0){duration=888;} 
    if (rec[i][0]==0){delay(duration);}
    else {play_note_song(rec[i][0], duration);}
    // next instructions are to debug recording and play feature
    //Serial.print (i);
    //Serial.print ("---");
    //Serial.print (rec[i][0]);
    //Serial.print (" ");
    //Serial.println (duration);
  }
  //Serial.print ("exit j=");
  //Serial.println (j);
}

Credits

zaffaroby
1 project • 0 followers

Comments