Go Down

Topic: Perform time-based task once only, at set times per hour, every hour (Read 351 times) previous topic - next topic

brice3010

It must be nice to be psychic.
It helps.. and a good glass of wine is a start  :smiley-lol:

I didn't say alarms would necessarily be easier but all it would require is an alarm interrupt every minute. The code in loop() would look almost the same except that the alarm must be reset and there's no need to test if minute == lastMinute.


Pete
And you would need a library?

el_supremo

You've already got functions to read and set the time in the RTC. A little bit of browsing through the DS3231 datasheet would show you how to set and reset the alarms. Or you could google for code to do it.
Or use a library. No big deal.

Pete
Don't send me technical questions via Private Message.

larryd

So by the same token, if I have task A to be performed once at 3 instances per hour, is the following correct?
Code: [Select]

if((taskAflag == true && minute == 10) || (taskAflag == true && minute == 20) || (taskAflag == true && minute == 40))
{
taskAflag = false; //disable, do only one taskA
. . .
}

if((taskAflag == false && minute == (10+1)) || (taskAflag == false && minute == (20+1)) || (taskAflag == false && minute == (40+1)))
{
taskAflag = true; //enable taskA for next time
}


The good thing about the Arduino platform is you can test things out ;)

Sketch to check out algorithm.
Code: [Select]

bool taskAflag = true;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}
void loop()
{
  for (int x = 0; x < 6000; x++)
  {
    if (taskAflag == true && (x == 1000 ||  x == 2000  ||  x == 4000 ))
    {
      taskAflag = false; //disable, do only one taskA
      //call the task
      task(x);
      //. . .
    }

    if (taskAflag == false && (x == 1001 ||  x == 2001 ||  x == 4001 ))
    {
      taskAflag = true; //enable taskA for next time
    }
  }
}// END of loop()

void task(int x)
{
  //print test time
  Serial.println(x);
  //toggle LED
  digitalWrite(13, !digitalRead(13));
  delay(200);
  digitalWrite(13, !digitalRead(13));
}


Edit, updated code.
No technical PMs.
The last thing you did is where you should start looking.

brice3010

The good thing about the Arduino platform is you can test things out ;)

Sketch to check out algorithm.
Code: [Select]

bool taskAflag = true;

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}
void loop()
{
  for (int x = 0; x < 6000; x++)
  {
    if (taskAflag == true && (x == 1000 ||  x == 2000  ||  x == 4000 ))
    {
      taskAflag = false; //disable, do only one taskA
      //call the task
      task(x);
      //. . .
    }

    if (taskAflag == false && (x == 1001 ||  x == 2001 ||  x == 4001 ))
    {
      taskAflag = true; //enable taskA for next time
    }
  }
}// END of loop()

void task(int x)
{
  //print test time
  Serial.println(x);
  //toggle LED
  digitalWrite(13, !digitalRead(13));
  delay(200);
  digitalWrite(13, !digitalRead(13));
}


Edit, updated code.
Hi larryd, thanks for your input; from what I read you do not use the DS3231? Or am I confused?

larryd

The example I gave is a quick and dirty way for checking the algorithm.

Yes I do, exclusively, use the DS3231 RTCs.

They are much much better than the DS1307.

You can change the 'x' in the sample sketch to 'minute' or whatever.

If you test the code out, you should find it does what you what you asked.


No technical PMs.
The last thing you did is where you should start looking.

Go Up