I have a badly behaved ISR and no idea why. The interrupt is attached at the beginning of a function and is supposed to trigger when pin 3 goes from low to high. The pin is reading low when the function is entered because it can only be entered if this is the case.
However, the first time through, it runs the interrupt. If I leave the switch down so the pin is low, it will not run it again, but every time the interrupt runs, it runs a second time, and I don't know why.
Any help would be appreciated.
Relevant code:
void loop ()
{
currenttime = millis();
if (digitalRead(safety)==LOW) //safety is on Pin 3
{
if (digitalRead(LED)==HIGH)
{
//stuff
if (digitalRead(start)==LOW)
{
digitalWrite(LED,LOW);
robot();
}
}
}
void robot()
{
//set interrupt to trigger
attachInterrupt(1,safetyISR,RISING);
//does stuff
detachInterrupt(1);
//digitalWrite(reset,LOW);
}
void safetyISR()
{
detachInterrupt(1);
wdt_reset(); //reset board
wdt_enable(WDTO_60MS);
}
Why are you continually detaching and reattaching the interrupt handler?
Relevant code
I'm calling bulls**t on this. If you KNOW that this is the relevant code, then fix your problem. If you don't KNOW that this is the relevant code, post all of it.
The reason I keep attaching and detaching the ISR trigger is because I don't want it to run in loop() but I do want it to run in robot()
That makes little sense, you either need the ISR active all the time or you don't need an ISR at all and you can pole for the condition in the robot() function.
Anyway all the ISR seems to do is to detach an interrupt. (Well it did when I first wrote this comment) This is a bad thing to do inside an ISR.
My loop program blinks an LED, and during that point, I don't want the ISR to run and reset the board. When the robot is running, I want to reset it if the cover is removed, so that's why I detach the ISR.
How would you poll for the change?
AWOL,
I'm just using that right now to test the program. All the Serial.println commands will be removed in the final program; they're just they're for troubleshooting.
They are likely to be the cause of trouble. It's like hunting for a gas leak with a flame and saying it's only for fault finding when there is no gas leak I will not have the flame.
Where could I put that in my code since I want it to be checking constantly during robot()?
Mike,
I tried taking that serial command out, and it still triggered when it wasn't supposed to. I also replace a possibly offending switch with a wire, so it's not a connection problem.