run part of a sketch for 2 days and then run the loop

Hi All, using a Leonardo & DHT22 I'm trying to set the temperature and humidity of a box at a certain level for 2 days and then set the temperature and humidity at another level thereon in.

I assume I'm in need of a run once procedure & a countdown sub-routine but so far I've not been able achieve this - any help would be appreciated.

I can read and write from the IP/OP no problems, my code is as follows;

/*
#include "DHT.h" // include library
#define DHTPIN 2 // sensor pin
#define DHTTYPE DHT22 // type of sensor
#define fan 12 // fan on/off pin
#define heater 13 // heater on/off pin

int setTemp_ff2d = 20; // target temp in Celsius for first two days
int setHum_ff2d = 30; // target humidity for first two days
int setTemp = 40; // target temp in Celsius for remaining days
int setHum = 50; // target humidity 0-100% for remaining days

DHT dht(DHTPIN, DHTTYPE); // Initialize DHT sensor

void setup(){
pinMode(fan, OUTPUT); // enable fan output
pinMode(heater, OUTPUT); // enable heater output
Serial.begin(9600); // enable serial monitor
dht.begin(); // begin sensing
}

void loop(){
labela:{ // this code is for the first 2 days
delay(2500); // wait 2.5 sec before measuring
float h = dht.readHumidity(); // read humidity
float t = dht.readTemperature(); // read temp in Celsius
if(t < setTemp_ff2d){digitalWrite(heater, HIGH);}
else{digitalWrite(heater, LOW);
if(h > setHum_ff2d){digitalWrite(fan, HIGH);}
else{digitalWrite(fan, LOW);}
Serial.print("ff2d Temperature: ");
Serial.print(t);
Serial.print((char)176);
Serial.print("C ");
Serial.print("\t ff2d Humidity: ");
Serial.print(h);
Serial.println(" %\t");
goto labelb;}

labelb:{ // this code is for the rest of the time
delay(2500); // wait 2.5 sec before measuring
float h = dht.readHumidity(); // read humidity
float t = dht.readTemperature(); // read temp in Celsius
if(t < setTemp){digitalWrite(heater, HIGH);}
else{digitalWrite(heater, LOW);}
if(h > setHum){digitalWrite(fan, HIGH);}
else{digitalWrite(fan, LOW);}
Serial.print("Temperature: ");
Serial.print(t);
Serial.print((char)176);
Serial.print("C ");
Serial.print("\t Humidity: ");
Serial.print(h);
Serial.println(" %\t");
}
goto labelb;
}
}
*/

 goto labelb;

Incoming!

Use Auto Format and you can see the code flow better.

At first glance - you have two identical blocks of code and they work (?) by itself . right?

Now is the time to put the common code into a function and than control the access to it

Function

void ReadTempAndHumidity(void)
{
you current working code goes here
....
}

Than use the function - I prefer in Setup

....

StartTime = ...
do
{
ReadTempAndHumidity();
}while ( Current Time - StartTime < 2 days ); // 2 days cycle

// after 2 days
do
{
ReadTempAndHumidity();
}while(true:); // forever cycle

You probably want to add some timing constrains , for example run ReadTempAndHumidity(); every 5 minutes or when temperature or humidity changes etc.
Your Serial.print(s) will go crazy if you do not control their execution in some time "stamps".

Keep in mind that putting code in function MAY require to change your variables "scope".
To keep things simple - use global variables for start, you can experiment with local variables later.
Have fun.

Vaclav:
Use Auto Format and you can see the code flow better.

Or . . . simply remove the "goto"

And code tags... yeah, we really lurve code tags.

Don't use goto
Don't use delay()

If you want to manage time over a period of some days you will probably need a Real Time Clock (RTC) module as the normal Arduino oscillator is not precise enough.

Have a look at how millis() is used to manage time in the demo several things at a time. The same principle can be used with an RTC.

...R

Robin2:
If you want to manage time over a period of some days you will probably need a Real Time Clock (RTC) module as the normal Arduino oscillator is not precise enough.

Not precise enough for temperature and humidity control? That would be unusual. But if power loss is a concern...

the only thing that changes in your program is at the end of day 2 the setpoints for temp and humidity change.

if you put the sensor reads in a simple timer loop using millis() then that takes care of the temp reads

do the same for the prints and have them run every 15 seconds

now you just need a one more timer loop that when false makes the set points for the first two days. When true makes the set points the ones you wish to use for the remaining days.

this is a simple timer loop

unsigned long currentMillis = millis();//used by timer

if (currentMillis - previousMillis > 2500L) {
//Do this every 2.5 seconds
  previousMillis = currentMillis;//re-start timer
}

this is not code just a concept

unsigned long currentMillis = millis();//used by 3 timers

if (currentMillis - previousMillis1 > 2500L) {//2.5 seconds
  float h = dht.readHumidity();                        // read humidity
  float t = dht.readTemperature();                          // read temp in Celsius
  if (t < tempSetPoint) {
    digitalWrite(heater, HIGH);
  }
  else {
    digitalWrite(heater, LOW);
  }
  if (h > humidtySetPoint) {
    digitalWrite(fan, HIGH);
  }
  else {
    digitalWrite(fan, LOW);
  }
  previousMillis1 = currentMillis;//reset timer for next loop
}

if (currentMillis - previousMillis2 > 15000L) {//15 mins
  Serial.print("ff2d Temperature: ");
  Serial.print(t);
  Serial.print((char)176);
  Serial.print("C   ");
  Serial.print("\t ff2d Humidity: ");
  Serial.print(h);
  Serial.println(" %\t");
  previousMillis2 = currentMillis;//reset 15mins timer
}

if (currentMillis - previousMillis3 > 172800L)//2day
{
  tempSetPoint = 40; //set global including the low level temp
  humidtySetPoint = 60; //set global e.g  int humidtySetPoint=40;
}
//no reset on timer after 2 days it will stay in this if

if you want to play with the code make sure all previousMillis, previousMillis1, previousMillis2 are unsigned long when assigned in global

also make sure any time e.g 172800 has a capital L after it so the compiler knows you are using a long.(there are 1000 millis in a second )

aarg:
Not precise enough for temperature and humidity control?

Not precise enough to measure 48 hours that is the same as the clock on the mantlepiece.

...R