In a program I use void loop(); to call void actionTime();
In this last function I read DS3231 time to execute a task when a certain time is met. Otherwise the tasks in the void loop(); should continue to be executed.
Is it proper to use a return; function while checking if the actual time has changed from the previous time that the condition was met? Without interfering with the execution of the void loop(); ?
In other words: the void loop(); is the main loop program, void actionTime(); is a time dependent program to be executed only once if certain time conditions are met.
The purpose of the return; is to make sure that the time dependent if condition in void actionTime(); is executed only once as soon as the required time is met.
#define DS3231_I2C_ADDRESS 0x68
#include <Wire.h>
const int buttonPin1 = 12;
const int buttonPin2 = 13;
const int LED1 = 14;
const int LED2 = 16;
int ledState = HIGH;
int buttonState1;
int buttonState2;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long elapsedTime;
unsigned long startTime;
unsigned long currentTime;
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}
void setup() {
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
Serial.begin(115200); // Setup Serial Communication.
digitalWrite(LED1, ledState);
}
/**************************
R E A D T I M E
**************************/
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void actionTime() {
// do something once, using Ds3231: read time and
// act only if certain time criteria are met
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
// execute time dependent task only once
static byte lastSecond = 0; // execute time dependent task only once
if ( second == lastSecond ) {
return;
}
lastSecond = second; // remember what second was last time we looked
// second has changed... what is it now?
if (second == 01)
{
// do something once
}
}
void loop() {
currentTime = millis();
// debounce
if (currentTime - lastDebounceTime > debounceDelay) {
lastDebounceTime += debounceDelay;
buttonState1 = digitalRead(buttonPin1);
// then check the change of state
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
ledState = !ledState;
if (ledState == HIGH) {
startTime = currentTime;
}
}
lastButtonState1 = buttonState1;
}
}
digitalWrite(LED1, ledState);
// manage LED2
if (currentTime - startTime > 3000 && ledState == HIGH) {
digitalWrite(LED2, HIGH);
}
else {
digitalWrite(LED2, LOW);
}
actionTime(); // do something, time based
}