Resetting millis

For expanding the time to 4 days

const int _1000_milli_per_second;
const int _3600secs_per_hour;
const int _24hours_per_day;
const unsigned long _4days_millisecs = 4 * _1000_milli_per_second * _3600secs_per_hour * _24hours_per_day;

As already suggested for such longs times like 4 days
for better long-term precision a real time clock RTC is better suited

to reduce the number of variables that must be checked if 4 days have passed by
you can calculate minute of day

minuteOfDay = hours * 60 + minutes;
secondOfDay = hours * 3660 + minutes * 60 + seconds;

and for counting 4 days I would use an extra variable counting up seconds

if (lastRTC_second != RTC_Second) {
  lastRTC_second = RTC_Second;
  _4DaysSecondsCounter++;
}

if  (_4DaysSecondsCounter >= 345600 ) {
  _4DaysSecondsCounter = 0;
// change boolean variable for 1st / 2nd-4-day-period
}
1 Like

Thats fantastic, thank you. I'm fascinated every time I learn something about how to write this code.

Thank you, going to study this for days :smiley:

4UL

const unsigned long _4days_millisecs = 4UL * _1000_milli_per_second * _3600secs_per_hour * _24hours_per_day;

:thinking:

I was trying to add in a long, as in runs for 4 days, then swicthes for 4 days, then goes back to the original 4days indefinitely.

// 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 mylongTimer;
//unsigned long myShortTimer;
const unsigned long myInterval = 4000;
const unsigned long myFulltime = 8000;

boolean first4Days;
boolean second4Days;
boolean longDays;

void setup() {
  Serial.begin(115200);
  Serial.println("Setup-Start");
  pinMode(led1Pin, OUTPUT);
  pinMode(led2Pin, OUTPUT);

  my4DayTimer = millis();
  myPrintInfoTimer = millis();
  mylongTimer = millis();
  first4Days = true;
  second4Days = false;
  longDays = 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();
  }

  if (TimePeriodIsOver(mylongTimer, myFulltime) == true) {
    flip_longdays();
  }

  // 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;
    longDays = false;
    //Serial.println("change to SECOND 4 days code");
    Serial.println();
    digitalWrite(led1Pin, LOW);
  } else {
    first4Days = true;
    second4Days = false;
    longDays = false;
    //Serial.println("change to first 4 days code");
    Serial.println();
    digitalWrite(led2Pin, LOW);
  }
}

void flip_longdays();
{
  first4Days = false;
  second4Days = false;
  longDays = true;
  //Serial.println("change");
  Serial.println("long days");
}

get stuck here


  if (TimePeriodIsOver(mylongTimer, myFulltime) == true) {
    flip_longdays();
  }

think about what the above does when day is just the # of days that have elapsed.

dividing by 4 indicates the Nth group of 4 days and the (modulus) %2 indicates whether its odd or even, 1 or 0 which can be used to determine which of the 2 values to use

1 Like

I have a question
did you run the simulation?

You seem to have not yet understood

My demo code does

EXACTLY

runs for 4 days part 1

then switches

switches for 4 days part 2

4 days code-part-1
4 days code-part-2
4 days code-part-1
4 days code-part-2
4 days code-part-1
4 days code-part-2
4 days code-part-1
4 days code-part-2
4 days code-part-1
4 days code-part-2
4 days code-part-1
4 days code-part-2

can you see that the code already switches between
part 1
part 2
part 1
part 2
part 1
part 2
part 1
part 2
part 1
part 2
??
With real dates starting with today 21.01.24
day 1: 21.01.24 part 1
day 2: 22.01.24 part 1
day 3: 23.01.24 part 1
day 4: 24.01.24 part 1

4 days over switching to part 2

day 5: 25.01.24 part 2
day 6: 26.01.24 part 2
day 7: 27.01.24 part 2
day 8: 28.01.24 part 2

8 days over switching to part 1

day 9: 29.01.24 part 1
day 10: 30.01.24 part 1
day 11: 31.01.24 part 1
day 12: 01.02.24 part 1

12 days over switching to part 2

...

going on in this pattern

next question:
is the pattern as described above the pattern that you want?

You added a long to my code.
For doing what?
The democode does already switch between two different parts !
Did you understand it?

what is your intention for this line of code

Your question is faaar to unprecise to be understandable

Please write down what your code shall do
write it down by many lines where each line represents a day

here is an example
There is a pattern in Yellow
4 days part 1
4 days part 2

then there is a pattern in pink
4 days part 1
8 days part 2

You should write down your wanted pattern as detailed as this example
With such poor words like

is

NOT

understandable

Yea i dont know what im doing, just making it up.

I did run the code.

Trying to get;

first 4 days
second 4 days
first 4 days
first 4 days
first 4 days
first 4 days
first 4 days
first 4 days
....

How I work too...

1 Like

murphyslaww69

The original wording of murphy's law is:

if something can go wrong it will go wrong some day.

I hope you are only tinkering with small watering systems outside in your garden
and that you do not tinkering with Lipos or main AC-voltage or even gas in your house

I do not even understand what you want to say with that.

For getting

If you are as lazy as that my support shrinks down to

you don't need "blinking all the time"
you need a one-shot timer
in combination with a boolean variable
secondPartNotYetDone

go tinkering finding it yourself how this will be coded

You showed way too less own effort with your answer
good luck with your "project"

2 Likes

How is that lazy?
that's all im trying to do.

i dont know code, im not a computer guy. Just want to make thing work

You refuse to learn coding.
maybe this picture sequence is better understandable for you

Ive spent every waking hour for the last month learning this stuff.

Sounds like you have invested 50 to 80 hours.
So it seems to be very in-effective how you tried to learn coding.

If you want to know the basics do yourself a favor and invest maybe 10 hours.
Where these 10 hours include tinkering and experimenting with small demo-codes

Do you understand
small
demo-codes
where these demo-codes have nothing to do with your real project but are easy to understand.

You seem to have chosen this way
learn basic baking by making this wedding cake

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

it's an ambitious program

but to more easily handle different dewPoint targets values without duplicating a lot of code requires restructuring the existing code, as well as efficiently recognizing 4 day intervals

It didnt start so ambitious, just got a bit out of control, I used to look at the code and knew what everything did.
Now im learning what it does more.

Up until trying to change the dewpoint after 4 days it was pretty easy.

I have about 30 different versions of code, everytime i try something new i save a different copy.

It occurred to me I don't need to be trying to time it.

I could just use a button to start the one off 4 day cycle whenever i needed then revert back to the old cycle?

you mean a button to toggle between 2 target values?