Very Long Specific Time Intervals

Hi everyone. I am currently building a arduino controlled machine which include dc motor, heating element and a exhaust fan.
Heating element will be controlled by temperature sensor and relay. I'd like to heat up my machine on three different phases. For example like
in the first pahse, the heater will be on up to 45C and when temp sensor read over 45C, relay will cut off heater. Then the temperature
will fall gradually and when the sensor read some amount of lower limit like 35C, heter will be on again. I'd like to go this stage up to 3 hours then
I'll move to next phase. Second pahse is also similar to first phase but temperature will be a little higher like up to 55C and relay cut off heater and turn
on again when reach lower limit. This stage also go on up to 3 hours duration. Third phase also similar. I've some difficulties with using amount of time. If I will use millis, the number can be big a lot which is approximately (108000000). So I think that would be inappropriate to use And. I'd also like to stop machine after 48 hours. Is there any alternative ways to handle very long duration of time?

Use millis() for up to 50 days. Large number or not, that is not of any interest.

Three hours is: 1000UL * 60UL * 60UL * 3UL.
1000 for milliseconds to seconds
60 for seconds to minutes
60 for minutes to hours
The trick is not to calculate the result of that.
No one cares what the actual number is, as long as the code shows what is going on.

const unsigned long threeHours = 1000UL * 60UL * 60UL * 3UL;
1 Like

but if the millis() time was counting hours and a counter was incremented every hour different events can be done on a hourly basis and when the count reaches 48 do the stop thing.

unsigned long millisHour = 3600000;
unsigned long currentmillistime=millis();
int hourcount.

voided loopy()
{
if ( millis()-currentmillistime >= millisHour ) 
{
hourcount++;
currentmillistime=millis();
}

if( hourcount == 1 )
{
do the 1 hour thing
}

if( hourcount == 2 )
{
do the 2 hour thing
}

if( hourcount == 48 )
{
do the 48 hour thing
}





}

or use

switch( hourcount )
{
case 1:

do things
break;

and so on and so forth



}

Real world will see the temperature continue on up for a time, and then move down. Will this be a problem?

So you want the heater on until 45°C and the heater off until 35°C for three hours followed by the heater on until 55°C and the heater off until 35°C for three hours... Every three hours the temperature range changes until you reach 48 hours and the heater turns off?

That's 16 intervals of three hours each. You could use a table of high limits and low limits.

(EDIT: Full sketch)

const byte LM35DZPin = A0;
const byte HeaterRelayPin = 4;
const unsigned long ThreeHours = 3ul * 60ul * 60ul * 1000ul;

const int ThreeHourCycles = 16; // 48 hours total
const int HighLimits[ThreeHourCycles] =
{
  45, 55, 45, 55, 45, 55, 45, 55,
  45, 55, 45, 55, 45, 55, 45, 55
};

const int LowLimits[ThreeHourCycles] =
{
  35, 45, 35, 45, 35, 45, 35, 45,
  35, 45, 35, 45, 35, 45, 35, 45,
};

int getTemperature()
{
  int adcValue = analogRead(LM35DZPin);
  return (adcValue * 500ul) / 1024;
}

void TurnOnHeater()
{
  digitalWrite(HeaterRelayPin, LOW);
}

void TurnOffHeater()
{
  digitalWrite(HeaterRelayPin, HIGH);
}

void setup()
{
  pinMode(HeaterRelayPin, OUTPUT);

  for (int cycle = 0; cycle < ThreeHourCycles; cycle++)
  {
    int highLimit = HighLimits[cycle];
    int lowLimit = LowLimits[cycle];
    
    unsigned long cycleStartTime = millis();
    while (millis() - cycleStartTime <= ThreeHours)
    {
      if (getTemperature() <= lowLimit)
        TurnOnHeater();
      if (getTemperature() >= highLimit)
        TurnOffHeater();
    }
  }
  // Finished all cycles
  TurnOffHeater();
}

void loop() {}

What is the device for?

What do you mean bro? If relay turn off the heater, there will be no heat add up and temperature will be cool down.

1 Like

I am testing electric composter to turn kitchen wastes into compost

Thank you for your code. What I want to be is to turn off heater at 45°C and turn on again at 35°C. This on and off process will be repeating up to 3 hours and after 3 hours the temperature upper and lower limit will increase to like 55 and 45°C and then it will also go for another 3 hours.

Thank you for your help. I'll try it out.

Thank you very much for your code.

Time. TIme to begin to cool, the heater is busy trying to heat and all takes time to radiate the existing heat.

That covers the first 6 hours of your 48-hour cycle. Now you just have to decide what temperatures limits you want for the remaining 14 intervals.

int HighLimits[16] = 
{
  45, 55, ??, ??, ??, ??, ??, ??, 
  ??, ??, ??, ??, ??, ??, ??, ??
};
int LowLimits[16] = 
{
  35, 45, ??, ??, ??, ??, ??, ??, 
  ??, ??, ??, ??, ??, ??, ??, ??
};

I have problems running the following code. if runtime is over two minutes, I'd like to stop first function which is light1() and run another function, light2(). But both function are taking action even after 2 minutes. How do I stop light1() and allow light2() function after passing particular duration?

#define ledR 7

#define ledG 10

unsigned long operateT = 0;

const long Delay1 = 1000UL*30UL;

const long Delay2 = 1000UL*30UL;

unsigned long two_minutes = 1000UL*60UL*2UL;

void setup() {

// put your setup code here, to run once:

pinMode(ledR, OUTPUT);

pinMode(ledG, OUTPUT);

}

void loop() {

if (millis() - operateT <= two_minutes){

light1();

operateT = millis();

}

else if (millis() - operateT > two_minutes) {

light2();

operateT = millis();

}

}

void light1() {

digitalWrite(ledR, HIGH);

digitalWrite(ledG, LOW);

delay(5000);

digitalWrite(ledR, LOW);

digitalWrite(ledG, HIGH);

delay(5000);

digitalWrite(ledR, LOW);

digitalWrite(ledG, LOW);

delay(Delay1);

}

void light2() {

digitalWrite(ledR, HIGH);

digitalWrite(ledG, HIGH);

delay(5000);

digitalWrite(ledR, LOW);

digitalWrite(ledG, LOW);

delay(Delay2);

}

Is this relared to your other topic Very Long Specific Time Intervals ?

Arh yes but this one is another problem

you need a way to remember which state you are in (light1 or light2). You only have 2 states so a bool could do but an enum would get you ready for further expansion if you want more states

the code architecture could look something like the below: (totally untested, typed here)

const byte ledR =  7;
const byte ledG = 10;

const unsigned long twoMinutes = 1000UL * 120UL; // 2 minutes

enum {light1State, light2State} state = light1State;

unsigned long operateT = 0;

void light1() {

}

void light2() {

}

void setup() {
  pinMode(ledR, OUTPUT);
  pinMode(ledG, OUTPUT);
  operateT = millis();
}

void loop() {
  switch (state) {
    case light1State:
      if (millis() - operateT <= twoMinutes) {
        light1();
      } else {        // it's time to switch
        operateT = millis();
        state = light2State;
      }
      break;

    case light2State:
      if (millis() - operateT <= twoMinutes) {
        light2();
      } else {        // it's time to switch
        operateT = millis();
        state = light1State;
      }
      break;
  }
}

PS/ please use code tags when posting code ((also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

Please keep questions about the same project in the same topic as the context of one part can be relevant to the new question.

Thank you

1 Like

Thank you very much for your code. It works.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.