I'm working on a simple project with NodeMCU and IR receiver module.
The IR module detect the key and the board just print something .
My code:
const int ir_pin = 4; // NodeMCU D2
#include <IRremote.h>
const int BUTTON_POWER = 0x12;
void setup() {
Serial.begin(115200);
IrReceiver.begin(ir_pin, ENABLE_LED_FEEDBACK);
}
int decode_button(IRData data) {
if (
(data.protocol == NEC) &&
(!(data.flags & IRDATA_FLAGS_IS_REPEAT))
)
{
IrReceiver.printIRResultShort(&Serial);
return data.command;
}
}
void loop() {
if (IrReceiver.decode()) {
int button = decode_button(IrReceiver.decodedIRData);
Serial.println(button);
if (button == BUTTON_POWER) {
Serial.println("got power");
}
IrReceiver.resume();
}
}
During the work I noticed that after i created the 'decode_button' function, the board started to behaved weird and the code inside the 'if' function in 'decode_button' was always executed and printed stuff no matter what key I pressed or remote I used.
Before that everything worked fine.
After some debugging I noticed i forgot to return a value from that function when the 'if' is false, so I added 'return 0;' at the end and that's when everything worked again.
Now I enabled the warnings in the compiler, but that got me interested how is that even possible?
How can a missing return value can make the 'if' to always execute?
Output from the code above with a different remote:
Protocol=SAMSUNG Address=0x707 Command=0x7 Raw-Data=0xF8070707 32 bits LSB first
7
Protocol=SAMSUNG Address=0x707 Command=0xB Raw-Data=0xF40B0707 32 bits LSB first
11
Protocol=SAMSUNG Address=0x707 Command=0xF Raw-Data=0xF00F0707 32 bits LSB first
15
Educated guess would be that if decode_button() does not return a value then button will end up with whatever value happens to be left in RAM from the last time button was declared. As it is only a short sketch I would think button gets the same value it had from the previous time it was declared.
So to put it another way, if decode_button() does not return a value then you don't know what value button has.
In that case, the function was declared to return a String, but had no return, and the returned value was never used. Not sure what the compiler was doing, but the 2nd time the function was called the program would crash. Likely falls under "undefined behavior" from the compiler.
I suggest you make the prints inside the if a bit more specific, so print the actual values that got you there, so print data.protocol, NEC, data.flags, IRDATA_FLAGS_IS_REPEAT
I suspect they won't be what you think they are. I stress, print them inside the if.
I can't try your code as I don't have the right hardware, sorry.