0

I am working in an arduino which sleeps after 1.2 seconds of running time and wake up when receiving a signal in the pin 2, but this last part is not working. I tried with the four modes of the interrupt ( LOW, CHANGE, RISING, FALLING) and also tring to connect it to vcc and disconect it myself, depending on the mode.

The sleeping part works as wanted

#include <avr/sleep.h>

// ### pin paging ### \\

#define RLEDPIN 13       //pin for red led 
#define GLEDPIN 12       //pin for green led
#define BLEDPIN 11       //pin for blue led
#define wakePin 2        // pin used for waking up

// ### measure variables ### \\
unsigned long times;

void wakeUpNow()        // here the interrupt is handled after wakeup
{
  // execute code here after wake-up before returning to the loop() function
  // timers and code using timers (serial.print and more...) will not work here.
  // we don't really need to execute any special functions here, since we
  // just want the thing to wake up
}

void setup() 
{
  Serial.begin(115200);
  pinMode(RLEDPIN, OUTPUT);
  pinMode(GLEDPIN, OUTPUT);
  pinMode(BLEDPIN, OUTPUT);
  pinMode(wakePin, INPUT);

  attachInterrupt(0, wakeUpNow, CHANGE);
}

void sleepNow()         // here we put the arduino to sleep
{

    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here

    sleep_enable();          // enables the sleep bit in the mcucr register
                             // so sleep is possible. just a safety pin

    attachInterrupt(0,wakeUpNow, CHANGE); // use interrupt 0 (pin 2) and run function
                                       // wakeUpNow when pin 2 gets LOW

    sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

    sleep_disable();         // first thing after waking from sleep:
                             // disable sleep...
    detachInterrupt(0);      // disables interrupt 0 on pin 2 so the
                             // wakeUpNow code will not be executed
                             // during normal running time.

}

void loop() 
{

    digitalWrite(GLEDPIN, HIGH); //set green led high

  times = millis();

  if(times>=1200)
  {

    digitalWrite(GLEDPIN, LOW); //Reset the LED's
    digitalWrite(BLEDPIN, LOW);
    digitalWrite(RLEDPIN, LOW);
    sleepNow();
  }
}
  • Only a LOW interrupt will wake the chip from PWR_DOWN mode. Does your device set the pin LOW when it's activated? – Majenko Nov 2 '17 at 11:15
  • @Majenko I tried that and also having the pin 2 conected to vcc and when the arduino is sleeping putting it to ground and nothing happend, in case it was a failure of my device – Miguel Sanz Narrillos Nov 2 '17 at 11:21
  • By the way: sleep_mode() does all of sleep_enable() and sleep_disable() for you - you don't need to call them. – Majenko Nov 2 '17 at 11:36
  • @Majenko I had the code from the arduino web: code for sleep – Miguel Sanz Narrillos Nov 2 '17 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.