Hello everyone,
I'm using a arduino MEGA2560 board provided by Elegoo in their starter kit. I've set up a piezo buzzer and an IR receiver, and i programmed it to play a melody when the "1" button is pressed on the remote. It does play, but only once. afterwards, all signals from the remote are received as noise, with protocol printing as "UNKNOWN". I have no idea why it does this, and it only does this with buttons im programmed to do something with the buzzer. If its any other button, it prints what its called can some other values, and gets ready to receive the next signal. I'm also not using soldering connections, and i dont know how much of a problem that poses, since im brand new to this.
heres my code.
#define DECODE_NEC
#include "IRremote.h"
#include "pitches.h"
int melody[] = { /* Final fantasy victory fanfare notes*/
NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_GS4, NOTE_B4, NOTE_C5, NOTE_B4, NOTE_C5
};
int noteDurations[] = { /* Duration of each note to be used in calculation during melody*/
8, 8, 8, 4, 4, 4, 4, 8, 2
};
void setup() {
Serial.begin(9600);
Serial.println(F("START " __FILE__ " from " __DATE__ "\r\nUsing library version " VERSION_IRREMOTE));
IrReceiver.begin(11, true);
}
void loop() {
if (IrReceiver.decode())
{
Serial.println(IrReceiver.decodedIRData.command, HEX);
IrReceiver.printIRResultShort(&Serial);
switch (IrReceiver.decodedIRData.command) {
case 0x45: Serial.println("POWER"); break;
case 0x46: Serial.println("FUNC/STOP"); break;
case 0x47: Serial.println("VOL+"); break;
case 0x44: Serial.println("FAST BACK"); break;
case 0x40: Serial.println("PAUSE"); break;
case 0x43: Serial.println("FAST FORWARD"); break;
case 0x7: Serial.println("DOWN"); break;
case 0x15: Serial.println("VOL-"); break;
case 0x9: Serial.println("UP"); break;
case 0x19: Serial.println("EQ"); break;
case 0xD: Serial.println("ST/REPT"); break;
case 0x16:
Serial.println("0");
tone(8, 500, 1000); // to test if the code for the melody was the problem
break;
case 0xC:
for (int thisNote = 0; thisNote < 9; thisNote++) { // the code for the melody itself
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(8);
};
Serial.println("1");
break;
case 0x18: Serial.println("2"); break;
case 0x5E: Serial.println("3"); break;
case 0x8: Serial.println("4"); break;
case 0x1C: Serial.println("5"); break;
case 0x5A: Serial.println("6"); break;
case 0x42: Serial.println("7"); break;
case 0x52: Serial.println("8"); break;
case 0x54: Serial.println("9"); break;
/*case 0xFFFFFFFF: Serial.println(" REPEAT"); break;*/
default: Serial.println(" other button ");
}
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
Serial.println(F("Received noise or an unknown (or not yet enabled) protocol"));
// We have an unknown protocol here, print more info
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
IrReceiver.resume(); // receive the next value
delay(50); // to prevent repeat values
}
}
Here is the input when i press the button to play the melody
C
Protocol=NEC Address=0x0 Command=0xC Raw-Data=0xF30CFF00 32 bits LSB first
1
here is the code it gives when i press it again after the melody
0
Protocol=UNKNOWN 14 bits (incl. gap and start) received
other button
Received noise or an unknown (or not yet enabled) protocol
rawData[28]:
-893200
+ 450,- 300
+ 300,- 200 + 50,- 50 + 50,- 50 + 50,- 400
+ 50,- 50 + 150,- 200 + 250,- 250 + 100,- 50
+ 50,- 50 + 50,- 50 + 50,- 100 + 50,-1900
+ 500
Sum: 5800
heres my circuit
After i press any button it gives relatively the same result, but the values are always different. it doesnt go back to normal until i reset the board. im using the IRremote library by shirriff, z3t0, and ArminJo. pitches.h just defines some values for the notes.
*** UPDATE ***
I found an ugly way to make it work, by resetting it every time the melody is played, however i wanted an indicator to known when it was done resetting, so i set it to play a short tone when it came on. However it gives the same error codes just after it plays the tone, so im almost positive its either the passive buzzer or the tone function thats causing the problem. just not sure exactly why or how