#include <IRremote.hpp>
unsigned long keyvalue=0;
#define note_c5 523 //D0
#define note_d5 587 //Re
#define note_e5 659 //Mi
#define note_f5 698 //Fa
#define note_g5 784 //Sol
#define note_a5 880 //La
#define note_b5 988 //Ti
#define note_c6 1047 //Do
const int tonepin=8;
void setup() {
Serial.begin(9600);
IrReceiver.begin(A0);
}
void loop() {
if(IrReceiver.decode()){
keyvalue=IrReceiver.decodedIRData.decodedRawData;
Serial.println(keyvalue,HEX);
switch(keyvalue){
case 3125149440 : piezoSound(1); break;
case 3108437760 : piezoSound(2); break;
case 3091726080 : piezoSound(3); break;
case 3141861120 : piezoSound(4); break;
case 3208707840 : piezoSound(5); break;
case 3158572800 : piezoSound(6); break;
case 4161273600 : piezoSound(7); break;
case 3927310080 : piezoSound(8); break;
}
}
IrReceiver.resume();
}
void piezoSound(int i){
if(i==1){tone(tonepin,note_c5,1000);}
else if(i==2){tone(tonepin,note_d5,1000);}
else if(i==3){tone(tonepin,note_e5,1000);}
else if(i==4){tone(tonepin,note_f5,1000);}
else if(i==5){tone(tonepin,note_g5,1000);}
else if(i==6){tone(tonepin,note_a5,1000);}
else if(i==7){tone(tonepin,note_b5,1000);}
else if(i==8){tone(tonepin,note_c5,1000);}
}
The objective of this code is to use numbers 1-8 of the IR remocon to make the sounds 1-do, 2-le, 3-mi, 4-wa, 5-sol, 6-la, 7-si, and 8-do come out from the piezo speaker for 1 second. However, I don't understand why the key value keep changes and it only works one time when i start the code .