#define led 7
#define sw1 11
#define sw2 12
#define sw3 13
#define btn 8
int val1;
int val2;
int val3;
int note;
int btnVal;
int btnState; // variable to hold the last button state
boolean playMusic=false;
int playVal;
char names[] = {'c', 'd', 'e',
'f', 'g', 'a', 'b', 'C',' '};
int tonesStart[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
int tonesEnd[] = { 1905, 1690, 1509, 1422, 1265, 1126, 1004, 946 };
void setup(){
pinMode(led,OUTPUT);
pinMode(sw1,INPUT);
pinMode(sw2,INPUT);
pinMode(sw3,INPUT);
pinMode(btn,INPUT);
Serial.begin(9600);
btnState=digitalRead(btn);
}
void loop(){
btnVal=digitalRead(btn);
if(btnVal!=btnState){
if(btnVal){
playMusic=!playMusic;
}
}
if(playMusic){
val1=digitalRead(sw1);
val2=digitalRead(sw2);
val3=digitalRead(sw3);
//using the arduino guide above construct your if statements
// If all buttons are pressed, then note is c
if (val1 && val2 && val3) {
note=0;
}
// If the first and third buttons are pressed, then note is d
else if (val1 && !val2 && val3) {
note=1;
}
//continue for all combinations
else if (val1 && val2 && !val3) {
note=2;
}
//continue for all combinations
else if (val1 && !val2 && !val3) {
note=3;
}
//continue for all combinations
else if (!val1 && !val2 && !val3) {
note=4;
}
//continue for all combinations
else if (!val1 && !val2 && val3) {
note=5;
}
//continue for all combinations
else if (!val1 && val2 && !val3) {
note=6;
}
//continue for all combinations
else if (!val1 && val2 && val3) {
note=7;
}
//print the note in the serial monitor
Serial.println(note);
//turn the speaker on, delay the appropriate amount of microseconds
digitalWrite(led,HIGH);
delayMicroseconds(tonesStart[note]);//turn the speaker off and delay the appropriate amount of microseconds
digitalWrite(led,LOW);
delayMicroseconds(tonesEnd[note]);
//turn the speaker off and delay the appropriate amount of microseconds
}else{
digitalWrite(led,LOW);
}
btnState=btnVal;
}