Need help using IR

#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 .

I moved your topic to an appropriate forum category @wqeeq.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Welcome to the Arduino forum, @wqeeq ! As you have already found out, the forum has rules and etiquette we all must abide by. When we do so, forum interactions are usually much more fruitful for the poster, and less frustrating for the helpers.
Again, welcome!

1 Like

It's a matter of the IR protocol. Print the protocol name for more information.

Please read

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.