What I'm trying to do
I wired up buzzer and IR receiver.
I'm trying to play music with buzzer using IR remote.
I intend to make the project so that
if I press button 0, playtestSong() gets called to play some music on the buzzer
if I press button 1, playHipHop() gets called and so on.
Overview of the Issue
However, in reality,
when I press button 0, the music plays the first time but none of the buttons work for the second time.
If I remove playtestSong();
then remote works fine.
The print statements inside the if statement for respective buttons gets executed.
For example, if I press button 1, 2, 3, it outputs
You pressed button 1
You pressed button 2
You pressed button 3
What I tried to fix it
I added print statements before, after, and inside the playtestSong();
.
There is another print statement Serial.println("after if");
at the end of the if statements.
If I run it and press button 0, the print statement before, inside, after playtestSong();
gets executed and works fine for the first button press.
First time I pressed button 0:
You pressed button 0
Playing test song
Done with test song
Exited out of the function
after if
However, if I press it again. Only "after if" gets printed. The whole if statement doesn't seem to be running.
Second, third, fourth time I pressed button 0:
after if
after if
after if
I figure out the issue but I don't know how to fix it
So I printed buttonCode after the if statements.
Serial.println(buttonCode);
and when I run the program and press button 0 it only print 3910598400 (the button code associated with button 0) first time but seemingly random number thereafter.
Pressed button 0 four times
3910598400
2287147692
16384
96
Reuploaded and pressed button 0 four times again
3910598400
2048
2450028189
2104792976
When I removed playtestSong();
it consistently prints 3910598400
pressed button 0 four times
3910598400
3910598400
3910598400
3910598400
But why on earth would playtestSong() affect the variable buttonCode?
The code
music.ino
#include <IRremote.hpp>
const int IR_RECEIVE_PIN = 9;
void setup() {
Serial.begin(9600);
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK); // Start the receiver
}
void loop() {
if (IrReceiver.decode()) {
unsigned long buttonCode = IrReceiver.decodedIRData.decodedRawData;
// Button 0
if (buttonCode == 3910598400) {
Serial.println("You pressed button 0");
playtestSong();
Serial.println("Song done");
}
// Button 1
else if (buttonCode == 4077715200) {
Serial.println("You pressed button 1");
}
// Button 2
else if (buttonCode == 3877175040) {
Serial.println("You pressed button 2");
}
// Button 3
else if (buttonCode == 2707357440) {
Serial.println("You pressed button 3");
}
// Button 4
else if (buttonCode == 4144561920) {
Serial.println("You pressed button 4");
}
// Button 5
else if (buttonCode == 3810328320) {
Serial.println("You pressed button 5");
}
// Button 6
else if (buttonCode == 2774204160) {
Serial.println("You pressed button 6");
}
// Button 7
else if (buttonCode == 3175284480) {
Serial.println("You pressed button 7");
}
// Button 8
else if (buttonCode == 2907897600) {
Serial.println("You pressed button 8");
}
// Button 9
else if (buttonCode == 3041591040) {
Serial.println("You pressed button 9");
}
Serial.println("after if");
Serial.println(buttonCode);
IrReceiver.resume(); // Enable receiving of the next value
}
delay(0.01);
}
musicFunctions.ino
const int BUZZER_PIN = 8;
// Note frequencies in Hz
const int NOTE_B0 = 31;
const int NOTE_C1 = 33;
... other notes here
const int NOTE_D8 = 4699;
const int NOTE_DS8 = 4978;
void playNote(int note, int duration) {
tone(BUZZER_PIN, note, duration);
delay(duration * 1.30); // Adding a delay to separate the notes
noTone(BUZZER_PIN); // Stop the tone
}
void playtestSong() {
Serial.println("Playing test song");
playNote(NOTE_E5, 300);
playNote(NOTE_DS5, 300);
Serial.println("Done with test song");
}
I'm using GitHub - Arduino-IRremote/Arduino-IRremote: Infrared remote library for Arduino: send and receive infrared signals with multiple protocols library