// https://forum.arduino.cc/t/resetting-millis/1213723/19
// https://wokwi.com/projects/387534707798036481
const byte led1Pin = 12;
const byte led2Pin = 2;
unsigned long my4DayTimer;
unsigned long myPrintInfoTimer;
//unsigned long myShortTimer;
const unsigned long myInterval = 4000;
boolean first4Days;
boolean second4Days;
void setup() {
Serial.begin(115200);
Serial.println("Setup-Start");
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
my4DayTimer = millis();
myPrintInfoTimer = millis();
first4Days = true;
second4Days = false;
}
void loop() {
// check if 4 days of time have passed by
if ( TimePeriodIsOver(my4DayTimer, myInterval) == true ) {
// if REALLY 4 days of time have passed by
flip_between_First_and_Second();
}
// check if boolean variable with name "first4Days" is true
if (first4Days == true) {
printFirst_4_days_message(); // does what the function name says
}
// check if boolean variable with name "second4Days" is true
if (second4Days == true) {
printSecond_4_days_message(); // does what the function name says
}
printDaysPassedBy(); // does what the functions name says
}
// easy to use helper-function for non-blocking timing
boolean TimePeriodIsOver (unsigned long & startOfPeriod, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - startOfPeriod >= TimePeriod ) {
// more time than TimePeriod has elapsed since last time if-condition was true
startOfPeriod = currentMillis; // a new period starts right here so set new starttime
return true;
}
else return false; // actual TimePeriod is NOT yet over
}
void printDaysPassedBy() {
static unsigned long myPrintInfoTimer;
if ( TimePeriodIsOver(myPrintInfoTimer, 1000) ) {
Serial.print("-day");
Serial.print( ( millis() - my4DayTimer) / 1000);
}
}
void printFirst_4_days_message() {
static unsigned long myShortTimer;
if ( TimePeriodIsOver(myShortTimer, 500) == true) {
Serial.print(" 1st");
digitalWrite(led1Pin, !digitalRead(led1Pin)); // invert LOW/HIGH by using the not-operator
}
}
void printSecond_4_days_message() {
static unsigned long myShortTimer;
if ( TimePeriodIsOver(myShortTimer, 500) == true) {
Serial.print(" 2nd");
digitalWrite(led2Pin, !digitalRead(led2Pin)); // invert LOW/HIGH by using the not-operator
}
}
void flip_between_First_and_Second() {
if (first4Days == true) {
first4Days = false;
second4Days = true;
//Serial.println("change to SECOND 4 days code");
Serial.println();
digitalWrite(led1Pin, LOW);
}
else {
first4Days = true;
second4Days = false;
//Serial.println("change to first 4 days code");
Serial.println();
digitalWrite(led2Pin, LOW);
}
}
execute code
for 1st 4 days
execute code
for 2nd 4 days