Arduino sleep mode 1 button only

I'm trying to put my arduino on sleep with the same button but it's not working out.
Since I'm using interrupts, the source is independent so when i press the button i automatically wake and sleep it up.
I placed a delay() after pressing the button to enter the sleep mode, it works since it's on the loop function. But when i use the delay inside the interrupt it doesn't work out and it goes on and of right away :frowning:

This is my attempt to reset it after the button on pin 2 (with a pullup resistor) gets pressed again (still doesn't work)

#include <avr/interrupt.h>
#include <avr/sleep.h>

boolean ledon = false; //debug only

void setup() {
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(2, INPUT);

  digitalWrite(13, HIGH);
  
  Serial.begin(9600);
}

boolean in_sleep = false;
boolean on_off_pressed = false;

void sleep(){
  in_sleep = true;
  digitalWrite(13, LOW);
  attachInterrupt(0, wake, LOW);
  
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  
  //1\8 de 16Mhz
  CLKPR = 0x80;
  CLKPR = 0x08;
}

void wake(){
  asm volatile ("  jmp 0");
}

void loop() {
  if (digitalRead(2)== LOW){
    delay(1000);
    sleep();
  }
}

IIRC delay() uses millis() and as millis() is not updated when the UNO is in interrupt code ==> it will not work.

furthermore the way you wake up is to reset the Arduino.

Check this tutorial about sleep from Nick Gammon - http://www.gammon.com.au/forum/?id=11497 -

Sorry, didn't read the code.

But:

If you want to "sleep" and wake with one button, then you need to fully debounce the button in both the press and release - you only sleep after the release has been fully debounced, not the press.

Easy when you think it all through ... :smiley:

I got it done, i'll sharing my source:

#include <avr/interrupt.h>
#include <avr/sleep.h>

void setup() {
  pinMode(13, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  
  for(int i = 0; i<4; i++){
    digitalWrite(9, HIGH);
    delay(50);
    digitalWrite(9, LOW);
    delay(50);
  }

  digitalWrite(13, HIGH);

  Serial.begin(9600);
}

boolean in_sleep = false;
boolean on_off_pressed = false;

void sleep(){
  in_sleep = true;
  digitalWrite(13, LOW);

  //1\8 de 16Mhz
  CLKPR = 0x80;
  CLKPR = 0x06;

  for (byte i = 0; i <= A7; i++){
    if(i == 2)
      continue;

    pinMode (i, OUTPUT);
    digitalWrite (i, LOW);
  }
  
  // disable ADC
  ADCSRA = 0;

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  
  // turn off brown-out enable in software
  MCUCR = bit (BODS) | bit (BODSE);
  MCUCR = bit (BODS); 
  //sleep_cpu ();
  
  interrupts();
  attachInterrupt(0, wake, LOW);

  sleep_mode();
  sleep_disable(); 
  detachInterrupt(0);
}

void wake(){
  in_sleep = false;
  
  //1\8 de 16Mhz
  CLKPR = 0x80;
  CLKPR = 0x01;
  
  //Internal Reset
  asm volatile ("  jmp 0");

  /*digitalWrite(13, HIGH);
  delay(500);
  */
  
  //delay(5);
  noInterrupts();
  
}

void loop() {
  if (digitalRead(2)== LOW && in_sleep == false){
    Serial.println("Sleep!");
    delay(300);
    sleep();
  }
}

BTW, something is not right, my arduino out of sleeping consumes 30.4mA and on sleeping 24.1mA.
Might this be because it's a Chinese replica, or i have something wrong?
(I'm using arduino Pro mini v3)

tsunamy_boy:
BTW, something is not right, my arduino out of sleeping consumes 30.4mA and on sleeping 24.1mA.
Might this be because it's a Chinese replica, or i have something wrong?
(I'm using arduino Pro mini v3)

If it's an unlicensed clone, then yes something is wrong, but also see:
http://forum.arduino.cc/index.php?topic=164146.msg1232507#msg1232507

Thank you, yeh it looks like it makes sense now.
Is there a way to put the arduino on sleep with another IC specifically for that?

tsunamy_boy:
Thank you, yeh it looks like it makes sense now.
Is there a way to put the arduino on sleep with another IC specifically for that?

I must not understand your question. The ATmega328P microcontroller in the Arduino Pro Mini is specifically designed to sleep. In fact, it has six different sleep modes and at least as many wake-up sources. See section 10 in the datasheet.

Yes but my Chinese replica doesn't do the job, so i might need another IC to do the work

tsunamy_boy:
Yes but my Chinese replica doesn't do the job...

How so?

Because i keep getting 20 something mA on sleep mode. :frowning:

tsunamy_boy:

[quote author=Jack Christensen link=topic=237668.msg1710628#msg1710628 date=1399338155]

tsunamy_boy:
Yes but my Chinese replica doesn't do the job...

How so?

Because i keep getting 20 something mA on sleep mode. :frowning:
[/quote]

Sounds like you didn't read the thread I linked in reply #4 above.

I did, and i tried your source, the amp are the same that i had previously, so that's why i need another solution

tsunamy_boy:
I did, and i tried your source, the amp are the same that i had previously, so that's why i need another solution

I think some folks have successfully removed the voltage regulator and power-on LED from the Pro Mini to minimize supply current while sleeping.

Ok, i'll try that, thanks have a nice day