arduino sleep wake ds3231

hello, putting the arduino to sleep and waking it back up I am able to do. I have asked this question prior however it was inside some other questions and I still haven't found out the answer or a tutorial that explains whether or not it is even possible.

my arduino is set to SLEEP at 5am, it is set to WAKE at 7pm. it works fine if you power it up before 7pm. if I power it up at 7:01 it has to wait a full 24 hours to pick up the next WAKE interrupt. in other words it doesn't know it is "supposed" to be awake.

does anyone know if it is possible to correct this. meaning at "what ever time of day" I turn it on it can tell whether it is supposed to be "asleep" or "awake". state machine has been suggested and I have reviewed SWITCHSTATE. but haven't been able to conclude a solution due to the variable of what time it "might" be turned on.

once it gets to a known state it's fine.

thanks for any input.

The answer depends completely on how you implemented it.

I see. How would one implement a power down-power up in a fashion that the arduino could determine which state it should be in if it is turned on at any given time?

Fredric58:
I see. How would one implement a power down-power up in a fashion that the arduino could determine which state it should be in if it is turned on at any given time?

By saving the desired state in a variable.

Why not check if it is >= 7:00pm and < 5:00 am and then do whatever it supposed to do? That way if you miss 7:00 exactly you're still gonna perform.

Thank you, because I didn't know I could do that with the RTC. I will experiment

