I am having a problem with my Interrupt working randomly. I have pared back the code to just the basics. I have a touch button to Pin 2 (Int 2) (circuit looks like this). When I run the sketch it works as expected, but randomly crashes after varying wake up clicks of the button. Problem is that sometimes it works for 2 'wakes', and other times I get more, but at some pt the sketch always crashes and clicking the button will not wake it up - This can be seen by the serial monitor. I have also confirmed this using just a blinking LED and it also crashes at some random point.
#include <avr/sleep.h>
#include <avr/power.h>
int centerToggle = 2;
//switch debouncing
long debounceDelay = 20; // the debounce time; increase if the output flickers -
long lastDebounceTime = 0; // the last time the output pin was toggled
volatile int toggle = 1;
long shutoffStartMillis = 0; //Store the value to start the shutoff timer from in Milliseconds.
//**********************************
//Change this to determine how long the device stays on - in MS
long shutOffDelayMS = 5000; //In Milliseconds - Determines how long to keep LED system on (after shutoff timer clicked) in Milliseconds 10000 (10sec), 60000 (1min) 600000 (10min)
//**********************************
/***************************************************
* Name: pin2Interrupt
*
***************************************************/
void pin2Interrupt(void)
{
/* This will bring us back from sleep. */
/* We detach the interrupt to stop it from
* continuously firing while the interrupt pin
* is low.
*/
toggle = 1;
/* Delay in here as a very crude debouncing mechanism. */
delay(100);
detachInterrupt(0);
}
void enterSleep(void)
{
/* Setup pin2 as an interrupt and attach handler. */
//attachInterrupt(interrupt, function, mode)
//attachInterrupt(0, pin2Interrupt, LOW);
attachInterrupt(0, pin2Interrupt, FALLING);
delay(100);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
/* The program will continue from here. */
/* First thing to do is disable sleep. */
sleep_disable();
}
void setup()
{
Serial.begin(38400);
/* Setup the pin status. */
pinMode(centerToggle, INPUT);
}
void loop()
{
Serial.println("Still awake....");
//this will shut down the LED after the ellapsed time period. Rules - if the shutoffStartMilis has been set AND the time ellapsed is greater than the delay time. Shut off the LED
if (shutoffStartMillis != 0 && millis() - shutoffStartMillis > shutOffDelayMS) //if shutoff time has elapsed, turn the LEDs off 1000ms = 1 second
{
shutoffStartMillis = 0;
//power down the SHT11 Sensor and other ports
Serial.println("###################Entering Sleep....");
//go to sleep
delay(200);
enterSleep();
}
delay(500);
if (toggle == 1)
{
shutoffStartMillis = millis(); //get a start value to start the countdown from.
toggle = 0;
}
}