How to create a time swich function?

@PeteKnight first of all, thank you for your help, i now understand the mathematic issue of the code :slight_smile:

i put it above void setup.

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V6); //AirflowDuration

  Blynk.syncVirtual(V7); //AirflowDelay

  timer.setInterval((airflowdelay*1000L)+(airflowduration*1000L), Airflow);
}
void setup()
{

I also changed the declartion from long to int

I changed it now to:
send sensor data to the esp every 20 seconds,
recieve data from the cloud ervery 60 seconds,
send the sensor data from the esp to the cloud every 20 seconds.

BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(10*1000L, CCS811);               //runs void CCS811 every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataRecive);     //runs void sensorDataRecive every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataSend);       //runs void sensorDataSend every X*1000ms

I still get into a reboot loop :confused:

I found something out:

int airflowduration =5 ;
int airflowdelay = 5 ;

if I go for this one, it starts fine, but the int airflowduration and airflowdelay won’t change, even after cloud handshake. So they stay at value 5 and 5 instead of syced values, 27 and 16. In they serial motior they get right displayed:

"Serial Montitor"
19:24:55.257 ->         Airflow - duration: 27sec
19:24:55.342 ->         Airflow - delay: 16min

When I try the code without an value for the int, it keeps rebooting. This does make sense to me, becuase their is no value, equals its 0, equals reebot. Did I understand that right?

int airflowduration;
int airflowdelay;

I don’t think you understood what I was saying.
Your variables are declared as global at the top of your code.
Having any variable type prefix to these variables inside a function re-declares the variable as local, and it’s locally re-defined copy is only visible inside that function.

You must totally remove the int prefix within these BLYNK_WRITE function, not replace the int prefix with a different prefix, as that once again re-declares the variable as local.

Pete.

I hope I fixed that now…
airflowdelay instead of int airflowdelay

BLYNK_WRITE(V7)
{
  airflowdelay = param.asInt();
  Serial.print("        Airflow - delay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  Serial.println(" ");
  Serial.println(" "); 
  Serial.println(" ");
  Serial.println("--------------------------------------");
  Serial.println(" "); 

airflowduration instead of int airflowduration

BLYNK_WRITE(V6)
{     
  airflowduration = param.asInt();
  Serial.print("        Airflow - duration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}

I noticed that this Timer here does nothing, so they fans stay “off”. in reality they go on and instanty off again. I have no clue why that timer adds no delay. The light from the relays go on and instantly off. And in the serial monitor it also says:

21:52:48.098 ->       <>AIRFLOW ON FOR 10SEC<>
21:52:48.098 ->       <>AIRFLOW OFF FOR 21SEC<>

so there is no delay between on and off. both happens on second 48. The code where the timer is located is right below.

void Airflow()        //swiches Fans on and off for a certain time

  {

    digitalWrite(27, HIGH);     //swiches fan1 on

    digitalWrite(12, HIGH);     //swiches fan2 on

                    Serial.println(" ");

                    Serial.print("      <>AIRFLOW ON FOR ");

                    Serial.print(airflowduration);

                    Serial.println("SEC<>");

    timer.setTimeout(airflowdelay*1000, []() {    //delay between swichting fans on and off. int "fiveseconds" should be replaced with int "airflowduration"airflowduration

    digitalWrite(27, LOW);     //swiches fan1 off

    digitalWrite(12, LOW);     //swiches fan1 off

                    Serial.print("      <>AIRFLOW OFF FOR ");

                    Serial.print(airflowdelay);

                    Serial.println("SEC<>");

                    Serial.println(" ");

                    });  

  }

TBH, it’s almost impossible for me to make any sense of your sketch based on these snippets of code and serial output, especially as you seem to be mixing-up your air flow DURATION and DELAY variables…

I’d suggest that you post your full sketch (preferabley without all of the whacky double-spacing and indentations, and this stuff…

so that it’s actually readable.

But, before you do that, you should add some serial print statements into each of your BLYNK_WRITE(vPin) callback functions which make it clear which function the message is being printed from, and the value of the incoming datastream.

Pete.

Did so sir!
Here is the full code, as wished, i removed the //////////////// etc.
Thanky you for helping me, i really need to get this project working!

int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;

//Controlls TestLED on Pin12
BLYNK_WRITE(V100)
{
  Serial.print("->BLYNK_WRITE(V3)");
  int pinValue = param.asInt();
  digitalWrite(12,pinValue);
}

//Controlls Humidity on Pin14
BLYNK_WRITE(V4)
{
  Serial.print("->BLYNK_WRITE(V4)");
  Serial.println(" ");
  Serial.println("Blynk custom settings:");
  Serial.println(" ");
  int humiditycontrol = param.asInt();
  Serial.print("        RH: ");
  Serial.print(humiditycontrol);
  Serial.print("%");
   if(humiditycontrol>humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
    digitalWrite(14, LOW);
}

//Controlls Temperature on PinX       !To enanble this funtion, assign new pin and remove //
BLYNK_WRITE(V5)
 {
  Serial.print("->BLYNK_WRITE(V5)");
  int temperaturecontrol = param.asInt();
  Serial.print("        Temp: ");
  Serial.print(temperaturecontrol);
  Serial.println("°C");
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}

//Sets value for airflowduration from the cloud and prints it in serial monitor
BLYNK_WRITE(V6)
{     
  Serial.print("->BLYNK_WRITE(V6)");
  airflowduration = param.asInt();
  Serial.print("        Airflow - duration: ");
  Serial.print(airflowduration);
  Serial.println("sec");
}

//Sets value for airflowdelay from the cloud and prints it in serial monitor
BLYNK_WRITE(V7)
{
  Serial.print("->BLYNK_WRITE(V7)");
  airflowdelay = param.asInt();
  Serial.print("        Airflow - delay: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  Serial.println(" ");
  Serial.println(" "); 
  Serial.println(" ");
  Serial.println("--------------------------------------");
  Serial.println(" "); 
}

//swiches Fans on and off for a certain time
void Airflow()
  {
    digitalWrite(27, HIGH);     //swiches fan1 on
    digitalWrite(12, HIGH);     //swiches fan2 on
                    Serial.println(" ");
                    Serial.print("      <>AIRFLOW ON FOR ");
                    Serial.print(airflowduration);
                    Serial.println("SEC<>");
    timer.setTimeout(airflowduration*1000, []() {    //delay between swichting fans on and off. int "fiveseconds" should be replaced with int "airflowduration"airflowduration
    digitalWrite(27, LOW);     //swiches fan1 off
    digitalWrite(12, LOW);     //swiches fan1 off
                    Serial.print("      <>AIRFLOW OFF FOR ");
                    Serial.print(airflowdelay);
                    Serial.println("SEC<>");
                    Serial.println(" ");
                    });  
  }
  

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay 
  timer.setInterval((airflowdelay*1000+airflowduration*1000), Airflow);
}
void setup()
{
   //ccs811 start
  Serial.begin(9600);
  Serial.println("CCS811 test");
  if(!ccs.begin())
  {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
  // Wait for the sensor to be ready
  while(!ccs.available());
  hdc1080.begin(0x40);
//ccs811 end

  Serial.begin(115200);
  pinMode(12, OUTPUT);    //Pin 12 is an Output pin
  pinMode(14, OUTPUT);    //Pin 14 is an Output pin
  pinMode(27, OUTPUT);    //Pin 27 is an Output pin

  BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(10*1000L, CCS811);               //runs void CCS811 every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataRecive);     //runs void sensorDataRecive every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataSend);       //runs void sensorDataSend every X*1000ms
}

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  //Blynk.syncVirtual(V6); //AirflowDuration
  //Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

void sensorDataSend()
{
  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);
}

void CCS811()
{
      Serial.println("Sensor CJMCU 8118 meassurements:");
      Serial.println(" ");
      Serial.print("        RH=");
      Serial.print(hdc1080.readHumidity());
      Serial.print("%        Temp=");
      Serial.print(hdc1080.readTemperature());
      Serial.println("°C");

  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("        CO2: ");
      Serial.print(ccs.geteCO2());
      Serial.print("ppm     TVOC: ");
      Serial.println(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }

} 

void loop()
 {
  BlynkEdgent.run();
  timer.run(); 
}

And the serial output that this creates when you boot the device???

Pete.

I took some time rewriting the code. It should be more organised and better to read. I also updated the serial monitor output, it makes things really bestter to understand what is going on. Thank you for that advise!
Here is the updated full code:

int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;
int humiditycontrol;
int temperaturecontrol;

//Controlls TestLED on Pin12
BLYNK_WRITE(V100)
{
  Serial.println("  ->>  BLYNK_WRITE(V3)");
  int pinValue = param.asInt();
  digitalWrite(12,pinValue);
}

//Controlls Humidity on Pin14
BLYNK_WRITE(V4)
{
  
  Serial.println("  ->>  BLYNK_WRITE(V4) - humiditycontrol");
  humiditycontrol = param.asInt();
   if(humiditycontrol>humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
    digitalWrite(14, LOW);
}

//Controlls Temperature on PinX       !To enanble this funtion, assign new pin and remove //
BLYNK_WRITE(V5)
 {
  Serial.println("  ->>  BLYNK_WRITE(V5) - temperaturecontrol");
  temperaturecontrol = param.asInt();
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}

//Sets value for airflowduration from the cloud and prints it in serial monitor
BLYNK_WRITE(V6)
{     
  Serial.println("  ->>  BLYNK_WRITE(V6) - airflowduration");
  airflowduration = param.asInt();
}

//Sets value for airflowdelay from the cloud and prints it in serial monitor
BLYNK_WRITE(V7)
{
  Serial.println("  ->>  BLYNK_WRITE(V7) - airflowdelay");
  airflowdelay = param.asInt();
}

//swiches Fans on and off for a certain time
void Airflow()
  {
    digitalWrite(27, HIGH);     //swiches fan1 on
    digitalWrite(12, HIGH);     //swiches fan2 on
    Serial.println(" ");
    Serial.print("  ->>  <>AIRFLOW is now ON for ");
    Serial.print(airflowduration);
    Serial.println("seconds<>");
    timer.setTimeout(airflowduration*1000, []() { //delay between swichting fans on and off.
    digitalWrite(27, LOW);     //swiches fan1 off
    digitalWrite(12, LOW);     //swiches fan1 off
    Serial.print("  ->>  <>AIRFLOW is now off for ");
    Serial.print(airflowdelay);
    Serial.println("sec<>");
    Serial.println(" ");
    });  
  }
  
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay 
  timer.setInterval((airflowdelay*1000+airflowduration*1000), Airflow);
}

void setup()
{
   //ccs811 start
  Serial.begin(9600);
  Serial.println("CCS811 test");
  if(!ccs.begin())
  {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
  // Wait for the sensor to be ready
  while(!ccs.available());
  hdc1080.begin(0x40);
//ccs811 end
  Serial.begin(115200);
  pinMode(12, OUTPUT);    //Pin 12 is an Output pin
  pinMode(14, OUTPUT);    //Pin 14 is an Output pin
  pinMode(27, OUTPUT);    //Pin 27 is an Output pin

  BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(10*1000L, SerialMonitor);               //runs void CCS811 every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataRecive);     //runs void sensorDataRecive every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataSend);       //runs void sensorDataSend every X*1000ms
}

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  //Blynk.syncVirtual(V6); //AirflowDuration
  //Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

void sensorDataSend()
{
  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);
}

void SerialMonitor()
{
//BLYNK_WRITE (V4)
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("------------------------------------------------------");
  Serial.println("                      Serial Monitor                  ");
  Serial.println("------------------------------------------------------");  
  Serial.println(" ");
  Serial.println("        Blynk user settings:");
  Serial.println(" ");
  Serial.print("        RH: ");
  Serial.print(humiditycontrol);
  Serial.print("%");
//BLYNK_WRITE (V5)
  Serial.print("        Temp: ");
  Serial.print(temperaturecontrol);
  Serial.println("°C");
  Serial.println(" ");
//BLYNK_WRITE (V6)
  Serial.print("        Airflow - duration set by user at: ");
  Serial.print(airflowduration);
  Serial.println("sec");
//BLYNK_WRITE (V7)
  Serial.print("        Airflow - delay set by user at: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  Serial.println(" ");  
//Sensor CJMCU 8118
  Serial.println("        Sensor CJMCU 8118 meassurements:");
  Serial.println(" ");
  Serial.print("        RH=");
  Serial.print(hdc1080.readHumidity());
  Serial.print("%        Temp=");
  Serial.print(hdc1080.readTemperature());
  Serial.println("°C");
  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("        CO2: ");
      Serial.print(ccs.geteCO2());
      Serial.print("ppm        TVOC: ");
      Serial.println(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }
  Serial.println(" ");
  Serial.println("------------------------------------------------------");
  Serial.println(" ");
  Serial.println(" ");  
} 

void loop()
 {
  BlynkEdgent.run();
  timer.run(); 
}

Here is what the serial Monitor has to say after booting and connecting:

23:52:48.614 -> [15075] Certificate OK
23:52:48.771 -> [15247] Ready (ping: 170ms).
23:52:48.945 ->   ->>  BLYNK_WRITE(V6) - airflowduration
23:52:48.945 -> [15451] CONNECTING_CLOUD => RUNNING
23:52:48.977 ->  
23:52:48.977 ->  
23:52:48.977 -> ------------------------------------------------------
23:52:48.977 ->                       Serial Monitor                  
23:52:48.977 -> ------------------------------------------------------
23:52:48.977 ->  
23:52:48.977 ->         Blynk user settings:
23:52:48.977 ->  
23:52:48.977 ->         RH: 0%        Temp: 0°C
23:52:48.977 ->  
23:52:48.977 ->         Airflow - duration set by user at: 10sec
23:52:48.977 ->         Airflow - delay set by user at: 0min
23:52:49.023 ->  
23:52:49.023 ->         Sensor CJMCU 8118 meassurements:
23:52:49.023 ->  
23:52:49.023 ->         RH=28.53%        Temp=28.06°C
23:52:49.023 ->         CO2: 1623ppm        TVOC: 265
23:52:49.023 ->  
23:52:49.023 -> ------------------------------------------------------
23:52:49.023 ->  
23:52:49.023 ->  
23:52:57.877 ->  
23:52:57.877 ->  
23:52:57.877 -> ------------------------------------------------------
23:52:57.909 ->                       Serial Monitor                  
23:52:57.909 -> ------------------------------------------------------
23:52:57.909 ->  
23:52:57.909 ->         Blynk user settings:
23:52:57.909 ->  
23:52:57.909 ->         RH: 0%        Temp: 0°C
23:52:57.909 ->  
23:52:57.909 ->         Airflow - duration set by user at: 10sec
23:52:57.909 ->         Airflow - delay set by user at: 0min
23:52:57.909 ->  
23:52:57.909 ->         Sensor CJMCU 8118 meassurements:
23:52:57.909 ->  
23:52:57.909 ->         RH=28.44%        Temp=28.13°C
23:52:57.945 ->         CO2: 1774ppm        TVOC: 418
23:52:57.945 ->  
23:52:57.945 -> ------------------------------------------------------

Here is what the serial moitor says after reciving the first data from the cloud:

23:53:32.736 -> ------------------------------------------------------
23:53:32.736 ->                       Serial Monitor                  
23:53:32.736 -> ------------------------------------------------------
23:53:32.736 ->  
23:53:32.736 ->         Blynk user settings:
23:53:32.736 ->  
23:53:32.736 ->         RH: 38%        Temp: 27.3°C
23:53:32.736 ->  
23:53:32.736 ->         Airflow - duration set by user at: 10sec
23:53:32.736 ->         Airflow - delay set by user at: 20min
23:53:32.736 ->  
23:53:32.736 ->         Sensor CJMCU 8118 meassurements:
23:53:32.775 ->  
23:53:32.775 ->         RH=28.74%        Temp=28.06°C
23:53:32.775 ->         CO2: 1707ppm        TVOC: 346
23:53:32.775 ->  
23:53:32.775 -> ------------------------------------------------------
23:53:32.775 ->  
23:53:32.775 ->  
23:53:40.803 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol
23:53:40.803 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol
23:53:40.803 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol
23:53:40.929 ->  
23:53:40.929 ->   ->>  <>AIRFLOW is now ON for 10seconds<>
23:53:40.929 ->   ->>  <>AIRFLOW is now off for 20sec<>
23:53:40.929 ->  
23:53:40.929 ->  

You aren’t showing in the serial monitor the airflowduration value that has come from Blynk.

TBH, your SerialMonitor function adds nothing as far as I’m concerned, I’d like to see values at the point of processing, not a report.

Pete.

Here we go :slight_smile: Is that output that what you meant?

00:33:46.964 -> [117117] CONNECTING_NET => CONNECTING_CLOUD
00:33:46.964 -> [117148] Connecting to blynk.cloud:443
00:33:49.141 -> [119316] Certificate OK
00:33:49.219 -> [119359] Ready (ping: 41ms).
00:33:49.358 ->   ->>  BLYNK_WRITE(V6) - airflowduration - value=10
00:33:49.403 -> [119565] CONNECTING_CLOUD => RUNNING
00:33:49.450 ->   ->>  BLYNK_WRITE(V7) - airflowdelay - value=20
00:33:54.227 ->  
00:33:54.227 ->  
00:33:54.227 -> ------------------------------------------------------
00:33:54.227 ->                       Serial Monitor                  
00:33:54.227 -> ------------------------------------------------------
00:33:54.227 ->  
00:33:54.227 ->         Blynk user settings:
00:33:54.227 ->  
00:33:54.227 ->         RH: 0%        Temp: 0°C
00:33:54.227 ->  
00:33:54.227 ->         Airflow - duration set by user at: 10sec
00:33:54.227 ->         Airflow - delay set by user at: 20min
00:33:54.227 ->  
00:33:54.227 ->         Sensor CJMCU 8118 meassurements:
00:33:54.273 ->  
00:33:54.273 ->         RH=30.32%        Temp=27.91°C
00:33:54.273 ->         CO2: 844ppm        TVOC: 67
00:33:54.273 ->  
00:33:54.273 -> ------------------------------------------------------
00:33:54.273 ->  
00:33:54.273 ->  
00:33:54.466 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=38
00:33:54.544 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=26 
00:33:59.392 ->   ->>  <>AIRFLOW is now ON for 10seconds<>
00:34:04.477 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=38
00:34:04.556 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=26 
00:34:09.408 ->   ->>  <>AIRFLOW is now ON for 10seconds<>
00:34:09.408 ->   ->>  <>AIRFLOW is now off for 20sec<>
00:34:09.408 ->  
00:34:14.226 ->  
00:34:09.408 ->  
00:34:14.226 ->  
00:34:14.226 ->  
00:34:14.226 -> ------------------------------------------------------
00:34:14.226 ->                       Serial Monitor                  
00:34:14.226 -> ------------------------------------------------------
00:34:14.226 ->  
00:34:14.226 ->         Blynk user settings:
00:34:14.226 ->  
00:34:14.226 ->         RH: 38%        Temp: 26°C
00:34:14.226 ->  
00:34:14.226 ->         Airflow - duration set by user at: 10sec
00:34:14.226 ->         Airflow - delay set by user at: 20min
00:34:14.226 ->  
00:34:14.226 ->         Sensor CJMCU 8118 meassurements:
00:34:14.271 ->  
00:34:14.271 ->         RH=30.43%        Temp=27.93°C
00:34:14.271 ->         CO2: 1270ppm        TVOC: 132
00:34:14.271 ->  
00:34:14.271 -> ------------------------------------------------------
00:34:14.271 ->  
00:34:14.271 ->  
00:34:14.458 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=38
00:34:14.536 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=26 
00:34:19.409 ->   ->>  <>AIRFLOW is now ON for 10seconds<>
00:34:19.409 ->   ->>  <>AIRFLOW is now off for 20sec<>
00:34:19.409 ->  
00:34:24.434 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=38
00:34:24.527 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=26 
00:34:29.399 ->   ->>  <>AIRFLOW is now ON for 10seconds<>
00:34:29.399 ->   ->>  <>AIRFLOW is now off for 20sec<>
00:34:29.399 ->  

Impossible to say without seeing the code that produced the output.

I suggest that you go back to your original code and add-in serial prints to the BLYNK_WRITE() functions as the SerialMonitor function isn’t helping.

Pete.

int humidity;
int temp;
int co2;
int TVOC;
int airflowduration;
int airflowdelay;
int humiditycontrol;
int temperaturecontrol;

//Controlls TestLED on Pin12
BLYNK_WRITE(V100)
{
  int pinValue = param.asInt();
  Serial.print("  ->>  BLYNK_WRITE(V3) - value=");
  Serial.println(pinValue);
  digitalWrite(12,pinValue);
}

//Controlls Humidity on Pin14
BLYNK_WRITE(V4)
{
  humiditycontrol = param.asInt();
  Serial.print("  ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=");
  Serial.println(humiditycontrol);
   if(humiditycontrol>humidity)
  {
    digitalWrite(14, HIGH);
  }
  else
    digitalWrite(14, LOW);
}

//Controlls Temperature on PinX       !To enanble this funtion, assign new pin and remove //
BLYNK_WRITE(V5)
 {
  temperaturecontrol = param.asInt();
  Serial.print("  ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=");
  Serial.print(temperaturecontrol);
  //if (temperaturecontrol > temp) 
  //{
   // digitalWrite(15, HIGH);
  //} 
  //else
    //digitalWrite(15, LOW);
}

//Sets value for airflowduration from the cloud and prints it in serial monitor
BLYNK_WRITE(V6)
{     
  airflowduration = param.asInt();  
  Serial.print("  ->>  BLYNK_WRITE(V6) - airflowduration - value=");
  Serial.println(airflowduration);
}

//Sets value for airflowdelay from the cloud and prints it in serial monitor
BLYNK_WRITE(V7)
{
  airflowdelay = param.asInt();
  Serial.print("  ->>  BLYNK_WRITE(V7) - airflowdelay - value=");
  Serial.println(airflowdelay);

}

//swiches Fans on and off for a certain time
void Airflow()
  {
    digitalWrite(27, HIGH);     //swiches fan1 on
    digitalWrite(12, HIGH);     //swiches fan2 on
    Serial.println(" ");
    Serial.print("  ->>  <>AIRFLOW is now ON for ");
    Serial.print(airflowduration);
    Serial.println("seconds<>");
    timer.setTimeout(airflowduration*1000, []() { //delay between swichting fans on and off.
    digitalWrite(27, LOW);     //swiches fan1 off
    digitalWrite(12, LOW);     //swiches fan1 off
    Serial.print("  ->>  <>AIRFLOW is now off for ");
    Serial.print(airflowdelay);
    Serial.println("seconds<>");
    Serial.println(" ");
    });  
  }
  
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V6); //AirflowDuration
  Blynk.syncVirtual(V7); //AirflowDelay 
  timer.setInterval((airflowdelay*1000+airflowduration*1000), Airflow);
}

void setup()
{
   //ccs811 start
  Serial.begin(9600);
  Serial.println("CCS811 test");
  if(!ccs.begin())
  {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while(1);
  }
  // Wait for the sensor to be ready
  while(!ccs.available());
  hdc1080.begin(0x40);
//ccs811 end
  Serial.begin(115200);
  pinMode(12, OUTPUT);    //Pin 12 is an Output pin
  pinMode(14, OUTPUT);    //Pin 14 is an Output pin
  pinMode(27, OUTPUT);    //Pin 27 is an Output pin

  BlynkEdgent.begin();    //runs Blynk Connection Liberary
  timer.setInterval(20*1000L, SerialMonitor);               //runs void CCS811 every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataRecive);     //runs void sensorDataRecive every X*1000ms
  delay(200);
  timer.setInterval(10*1000L, sensorDataSend);       //runs void sensorDataSend every X*1000ms
}

void sensorDataRecive()
{                                            
  Blynk.syncVirtual(V4); //HumidityControl
  Blynk.syncVirtual(V5); //TemperatureControl
  //Blynk.syncVirtual(V6); //AirflowDuration
  //Blynk.syncVirtual(V7); //AirflowDelay
  Blynk.syncVirtual(V8); //CO2 Control
  humidity=hdc1080.readHumidity()+3;
  temp=hdc1080.readTemperature()-3.6;
  co2=ccs.geteCO2();
  TVOC=ccs.getTVOC();
}

void sensorDataSend()
{
  Blynk.virtualWrite(V0, humidity);
  Blynk.virtualWrite(V1, temp);
  Blynk.virtualWrite(V2, co2);
  Blynk.virtualWrite(V3, TVOC);
}

void SerialMonitor()
{
//BLYNK_WRITE (V4)
  Serial.println(" ");
  Serial.println(" ");
  Serial.println("------------------------------------------------------");
  Serial.println("                      Serial Monitor                  ");
  Serial.println("------------------------------------------------------");  
  Serial.println(" ");
  Serial.println("        Blynk user settings:");
  Serial.println(" ");
  Serial.print("        RH: ");
  Serial.print(humiditycontrol);
  Serial.print("%");
//BLYNK_WRITE (V5)
  Serial.print("        Temp: ");
  Serial.print(temperaturecontrol);
  Serial.println("°C");
  Serial.println(" ");
//BLYNK_WRITE (V6)
  Serial.print("        Airflow - duration set by user at: ");
  Serial.print(airflowduration);
  Serial.println("sec");
//BLYNK_WRITE (V7)
  Serial.print("        Airflow - delay set by user at: ");
  Serial.print(airflowdelay);
  Serial.println("min");
  Serial.println(" ");  
//Sensor CJMCU 8118
  Serial.println("        Sensor CJMCU 8118 meassurements:");
  Serial.println(" ");
  Serial.print("        RH=");
  Serial.print(hdc1080.readHumidity());
  Serial.print("%        Temp=");
  Serial.print(hdc1080.readTemperature());
  Serial.println("°C");
  if(ccs.available()){
    if(!ccs.readData()){
      Serial.print("        CO2: ");
      Serial.print(ccs.geteCO2());
      Serial.print("ppm        TVOC: ");
      Serial.println(ccs.getTVOC());
    }
    else{
      Serial.println("ERROR!");
      while(1);
    }
  }
  Serial.println(" ");
  Serial.println("------------------------------------------------------");
  Serial.println(" ");
  Serial.println(" ");  
} 

void loop()
 {
  BlynkEdgent.run();
  timer.run(); 
}

I have a serial print for all BLYNK_WRITE() functions.

So does this give you the information you need to track-down and solve your issue(s) ?

Pete.

I have no clue tbh :confused:

i just want that those timers get those values from blynk and done :expressionless:
Day three on solving that issue :face_with_monocle:
Do you have any Ideas what could be the issue?

No, because you aren’t providing me with serial data that tells me what I need to know.

Pete.

what exactly do you mean with serial data? Do you mean the Output from the Serial Monitor?

In that serial monitor output it says:

01:07:20.918 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=38
01:07:21.006 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=26
01:07:21.086 ->   ->>  BLYNK_WRITE(V6) - airflowduration - value=10
01:07:21.156 ->   ->>  BLYNK_WRITE(V7) - airflowdelay - value=20
01:07:26.989 ->  
01:07:26.989 ->   ->>  <>AIRFLOW is now ON for 10seconds<>
01:07:26.989 ->   ->>  <>AIRFLOW is now off for 20seconds<>
01:07:26.989 ->  
01:07:30.947 ->   ->>  BLYNK_WRITE(V4) - humiditycontrol  - value=38
01:07:31.017 ->   ->>  BLYNK_WRITE(V5) - temperaturecontrol - value=26
01:07:31.087 ->   ->>  BLYNK_WRITE(V6) - airflowduration - value=10
01:07:31.167 ->   ->>  BLYNK_WRITE(V7) - airflowdelay - value=20

what i dont understand why there is no time delay between
->> <>AIRFLOW is now ON for 10seconds<> and ->> <>AIRFLOW is now off for 20seconds<>. in the code there is a timer function in between those serial prints.
( timer.setTimeout(airflowduration*1000, []() {...});)
full void with the timer:

void Airflow()
  {
    digitalWrite(27, HIGH);     //swiches fan1 on
    digitalWrite(12, HIGH);     //swiches fan2 on
    Serial.println(" ");
    Serial.print("  ->>  <>AIRFLOW is now ON for ");
    Serial.print(airflowduration);
    Serial.println("seconds<>");
    timer.setTimeout(airflowduration*1000, []() { //delay between swichting fans on and off.
    digitalWrite(27, LOW);     //swiches fan1 off
    digitalWrite(12, LOW);     //swiches fan1 off
    Serial.print("  ->>  <>AIRFLOW is now off for ");
    Serial.print(airflowdelay);
    Serial.println("seconds<>");
    Serial.println(" ");
    });  
  }

To be 100% correct, it says the value of the airflowduraton is 10. so the script should wait 10*1000ms before going on and switching it off again…

Please stop with the snippets of code and serial monitor text. It’s all or nothing in terms of me being able to make any sense of the data.

Pete.

what kind of data should I exactly upload? I also can send you the whole project in a zip folder if you want. I do anything you sugest me to do. This is my first time using a forum for coding. I am sorry :frowning:

Can’t you simply use

timer.setTimeout(1000, []() {

Or

timer.setTimeout(duration, []() {

Instead?