TouchSensor Sleep+Wakeup

Hello so I'm about 75% there (I Think) I have a touchsensor connected to pin 2 and the standard ground and % volt connected to the arduino uno. My question is when I start the code it runs the function "Wakeupnow" fine and when I touch the sensor it puts the arduino to sleep perfectly fine. The issue i'm running is when I wake the arduino back up it runs "wakeupnow" once then goes immediately to the Loop() function then goes right back to sleep.

Anyway I can have it so when it wake up it runs the wakeupNow() function only without going immediatly to the second function (Loop() ) which puts it back to sleep? Here is the code

#include <avr/sleep.h>

int wakePin = 2; // pin used for waking up

void wakeUpNow() // here the interrupt is handled after wakeup
{
Serial.println("running code");
delay(100);
}

void setup()
{
Serial.begin(9600);
pinMode(wakePin, INPUT);
Serial.begin(9600);
attachInterrupt(0, wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
digitalWrite(led, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(led, LOW);
}

void loop()
{
Serial.println("work");
delay(1000);
sleepNow();
}

void sleepNow() // here we put the arduino to sleep
{
Serial.println("sleep");
delay(200);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(0,wakeUpNow, RISING); // use interrupt 0 (pin 2) and run function
sleep_mode(); // here the device is actually put to sleep!!
sleep_disable(); // first thing after waking from sleep:
wakeUpNow();
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
}

The Arduino will run loop() all the time, it's supposed to run loop() all the time. This code will put it to sleep about a second after you start up the device, whether you touch the sensor or not.

Solution: have your ISR (wakeUpNow()) set a flag, indicating you want to go to sleep. Then have loop() react to that flag.

And another thing: DON'T try to print from an ISR. That is not sure to work, and generally a bad thing to do from an ISR. Set a flag here and have loop() do the print() command based on it.

Don't forget to set all global variables that the ISR writes to as volatile.

Thank you for your replay I truly appreciate it. So I modified it where it is behaving a lot better. The main goal here is to run off a battery and have the arduino turn itself off and on with the button. Would this serve to put the arduino to sleep effieciently?

#include <avr/sleep.h>
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>

const int buttonPin = 2;     // the number of the pushbutton pin
boolean buttonWasLow = false;         // variable flag for when the pushbutton goes low

//int wakePin = 2; // pin used for waking up
SoftwareSerial mySerial(9, 10); // RX, TX

void wakeUpNow() // here the interrupt is handled after wakeup
{
 Serial.println("waking up now");
}

void setup(){
Serial.begin(9600);
//pinMode(wakePin, INPUT);
mySerial.begin (9600);
pinMode(buttonPin, INPUT);    // initialize the pushbutton pin as an input:
mp3_set_serial (mySerial);  //set softwareSerial for DFPlayer-mini mp3 module 
mp3_set_volume (30); 
delay(100);
}

void loop()
{
    // read the state of the pushbutton and set a flag if it is low:
    if (digitalRead(buttonPin) == LOW)  {
        buttonWasLow = true;
        wakeUpNow();
    }

    // This if statement will only fire on the rising edge of the button input
    if (digitalRead(buttonPin) == HIGH && buttonWasLow)  {
        // reset the button low flag
        buttonWasLow = false;
        sleepNow();
    }
}

void sleepNow() // here we put the arduino to sleep
{
Serial.println("sleeping now");
delay(200);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
attachInterrupt(0,wakeUpNow, RISING); // use interrupt 0 (pin 2) and run function
sleep_mode(); // here the device is actually put to sleep!!
sleep_disable(); // first thing after waking from sleep:
wakeUpNow();
detachInterrupt(0); // disables interrupt 0 on pin 2 so the
}

I'd have to consult the docs on Arduino's sleep.

More consideration:

  • LEDs that are still lit.
  • you don't put the mp3 player to sleep (I think it has a sleep mode).
  • check the power consumption of the touch sensor itself - by nature they're active sensors, or they can't sense the touch.