UNO R4 WIFI (Freenove version) - IDE 2.3.3. - Windows11
When I use a "attachInterrupt(digitalPinToInterrupt(IR_PIN)" it seems to create an interrupt.
In the following code the Serial.print produces an "1" i.e. an interrupt has been triggered even though the flag was preset to false.
if I enable the two lines:
//delay(50);
//IRflag = false;
then the printout will give me a "0" (false) however if i reduce the delay to any value less than 50 will get a "1"(true). which seems to infer that that attach instruction takes at least 40 milliseconds i.e. after the "IRflag = false;" instruction has taken place!
volatile bool IRflag = false;
void setup()
{
pinMode(IR_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(IR_PIN), IRfound, CHANGE);
}
void loop()
{
if (IRflag == true)
{
detachInterrupt(digitalPinToInterrupt(IR_PIN)); // I detach interrupt toprevent multiple interrupts during receipt of code
if (IrReceiver.decode())
{
//
// general code to analyse the decoded value from the IR Receiver
//
}
else if ((temp==0x1EA10000) // temp is decoded value from IR Receiver
{
do
{
IRflag = false;
attachInterrupt(digitalPinToInterrupt(IR_PIN), IRfound, CHANGE);
//delay(50);
//IRflag = false;
//
// additional testing code
//
Serial.println(IRflag);
if (IRflag == true)
{
break;
}
} while (testingflag == false);
}
}
}
void IRfound()
{
IRflag = true;
}
Any computer/microcomputer/microcontroller will report an interrupt if one is pending at the time the software link is established. Please tell us what is attached to the pin and what is it doing at the time you are powering the UNO. Did you try powering the UNO before powering whatever is attached
The pin is attached to an Infra-Red 3-pin device and receives signals from either a simple numeric pad or (for longer range) a standard TV controller.
There is no interrupt pending at the time I do the re-attaching as it is waiting for me to press a button on the pad.
(From the code you will see that I detach the pin to stop multiple interrupts
when the IR device is receiving the code - I then reattach to receive the next code when I press a button on the pad)
It was NOT bought from Amazon. Amazon doesn't sell clone Arduino boards. There are many other sellers who use the Amazon platform to sell their clone Arduino boards, but those are third party sellers. And there are LOTS of them selling LOTS of different things. Saying you bought it on Amazon doesn't tell anyone anything. WHO did you buy it from on Amazon? What was the product you actually bought?
You checked the flag in the register to make sure? It's usually a good idea to clear it anyway just to make sure. Sometimes it gets set during startup and an interrupt is just hanging out there waiting for you to enable it.
Please see Post 15 onwards of my post "Checking the voltage of a chain of 5 AA batteries" where there is a full discussion about my board.
"You checked the flag in the register to make sure? " as an Arduino Newbie I haven't got to the detailed hardware info yet - at the moment I am relying on the IDE.
You may be right as I have discovered if I put a short delay of 50ms after decoding the IR input and before the "detachInterrupt" the problem disappears.
I have just discovered that "IRremote.hpp" documentation states that it has a IRS (Interrupt Service Routine) so I must be really making a mess of things using a separate Interrupt routine.
Would have helped a lot if you had mentioned that in the beginning. Next time please post a complete piece of code, not just the part where YOU think the problem is. Put the pin numbers and libraries and all of that.