Why does my 24 hours loop ,not work?

I am currently engaged in the development of a bioreactor for cultivating algae. However, I have encountered an issue where my 24-hour loop is not functioning as intended. Despite my efforts to diagnose the problem, I have been unable to identify the root cause after days of investigation. Would you be so kind as to lend your expertise and examine the bioreactor code ? Your assistance would be greatly appreciated and hold significant value to me.

Situation sketch
Please accept my apologies for not having provided a clear explanation of the situation from the outset. To clarify, the following sequence of events occurred:

  1. The code was successfully uploaded to the Arduino.
  2. The Arduino was disconnected from the PC after being plugged in to the wall.
  3. For approximately five hours, the code operated without issue.
  4. The code ceased functioning correctly at a certain point, specifically after executing the next piece of code.
    if (Teltijd - Starttijd1 >= 25200000 && Teltijd - Starttijd1 <= 39600000) {
  5. At this juncture, the Arduino ceased functioning properly and for instance, the LED panel and pump stay on.
#include <EEPROM.h>
#include <Arduino.h>
template <class T> int EEPROM_writeAnything(int ee, const T& value) {
  const byte* p = (const byte*)(const void*)&value;
  unsigned int i;
  for (i = 0; i < sizeof(value); i++)
    EEPROM.write(ee++, *p++);
  return i;
}

const unsigned long DayIntterval = 86400000;
const unsigned long AlgenMeetingInterval = 1800000;
const unsigned long PompjeIntervalNacht = 3600000;
const unsigned long RustInterval = 7200000;
unsigned long Starttijd1 = 0;
unsigned long Starttijd2 = 0;
unsigned long Starttijd3 = 0;
unsigned long Starttijd4 = 0;
int RelayLedPanel = 7;
int RelayAirPump = 6;
int AlgaeConcentrationIndicator = 2;
int LDR1 = A0;
int LDR2 = A1;
int LDR3 = A2;
int addr = 0;
float versturen;


void setup() {
  Serial.begin(9600);
  pinMode(AlgaeConcentrationIndicator, OUTPUT);
  pinMode(RelayAirPump, OUTPUT);
  pinMode(RelayLedPanel, OUTPUT);
  pinMode(LDR1, INPUT);
  pinMode(LDR2, INPUT);
  pinMode(LDR3, INPUT);
  digitalWrite(RelayLedPanel, LOW);
  delay(2000);
  digitalWrite(RelayLedPanel, HIGH);
  delay(1000);
  digitalWrite(RelayLedPanel, LOW);
  delay(2000);

}

void loop() {
  unsigned long Teltijd = millis();
  if (Teltijd - Starttijd1 <= DayIntterval) {
    if (Teltijd - Starttijd1 <= 14400000) {
      if (Teltijd - Starttijd4 <= RustInterval) {
        digitalWrite(RelayLedPanel, HIGH);
        digitalWrite(RelayAirPump, HIGH);
        if (Teltijd - Starttijd2 <= AlgenMeetingInterval) {
          Algenmeeting();
          Starttijd2 = Teltijd;
        }
      }
      else {
        digitalWrite(RelayLedPanel, LOW);
        digitalWrite(RelayAirPump, LOW);
        delay(1200000);
        Starttijd4 = Teltijd;
      }
    }

    if (Teltijd - Starttijd1 >= 14400000 && Teltijd - Starttijd1 <= 25200000) {
      digitalWrite(RelayLedPanel, LOW);
      if (Teltijd - Starttijd3 <= PompjeIntervalNacht) {
        digitalWrite(RelayAirPump, HIGH);
        delay(1200000);
        digitalWrite(RelayAirPump, LOW);
      }
      else {
        Starttijd3 = Teltijd;
      }
    }

    if (Teltijd - Starttijd1 >= 25200000 && Teltijd - Starttijd1 <= 39600000) {
      if (Teltijd - Starttijd4 <= RustInterval) {
        digitalWrite(RelayLedPanel, HIGH);
        digitalWrite(RelayAirPump, HIGH);
        if (Teltijd - Starttijd2 <= AlgenMeetingInterval) {
          Algenmeeting();
          Starttijd2 = Teltijd;
        }
      }
      else {
        digitalWrite(RelayLedPanel, LOW);
        digitalWrite(RelayAirPump, LOW);
        Serial.println("UIT2 , UIT2"); delay(500);
        delay(1200000);
        Starttijd4 = Teltijd;
      }
    }

    if (Teltijd - Starttijd1 >= (39600000) && Teltijd - Starttijd1 <= (50400000)) {
      digitalWrite(RelayLedPanel, LOW);
      if (Teltijd - Starttijd3 <= PompjeIntervalNacht) {
        digitalWrite(RelayAirPump, HIGH);
        delay(1200000);
        digitalWrite(RelayAirPump, LOW);
        Starttijd3 = Teltijd;
      }
    }


    if (Teltijd - Starttijd1 >= (50400000) && Teltijd - Starttijd1 <= DayIntterval) {
      if (Teltijd - Starttijd4 <= RustInterval) {
        digitalWrite(RelayLedPanel, HIGH);
        digitalWrite(RelayAirPump, HIGH);
        if (Teltijd - Starttijd2 <= AlgenMeetingInterval) {
          Algenmeeting();
          Starttijd2 = Teltijd;
        }
      }
      else {
        digitalWrite(RelayLedPanel, LOW);
        digitalWrite(RelayAirPump, LOW);
        delay(1200000);
        Starttijd4 = Teltijd;
      }
    }
  }

  else {
    Starttijd1 = Teltijd;
  }
}

void Algenmeeting () {

  int meetingsaantal = 3;
  int teller = 0;
  if (teller < meetingsaantal) {
    if (teller = 0);
    delay(20000); delay(20000);
    float waarde01 = analogRead(A0);
    float waarde11 = analogRead(A1);
    float waarde21 = analogRead(A2);
    Serial.print("SENSOR 1 WAARDE 1= " ); Serial.print(waarde01); Serial.print("  SENSOR 2 WAARDE 1= " ); Serial.print(waarde11); Serial.print("  SENSOR 3 WAARDE 1= " ); Serial.println(waarde21);



    if (teller = 1);
    delay(20000); delay(20000);
    float waarde02 = analogRead(A0);
    float waarde12 = analogRead(A1);
    float waarde22 = analogRead(A2);
    Serial.print("SENSOR 1 WAARDE 2= " ); Serial.print(waarde02); Serial.print("  SENSOR 2 WAARDE 2= " ); Serial.print(waarde12); Serial.print("  SENSOR 3 WAARDE 2= " ); Serial.println(waarde22);


    if (teller = 2);
    delay(20000); delay(20000);
    float waarde03 = analogRead(A0);
    float waarde13 = analogRead(A1);
    float waarde23 = analogRead(A2);
    Serial.print("SENSOR 1 WAARDE 3= " ); Serial.print(waarde03); Serial.print("  SENSOR 2 WAARDE 3= " ); Serial.print(waarde13); Serial.print("  SENSOR 3 WAARDE 3= " ); Serial.println(waarde23);


    float  gemiddelde_algen = ((waarde01 + waarde11 + waarde02 + waarde12 + waarde03 + waarde23) / 6);
    float gemidelde_buiten = ((waarde21 + waarde22 + waarde23) / 3);
    float  constante = ((gemiddelde_algen / gemidelde_buiten) * 1000000);
    float  versturen = constante;
    EEPROM_writeAnything(addr, versturen);
    Serial.print("GEMMIDELDE ALGEN= " ); Serial.print(gemiddelde_algen); Serial.print("  GEMMIDELDE BUITEN= " ); Serial.print(gemidelde_buiten); Serial.print(" addr= " ); Serial.print(addr); Serial.print("  CONSANTE= " ); Serial.println(constante);


    addr = addr + 5;
    if (addr == EEPROM.length()) {
      addr = 0;
    }
    delay(150);

  }
  else {
    teller = 0;
  }
  return;
}

Sincerely,

Xander

Hi!

damn a bioreactor? sounds very professionnal for an hobbyist forum :wink:

Can you provide the model of arduino board you use? depending on the chip, you may be limited by the size of the data (like 16bit) wich avoid to complete your (awfull) delay(1200000); and causing overflow.

excluding the use of blocking function, there is plenty of arduino compatible RTC module. They will perfectly fit your needs for this kind of scheduling.

EDIT: considering the apparatus, a "state machine" style coding should be VERY appropriate.

Please explain what are you mean.
What the difference between your expectation and how the 24 hours loop is functioning in reality?

No, the parameter of the arduino delay() function has unsigned long type.

I have in mind: what if I ask my uno to count to 1200000 ?
do you think I am wrong?

I think Uno will do :slight_smile:
But you always can test it - 1200 000 milliseconds is only 1200 seconds or 20 minutes...

it makes a lot for a data able to range from 0 to 65535. What did I miss?

Not functioning is a way too often used description telling nothing.
Back to the drawing board, posting what that is supposed to tell.

millis() is great for short intervals, long-term time keeping, not so much. Put in a RTC and read the time until a preset is reached.

I have no idea...
You can use delay() up to 49 days long

in my mind it means "more than 16bit data", that's the point my head is banging on.
but it is late here! :grin:

Yes, but the machine does exactly NOTHING in the interval. Hard to test a ptogram with a seven week NOP and not very useful.

I think you are misunderstood the problem.
unsigned long has 32 bits, bro!

this, I got it. but not every arduino board may handle a 32bit, even if the delay works on unsigned long (so I quote the uno)

or I AM the problem :grin:

For this project I'm using a Arduino Uno, is this OK ?

Please point the arduino board which can't handle unsigned long type.
Are you trolling?
Of course Uno can

Regrettably, I am unable to precisely locate the source of the error at this time. However, it appears that the Arduino becomes unresponsive and enters a loop, continuously repeating a particular action.

Be sure I am not. sorry for letting this feeling.

you know, this kind of reference:

I really read 16bit for UNO

Please answer the questions in post#3 and #8

You don't want to tell me which one?
If you don't want to go into details, it will make it very difficult to help.
Then look for the problem yourself