Components and supplies
Resistor 10k ohm
Photo resistor
Resistor 221 ohm
Resistor 4.75k ohm
Arduino UNO
Resistor 100 ohm
Buzzer, Piezo
Resistor 1k ohm
RGB Diffused Common Cathode
Tactile Switch, Top Actuated
Resistor 1M ohm
Apps and platforms
Arduino IDE
Project description
Code
Seven Note Keyboard
c_cpp
1/* 2 Seven notes keyboard + 4 songs + rec and play functions 3 Hacking of Arduino Starter Kit example Project 7 - Keyboard created 13 Sep 2012 by Scott Fitzgerald 4 5 created February 2022 6 by Roberto Zaffanella 7 8*/ 9 10// create an array of notes 11int notes[] = {0, 262, 294, 330, 349, 392, 440, 494, 196}; 12// DO4 RE4 MI4 FA4 SOL4 LA4 SI4 SOL3 13 14int 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}}; 15// OFF BLUE PURPLE RED GREEN ORANGE PINK YELLOW ORANGE 16 17// Oh! When the saints go marchinig'in (first digit is the note , the second one is the note duration) 18int song_1[32][2] = {{1,1}, {3,1}, {4,1}, {5,5}, {1,1}, {3,1}, {4,1}, {5,5}, 19 {1,1}, {3,1}, {4,1}, {5,2}, {3,2}, {1,2}, {3,2}, {2,5}, 20 {3,1}, {3,1}, {2,1}, {1,3}, {1,1}, {3,2}, 21 {5,2}, {5,1}, {4,5}, 22 {3,1}, {4,1}, {5,2}, {3,2}, {1,2}, {2,2}, {1,5}}; 23 24// Jingle Bells 25int song_2[30][2] = {{3,1}, {3,1}, {3,2}, {3,1}, {3,1}, {3,2}, 26 {3,1}, {5,1}, {1,1}, {2,1}, {3,4}, 27 {4,1}, {4,1}, {4,1}, {4,1}, {4,1}, {3,1}, {3,1}, {3,1}, 28 {3,1}, {2,1}, {2,1}, {3,1}, {2,2}, {5,2}, 29 {5,1}, {5,1}, {4,1}, {2,1}, {1,2}}; 30 31// Oh! Susanna 32int 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}, 33 {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}, 34 {4,2}, {4,2}, {6,1}, {6,2}, {6,1}, {5,1}, {5,1}, {3,1}, {1,1}, {2,3}, 35 {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}}; 36 37// 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) 38int song_4[62][2] = {{3,2}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2}, 39 {1,2}, {1,2}, {2,2}, {3,2}, {3,3}, {2,1}, {2,4}, 40 {3,2}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2}, 41 {1,2}, {1,2}, {2,2}, {3,2}, {2,3}, {1,1}, {1,4}, 42 {2,2}, {2,2}, {3,2}, {1,2}, {2,2}, {3,1}, {4,1}, {3,2}, {1,2}, 43 {2,2}, {3,1}, {4,1}, {3,2}, {2,2}, {1,2}, {2,2}, {8,2}, 44 {3,4}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2}, 45 {1,2}, {1,2}, {2,2}, {3,2}, {2,3}, {1,1}, {1,4}}; 46 47int rec[110][2] = {}; //first digit is the note (0 for nothing played) and second digit is the duration 48const int greenLEDPin = 10; 49const int redLEDPin = 11; 50const int blueLEDPin = 9; 51const int photoresistor = A3; 52int btn = 0; // 0 = no buttons are pressed 53int j = 0; // recording index 54int current_btn = 0; 55 56void setup() { 57 pinMode(2,INPUT); // Button 7 58 pinMode(greenLEDPin,OUTPUT); 59 pinMode(redLEDPin,OUTPUT); 60 pinMode(blueLEDPin,OUTPUT); 61 Serial.begin(9600); 62} 63 64void loop() { 65 btn = read_btn(); 66 delay (50); //without this delay first note not sound good 67 // store the duration of pressed button and also the pauses between notes 68 if (current_btn != btn && btn<8){ 69 current_btn = btn; 70 rec[j][0] = btn; rec[j][1] = millis(); 71 j++; 72 if (j>110){j=0; rec[110][2] = {};} // reset recording buffer if full 73 } 74 if (btn==8){led_off();noTone(8);delay(2000);play_song_1();btn=0;} 75 if (btn==9){led_off();noTone(8);delay(2000);play_song_2();btn=0;} 76 if (btn==10){led_off();noTone(8);delay(2000);play_song_3();btn=0;} 77 if (btn==11){led_off();noTone(8);delay(2000);play_song_4();btn=0;} 78 //if (btn==12){delay(1000); Serial.println(j); j=0; btn=0;} // button 12 can be used for further needs 79 //if (btn==13){led_off();noTone(8);delay(2000); play_rec();btn=0;} // button 13 can be used for further needs 80 if (btn==14){led_off();noTone(8);delay(2000); play_rec();btn=0;} // play the performance played on keyboard 81 play_note(); 82 btn=0; 83} 84 85int read_btn(){ 86 int keyVal = analogRead(A0); 87 //recognize which button has been pressed 88 if (keyVal == 1023) {btn=1;} 89 if (keyVal >= 990 && keyVal <= 1010) {btn=2;} 90 if (keyVal >= 965 && keyVal <= 975) {btn=3;} 91 if (keyVal >= 690 && keyVal <= 700) {btn=4;} 92 if (keyVal >= 505 && keyVal <= 515) {btn=5;} 93 if (keyVal >= 5 && keyVal <= 12) {btn=6;} 94 if (digitalRead(2) == HIGH && btn==0) {btn =7;} 95 if (digitalRead(2) == HIGH && btn==1) {btn =8;} // for button 8, press both button 1 and 7 96 if (digitalRead(2) == HIGH && btn==2) {btn =9;} // for button 9, press both button 2 and 7 97 if (digitalRead(2) == HIGH && btn==3) {btn =10;} // for button 10, press both button 3 and 7 98 if (digitalRead(2) == HIGH && btn==4) {btn =11;} // for button 11, press both button 4 and 7 99 if (digitalRead(2) == HIGH && btn==5) {btn =12;} // for button 12, press both button 5 and 7 100 if (digitalRead(2) == HIGH && btn==6) {btn =13;} // for button 13, press both button 6 and 7 101 if (analogRead(photoresistor) < 11) {btn =14;} // for button 14, cover the photoresistor 102 //Serial.println (analogRead(playPin)); 103 //delay (300); 104 return (btn); 105} 106 107void play_note(){ 108 if (btn==0){ 109 noTone(8); 110 led_off(); 111 } 112 else { 113 led_on (btn); 114 tone(8, notes[btn]); 115 } 116} 117 118int play_note_song(int note_num, int duration){ 119 led_on (note_num); 120 tone(8, notes[note_num], duration); 121 delay (duration); 122 led_off(); 123} 124 125int led_on (int btn){ 126 analogWrite(redLEDPin, rgb[btn][0]); 127 analogWrite(greenLEDPin, rgb[btn][1]); 128 analogWrite(blueLEDPin, rgb[btn][2]); 129} 130 131void led_off(){ 132 analogWrite(redLEDPin, rgb[0][0]); 133 analogWrite(greenLEDPin, rgb[0][1]); 134 analogWrite(blueLEDPin, rgb[0][2]); 135} 136 137void play_song_1(){ 138 for (int i=0;i<32; i++) { 139 play_note_song (song_1[i][0], song_1[i][1]*200); 140 delay (100); //pause between a note and the other 141 } 142} 143 144void play_song_2(){ 145 for (int i=0;i<30; i++) { 146 play_note_song (song_2[i][0], song_2[i][1]*200); 147 delay (100); //pause between a note and the other 148 } 149} 150 151void play_song_3(){ 152 for (int i=0;i<52; i++) { 153 play_note_song (song_3[i][0], song_3[i][1]*200); 154 delay (100); //pause between a note and the other 155 } 156} 157 158void play_song_4(){ 159 for (int i=0;i<62; i++) { 160 play_note_song (song_4[i][0], song_4[i][1]*200); 161 delay (100); //pause between a note and the other 162 } 163} 164 165void play_rec(){ 166 int duration; 167 168 for (int i=0; i<j; i++){ 169 duration = rec[i+1][1] - rec[i][1]; 170 if (duration>1000){duration=777;} 171 if (duration<0){duration=888;} 172 if (rec[i][0]==0){delay(duration);} 173 else {play_note_song(rec[i][0], duration);} 174 // next instructions are to debug recording and play feature 175 //Serial.print (i); 176 //Serial.print ("---"); 177 //Serial.print (rec[i][0]); 178 //Serial.print (" "); 179 //Serial.println (duration); 180 } 181 //Serial.print ("exit j="); 182 //Serial.println (j); 183} 184
Seven Note Keyboard
c_cpp
1/* 2 Seven notes keyboard + 4 songs + rec and play functions 3 Hacking 4 of Arduino Starter Kit example Project 7 - Keyboard created 13 Sep 2012 by Scott 5 Fitzgerald 6 7 created February 2022 8 by Roberto Zaffanella 9 10*/ 11 12// 13 create an array of notes 14int notes[] = {0, 262, 294, 330, 349, 392, 440, 494, 15 196}; 16// DO4 RE4 MI4 FA4 SOL4 LA4 SI4 SOL3 17 18int rgb[9][3] 19 = {{0,0,0}, {0,0,255}, {255,0,140}, {255,0,0}, {0,255,0}, {255,165,0}, {255,92,203}, 20 {255,255,0}, {255,165,0}}; 21// OFF BLUE PURPLE RED 22 GREEN ORANGE PINK YELLOW ORANGE 23 24// Oh! 25 When the saints go marchinig'in (first digit is the note , the second one is the 26 note duration) 27int song_1[32][2] = {{1,1}, {3,1}, {4,1}, {5,5}, {1,1}, {3,1}, 28 {4,1}, {5,5}, 29 {1,1}, {3,1}, {4,1}, {5,2}, {3,2}, {1,2}, 30 {3,2}, {2,5}, 31 {3,1}, {3,1}, {2,1}, {1,3}, {1,1}, {3,2}, 32 33 {5,2}, {5,1}, {4,5}, 34 {3,1}, {4,1}, 35 {5,2}, {3,2}, {1,2}, {2,2}, {1,5}}; 36 37// Jingle Bells 38int song_2[30][2] 39 = {{3,1}, {3,1}, {3,2}, {3,1}, {3,1}, {3,2}, 40 {3,1}, {5,1}, 41 {1,1}, {2,1}, {3,4}, 42 {4,1}, {4,1}, {4,1}, {4,1}, {4,1}, 43 {3,1}, {3,1}, {3,1}, 44 {3,1}, {2,1}, {2,1}, {3,1}, {2,2}, 45 {5,2}, 46 {5,1}, {5,1}, {4,1}, {2,1}, {1,2}}; 47 48// 49 Oh! Susanna 50int song_3[52][2] = {{1,1}, {3,1}, {5,1}, {5,1}, {6,1}, {5,1}, {3,1}, 51 {1,1}, {2,1}, {3,1}, {3,1}, {2,1}, {1,1}, {2,3}, 52 {1,1}, 53 {3,1}, {5,1}, {5,1}, {6,1}, {5,1}, {3,1}, {1,1}, {2,1}, {3,1}, {3,1}, {2,1}, {2,1}, 54 {1,4}, 55 {4,2}, {4,2}, {6,1}, {6,2}, {6,1}, {5,1}, {5,1}, 56 {3,1}, {1,1}, {2,3}, 57 {1,1}, {3,1}, {5,1}, {5,1}, {6,1}, 58 {5,1}, {3,1}, {1,1}, {2,1}, {3,1}, {3,1}, {2,1}, {2,1}, {1,4}}; 59 60// Ode to 61 Joy (require SOL3 more than basic notes so SOL3 has been added as exception to notes 62 list and also in rgb list color as orange like SOL4) 63int song_4[62][2] = {{3,2}, 64 {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2}, 65 {1,2}, {1,2}, 66 {2,2}, {3,2}, {3,3}, {2,1}, {2,4}, 67 {3,2}, {3,2}, {4,2}, {5,2}, 68 {5,2}, {4,2}, {3,2}, {2,2}, 69 {1,2}, {1,2}, {2,2}, {3,2}, {2,3}, 70 {1,1}, {1,4}, 71 {2,2}, {2,2}, {3,2}, {1,2}, {2,2}, {3,1}, {4,1}, 72 {3,2}, {1,2}, 73 {2,2}, {3,1}, {4,1}, {3,2}, {2,2}, {1,2}, {2,2}, 74 {8,2}, 75 {3,4}, {3,2}, {4,2}, {5,2}, {5,2}, {4,2}, {3,2}, {2,2}, 76 77 {1,2}, {1,2}, {2,2}, {3,2}, {2,3}, {1,1}, {1,4}}; 78 79int 80 rec[110][2] = {}; //first digit is the note (0 for nothing played) and second digit 81 is the duration 82const int greenLEDPin = 10; 83const int redLEDPin = 11; 84const 85 int blueLEDPin = 9; 86const int photoresistor = A3; 87int btn = 0; // 0 = no buttons 88 are pressed 89int j = 0; // recording index 90int current_btn = 0; 91 92void 93 setup() { 94 pinMode(2,INPUT); // Button 7 95 pinMode(greenLEDPin,OUTPUT); 96 97 pinMode(redLEDPin,OUTPUT); 98 pinMode(blueLEDPin,OUTPUT); 99 Serial.begin(9600); 100} 101 102void 103 loop() { 104 btn = read_btn(); 105 delay (50); //without this delay first note 106 not sound good 107 // store the duration of pressed button and also the pauses 108 between notes 109 if (current_btn != btn && btn<8){ 110 current_btn = btn; 111 112 rec[j][0] = btn; rec[j][1] = millis(); 113 j++; 114 if (j>110){j=0; rec[110][2] 115 = {};} // reset recording buffer if full 116 } 117 if (btn==8){led_off();noTone(8);delay(2000);play_song_1();btn=0;} 118 119 if (btn==9){led_off();noTone(8);delay(2000);play_song_2();btn=0;} 120 if 121 (btn==10){led_off();noTone(8);delay(2000);play_song_3();btn=0;} 122 if (btn==11){led_off();noTone(8);delay(2000);play_song_4();btn=0;} 123 124 //if (btn==12){delay(1000); Serial.println(j); j=0; btn=0;} // button 12 125 can be used for further needs 126 //if (btn==13){led_off();noTone(8);delay(2000); 127 play_rec();btn=0;} // button 13 can be used for further needs 128 if (btn==14){led_off();noTone(8);delay(2000); 129 play_rec();btn=0;} // play the performance played on keyboard 130 play_note(); 131 132 btn=0; 133} 134 135int read_btn(){ 136 int keyVal = analogRead(A0); 137 //recognize 138 which button has been pressed 139 if (keyVal == 1023) {btn=1;} 140 if (keyVal 141 >= 990 && keyVal <= 1010) {btn=2;} 142 if (keyVal >= 965 && keyVal <= 975) {btn=3;} 143 144 if (keyVal >= 690 && keyVal <= 700) {btn=4;} 145 if (keyVal >= 505 && keyVal 146 <= 515) {btn=5;} 147 if (keyVal >= 5 && keyVal <= 12) {btn=6;} 148 if (digitalRead(2) 149 == HIGH && btn==0) {btn =7;} 150 if (digitalRead(2) == HIGH && btn==1) {btn =8;} 151 // for button 8, press both button 1 and 7 152 if (digitalRead(2) == HIGH && btn==2) 153 {btn =9;} // for button 9, press both button 2 and 7 154 if (digitalRead(2) == 155 HIGH && btn==3) {btn =10;} // for button 10, press both button 3 and 7 156 if 157 (digitalRead(2) == HIGH && btn==4) {btn =11;} // for button 11, press both button 158 4 and 7 159 if (digitalRead(2) == HIGH && btn==5) {btn =12;} // for button 12, 160 press both button 5 and 7 161 if (digitalRead(2) == HIGH && btn==6) {btn =13;} 162 // for button 13, press both button 6 and 7 163 if (analogRead(photoresistor) 164 < 11) {btn =14;} // for button 14, cover the photoresistor 165 //Serial.println 166 (analogRead(playPin)); 167 //delay (300); 168 return (btn); 169} 170 171void play_note(){ 172 173 if (btn==0){ 174 noTone(8); 175 led_off(); 176 } 177 else { 178 led_on 179 (btn); 180 tone(8, notes[btn]); 181 } 182} 183 184int play_note_song(int note_num, 185 int duration){ 186 led_on (note_num); 187 tone(8, notes[note_num], duration); 188 189 delay (duration); 190 led_off(); 191} 192 193int led_on (int btn){ 194 analogWrite(redLEDPin, 195 rgb[btn][0]); 196 analogWrite(greenLEDPin, rgb[btn][1]); 197 analogWrite(blueLEDPin, 198 rgb[btn][2]); 199} 200 201void led_off(){ 202 analogWrite(redLEDPin, rgb[0][0]); 203 204 analogWrite(greenLEDPin, rgb[0][1]); 205 analogWrite(blueLEDPin, rgb[0][2]); 206} 207 208void 209 play_song_1(){ 210 for (int i=0;i<32; i++) { 211 play_note_song (song_1[i][0], 212 song_1[i][1]*200); 213 delay (100); //pause between a note and the other 214 215 } 216} 217 218void play_song_2(){ 219 for (int i=0;i<30; i++) { 220 play_note_song 221 (song_2[i][0], song_2[i][1]*200); 222 delay (100); //pause between a note and 223 the other 224 } 225} 226 227void play_song_3(){ 228 for (int i=0;i<52; i++) { 229 230 play_note_song (song_3[i][0], song_3[i][1]*200); 231 delay (100); //pause 232 between a note and the other 233 } 234} 235 236void play_song_4(){ 237 for (int 238 i=0;i<62; i++) { 239 play_note_song (song_4[i][0], song_4[i][1]*200); 240 delay 241 (100); //pause between a note and the other 242 } 243} 244 245void play_rec(){ 246 247 int duration; 248 249 for (int i=0; i<j; i++){ 250 duration = rec[i+1][1] 251 - rec[i][1]; 252 if (duration>1000){duration=777;} 253 if (duration<0){duration=888;} 254 255 if (rec[i][0]==0){delay(duration);} 256 else {play_note_song(rec[i][0], 257 duration);} 258 // next instructions are to debug recording and play feature 259 260 //Serial.print (i); 261 //Serial.print ("---"); 262 //Serial.print (rec[i][0]); 263 264 //Serial.print (" "); 265 //Serial.println (duration); 266 } 267 //Serial.print 268 ("exit j="); 269 //Serial.println (j); 270} 271
Downloadable files
Circuit Diagram
Circuit Diagram
Comments
Only logged in users can leave comments
zaffaroby
0 Followers
•0 Projects
Table of contents
Intro
0
0