ok, I experimented with several options and here is what I found

 if (t.hour >= OnHour && t.hour <= OffHour) {
    digitalWrite(Led, HIGH);
    Serial.println("LIGHT ON");
  }

  [/code

this actually works in that it doesn't matter what time I turn the arduino on, if it isn't between the time slot it won't go on. my question is why won't it turn on off? I have added the following else statements believing the clock would check time and turn the light off.

1. "else" digitalWrite(Led, LOW);//light stays on

2.[code]else

 if (t.hour <= OnHour && t.hour >= OffHour) {
    digitalWrite(Led, LOW);
    Serial.println("LIGHT OFF");//light stays on
  }

  [/code

//and one that simply says

.[code]else

 if (t.hour == OffHour) {
    digitalWrite(Led, LOW);
    Serial.println("LIGHT OFF");//light stays on
  }

  [/code

if you need the whole thing let me know, it isn't very long

Thanks. puzzled

I don't see how your code is turning th LED on at all.

if (t.hour >= OnHour && t.hour <= OffHour) {

Whether or not this works properly depends upon what value OnHour and OffHour are declared to be.
if Onhour is 19 and OffHour is 5, there's no value of t.hour which will make that condition true. If t.hour is >= 19 then it cannot also be <= 5.
If OnHour is greater than OffHour (as in your case where you turn something on overnight) then you need the condition:

if (t.hour >= OnHour || t.hour <= OffHour) {

but if OnHour < OffHour (to turn something on during the day e.g. between 10am and 8pm) you need:

if (t.hour >= OnHour && t.hour <= OffHour) {

Pete

i'll need to look up the ll condition, in the mean time here's is what I am using to turn it on

#include <DS3231.h>
#include <Wire.h>

int Led = 4;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 9;   //24 hr clock
const int OnMin = 00;
const int OffHour = 10;
const int OffMin = 00;

void setup() {
  Serial.begin(115200);
  rtc.begin();
  pinMode(Led, OUTPUT);
  digitalWrite(Led, LOW);
}

void loop() {
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (5000);

  if (t.hour >= OnHour && t.hour <= OffHour) {
    digitalWrite(Led, HIGH);
    Serial.println("LIGHT ON");//this works in that if you turn it on at any time it can
                                           // determine if it should be on or off
  }

  else  {
    if (t.hour <= OnHour && t.hour >= OffHour)        // I thought this would be the opposite 
    digitalWrite(Led, LOW);                                       //side of "on" using the clock 
    Serial.println("LIGHT OFF");
  }
  }

In that example you have OnHour < OffHour so the condition:

 if (t.hour >= OnHour && t.hour <= OffHour) {

will work.

But this:

 else  {
    if (t.hour <= OnHour && t.hour >= OffHour)        // I thought this would be the opposite
    digitalWrite(Led, LOW);                                       //side of "on" using the clock
    Serial.println("LIGHT OFF");
  }

won't work as well. For a start, you don't need "else { if". The "else" already covers the situation where (t.hour >= OnHour && t.hour <= OffHour) is false.
Try:

 if (t.hour >= OnHour && t.hour <= OffHour) {
    digitalWrite(Led, HIGH);
    Serial.println("LIGHT ON");
  } else  {
    digitalWrite(Led, LOW);
    Serial.println("LIGHT OFF");
  }

Pete

BTW. In this code:

 else  {
    if (t.hour <= OnHour && t.hour >= OffHour)

you have the same situation as I mentioned in my original post. There is no value of t.hour which can be less than or equal to OnHour (9) and at the same be greater than or equal to OffHour (10).

Pete

 if (t.hour >= OnHour && t.hour <= OffHour) {
    digitalWrite(Led, HIGH);
    Serial.println("LIGHT ON");
  } else  {
    digitalWrite(Led, LOW);
    Serial.println("LIGHT OFF");
  }

From the OP's post #6

ok, I experimented with several options and here is what I found

if (t.hour >= OnHour && t.hour <= OffHour) {
digitalWrite(Led, HIGH);
Serial.println("LIGHT ON");
}

this actually works in that it doesn't matter what time I turn the arduino on, if it isn't between the time slot it won't go on. my question is why won't it turn on off? I have added the following else statements believing the clock would check time and turn the light off.

  1. "else" digitalWrite(Led, LOW);//light stays on

Only the op knows what exact syntax he used for his snippet.

1 of the original snippets. that didn't work, but note new change "off" hour and comment

#include <DS3231.h>
#include <Wire.h>

int Led = 4;

DS3231  rtc(SDA, SCL);
Time t;

const int OnHour = 9;
const int OnMin = 00;
const int OffHour = 11;     // now can be greater than or less than because 10 is greater than 9
                                       // and 10  is less than 11?
const int OffMin = 00;

void setup() {
  Serial.begin(115200);
  rtc.begin();
  pinMode(Led, OUTPUT);
  digitalWrite(Led, LOW);
}

void loop() {
  t = rtc.getTime();
  Serial.print(t.hour);
  Serial.print(" hour(s), ");
  Serial.print(t.min);
  Serial.print(" minute(s)");
  Serial.println(" ");
  delay (5000);

  if (t.hour >= OnHour && t.hour <= OffHour) {
    digitalWrite(Led, HIGH);
    Serial.println("LIGHT ON");
  }

  else  {

    digitalWrite(Led, LOW);
    Serial.println("LIGHT OFF");
  }
}

it's in those small details. I'm new to this and appreciate ya'lls input. let me test and will post in a couple hours. Thanks and Merry Christmas

for an unknown reason the LED is still on at 4:00pm. and if I turn the arduino off then on the LED is still on? what am I missing??

  • are you using the code you posted in #12?
  • are you sure that the RTC is set to the right time?
  • are you using 24 hour clock or am/pm on the RTC?
  • does this code turn the LED off?
int Led = 4;
void setup(void)
{
  Serial.begin(115200);
  pinMode(Led, OUTPUT);
  digitalWrite(Led, LOW);
}

void loop(void)
{
}
  • do you have a resistor in series with the LED? If so, what value?

Pete

yes using code in #12
yes right time
on 24 hours
no it does not turn the LED off (not the code you posted but the code o am using)
resistor is 330 ohm

Does the code I posted turn the LED off?
For that matter, I suppose you could just load the blink example and see if that flashes the LED on and off.

Pete

The Point being missed is this. I can turn the LED on at 7 PM and turn it off at 5 AM. That's not an issue. The issue is,

If you turn the Arduino on at 6 PM it turns the LED on at 7 PM, pay attention to the times. If you turn the Arduino on at 8 PM, it has to wait till 7 PM the next day to turn on the LED.

There are two tasks the Arduino is to perform in this project, the first one is to turn on at 7 PM and run a script. The second one is to turn off, at 5 AM and go in to power down mode.

The challenges, you never know what time the Arduino will be turned on. So it has to recognize whether it needs to be in the work mode or the sleep mode.

This is why you can't simply turn on the LED on at seven and turn it off at 5 AM. The Arduino has to be able to figure out what time it is in and what state it needs to be in. The two states are asleep, or awake. It gets even more involved from there.

question about state machines. they all have a starting point. they can either start as ON or OFF or WAIT etc. then move to STATE B or C or D depending on the conditions.

is it possible to have a conditional starting point? i can't find anything that even comes close in examples.

the time the arduino is actually turned on is the variable. the arduino can be turned on during the day or during the night. before it can move to another STATE it has to determine what time it is first.

what do i use to accomplish that if it is actually possible?

Fredric58:
question about state machines. they all have a starting point. they can either start as ON or OFF or WAIT etc. then move to STATE B or C or D depending on the conditions.

is it possible to have a conditional starting point? i can't find anything that even comes close in examples.

the time the arduino is actually turned on is the variable. the arduino can be turned on during the day or during the night. before it can move to another STATE it has to determine what time it is first.

what do i use to accomplish that if it is actually possible?

A conditional starting point would actually mean having a single starting point with additional conditional state changes.