Hi,
I have searched this forum for doing two different tasks independent to each other, using interrupts on Arduino Uno, but could not find any :o
One interrupt is through watchdog timer (wdt) and another is through PIN2 (connected to a push button).
I would like to use wdt for flashing an led connected to pin9 every 24 secs, and use PIN2 interrupt to flash an led connected to pin13.
After following Nick Gammons forum, I am able to flash the led connected to the wdt interrupt
, but not the PIN2 interrupt
. Could someone please help me. I want these interrupts to be independent to each other.
Attached is my code, also copy pasted below.
#include <avr/sleep.h>
#include <avr/wdt.h> // Watchdog timer
const byte LED = 9;
const byte LED2 = 13;
ISR (WDT_vect)
{
wdt_disable(); // disable watchdog
} //
void resetWatchdog ()
{
// clear various "reset" flags
MCUSR = 0;
// allow changes, disable reset, clear existing interrupt
WDTCSR = bit (WDCE) | bit (WDE);
// set interrupt mode and an interval (WDE must be changed from 1 to 0 here)
WDTCSR = bit (WDIE) | bit (WDP3) | bit (WDP0); // set WDIE, and 8 seconds delay
// pat the dog
wdt_reset();
} // end of resetWatchdog
void wake ()
{
// cancel sleep as a precaution
sleep_disable();
interrupts (); // one cycle
// must do this as the pin will probably stay low for a while
detachInterrupt (0);
} // end of wake
void setup ()
{
resetWatchdog ();
pinMode (LED, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (2, INPUT);
digitalWrite (2, HIGH); // enable pull-up
attachInterrupt (0, wake, LOW);
} // end of setup
void loop ()
{
if (digitalRead (2) == LOW)
{
digitalWrite (LED2, HIGH);
delay (1000);
digitalWrite (LED2, LOW);
delay (1000);}
else
{pinMode (LED, OUTPUT);
digitalWrite (LED, HIGH);
delay (1000);
digitalWrite (LED, LOW);
delay (50);
}
for (int thisPin = 1; thisPin < 4; thisPin++) {
goToSleep ();
}
}
void goToSleep ()
{
set_sleep_mode (SLEEP_MODE_PWR_DOWN);
ADCSRA = 0;
noInterrupts ();
resetWatchdog ();
sleep_enable();
// turn off brown-out enable in software
// BODS must be set to one and BODSE must be set to zero within four clock cycles
MCUCR = bit (BODS) | bit (BODSE);
// The BODS bit is automatically cleared after three clock cycles
MCUCR = bit (BODS);
interrupts (); // one cycle
sleep_cpu (); // one cycle
sleep_disable ();
} // end of loop
Many thanks in advance for your help !
![]()
IMP_INTERUPT_diff3.ino (1.93 KB)