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
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?
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
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
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.
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.