BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

The use of blynk timer instead of delay

I am working on a project : “automation of a chicken hatch”
The Arduino i’m using is arduino uno wifi rev 2. When i was using blynk on my smartphone (Android), i noticed that i didn’t have connection all the time. And so i read the post about using blynk timer instead of delay. I replaced the delays with timeout timer as a Lambda function. (3 times, i marked it in the comment) I’m not sure if that is the correct way because my serial output from the loop comes way to fast and not every second as it’s supposed to.
Here is my code:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>

char auth[] = "    ";
char ssid[] = "   ";        
char pass[] = "    ";    
int LuikGoedemorgen = 500;        
int LuikWelterusten = 100;         
int LuikPulsen = 5;             
const int LuikMetingen = 5;       
const int motortin1Pin = 4;       
const int motortin2Pin = 5;       
const int magneetPin = A1;        
const int ldrPin = A2;            

int intervalteller = 0;
int Luikstatus = 1;               
int intMeetMoment = 0;            
uint16_t Licht[LuikMetingen]; 

BlynkTimer timer;

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int buttonLed = param.asInt(); 
   if (buttonLed == 1){
          digitalWrite(8, HIGH);  
          digitalWrite(9, HIGH); 
          digitalWrite(10, HIGH);                                       
        }
        else{
        digitalWrite(8, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        };
}    
BLYNK_WRITE(V2) //Button Widget is writing to pin V2
{
  int buttonLuik = param.asInt(); 
   if (buttonLuik == 1) {
     OpenLuik();
     Luikstatus = 0;
    
                                          
        };
   if (buttonLuik == 0)    {
     SluitLuik();
     Luikstatus = 1;
     
        };
}    


/*************************/
void setup(void) {
/*************************/

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Kippenluik");

  pinMode(motortin1Pin, OUTPUT);
  pinMode(motortin2Pin, OUTPUT);
  pinMode(ldrPin, INPUT);            
  pinMode(magneetPin, INPUT_PULLUP); 
  
  VullenLuik((LuikGoedemorgen + LuikWelterusten) / 2);
  Luikstatus = 0;
  intMeetMoment = 0;

  Testluik();

}

/*************************/
void Testluik(void) {
  /*************************/
  digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, HIGH);
  for (int teller = 0; teller < 5; teller++) 
  timer.setTimeout(1000L, []()                   //INSTEAD OF DELAY
  {  
   OpenLuik(); 
  }); 
  
}

/*************************/
void SluitLuik(void) {
  /*************************/
  Serial.println("");
  Serial.println("Luik sluiten");
  digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, HIGH);
  for (int teller = 0; teller < LuikPulsen; teller++) 
  // motor uit
  timer.setTimeout(1000L, []()               // INSTEAD OF DELAY
  {  
    digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, LOW);
  Luikstatus = 1;
  }); 
  
}

/*************************/
void OpenLuik(void) {
  /*************************/
  Serial.println("");
  Serial.println("Luik openen");
  while (digitalRead(magneetPin) == 0) { 
    digitalWrite(motortin1Pin, HIGH);
    digitalWrite(motortin2Pin, LOW);
  }
  
  digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, LOW);
  Luikstatus = 0;
}

/*************************/
void VullenLuik(int Waarde) {
  /*************************/
  // meetwaarde array vullen met een opgegeven waarde
  for (int teller = 0; teller < LuikMetingen; teller++) {
    Licht[teller] = Waarde;
  }
}

/*************************/
void ProcesLuik(void) {
  /*************************/

  uint16_t gemiddelde = 0;

  Licht[intMeetMoment] = analogRead(ldrPin); 
  intMeetMoment++;                           
  if (intMeetMoment >= LuikMetingen) intMeetMoment = 0; 
  gemiddelde = 0;
  for (int teller = 0; teller < LuikMetingen; teller++) {
    gemiddelde += Licht[teller];
  }
  gemiddelde = gemiddelde / LuikMetingen;

  if ((gemiddelde <= LuikWelterusten) && (digitalRead(magneetPin) == 1)) { 
    SluitLuik();
  }

  if (gemiddelde >= LuikGoedemorgen) {
    OpenLuik();
  }

}

/*************************/
void loop(void) {
  /*************************/

  Blynk.run();
  timer.run(); 
  intervalteller = 0;
  
  while (intervalteller < 60) {
    
     timer.setTimeout(1000L, []()     //INSTEAD OF DELAY
  {  
  
    intervalteller++;
    Serial.print("LDR: ");
    Serial.print(analogRead(ldrPin));
    Serial.print("  Luik: ");
    Serial.print(Luikstatus);
    Serial.print("  Magneet: ");
    Serial.print(digitalRead(magneetPin));
    Serial.print("  Array: ");
    Serial.print(Licht[0]);
    Serial.print("-");
    Serial.print(Licht[1]);
    Serial.print("-");
    Serial.print(Licht[2]);
    Serial.print("-");
    Serial.print(Licht[3]);
    Serial.print("-");
    Serial.println(Licht[4]);
   
   
    }); 
  }

  ProcesLuik();
   
}

You should be using an interval timer, not a timeout timer. Read this:

You may also want to look at the several other chicken coop projects that have been made previously.

https://community.blynk.cc/search?context=topic&context_id=53326&q=Chicken%20Coop&skip_context=true

Pete.

Are the first two delays correct then? I’ve tried to replace the third delay with the interval timer but with no success… Does the interval call the function every second all the time or only when the function is called in the loop? Because the function must only be called when ‘intervalteller<60’.
Thanks!

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>

char auth[] = "ggmqCjX3XHGDgWWfBsnJnhBUAIbsw4Fk";
char ssid[] = "";        
char pass[] = "";    
int LuikGoedemorgen = 500;        
int LuikWelterusten = 100;         
int LuikPulsen = 5;             
const int LuikMetingen = 5;       
const int motortin1Pin = 4;       
const int motortin2Pin = 5;       
const int magneetPin = A1;        
const int ldrPin = A2;            

int intervalteller = 0;
int Luikstatus = 1;               
int intMeetMoment = 0;            
uint16_t Licht[LuikMetingen]; 

BlynkTimer timer;

BLYNK_WRITE(V1) //Button Widget is writing to pin V1
{
  int buttonLed = param.asInt(); 
   if (buttonLed == 1){
          digitalWrite(8, HIGH);  
          digitalWrite(9, HIGH); 
          digitalWrite(10, HIGH);                                       
        }
        else{
        digitalWrite(8, LOW);
        digitalWrite(9, LOW);
        digitalWrite(10, LOW);
        };
}    
BLYNK_WRITE(V2) //Button Widget is writing to pin V2
{
  int buttonLuik = param.asInt(); 
   if (buttonLuik == 1) {
     OpenLuik();
     Luikstatus = 0;
    
                                          
        };
   if (buttonLuik == 0)    {
     SluitLuik();
     Luikstatus = 1;
     
        };
}    


/*************************/
void setup(void) {
/*************************/

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  Serial.println("Kippenluik");
  timer.setInterval(1000L, SerialOutput);

  pinMode(motortin1Pin, OUTPUT);
  pinMode(motortin2Pin, OUTPUT);
  pinMode(ldrPin, INPUT);            
  pinMode(magneetPin, INPUT_PULLUP); 
  
  VullenLuik((LuikGoedemorgen + LuikWelterusten) / 2);
  Luikstatus = 0;
  intMeetMoment = 0;

  Testluik();

}

/*************************/
void Testluik(void) {
  /*************************/
  digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, HIGH);
  for (int teller = 0; teller < 5; teller++) 
  timer.setTimeout(1000L, []()                   //INSTEAD OF DELAY
  {  
   OpenLuik(); 
  }); 
  
}

/*************************/
void SluitLuik(void) {
  /*************************/
  Serial.println("");
  Serial.println("Luik sluiten");
  digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, HIGH);
  for (int teller = 0; teller < LuikPulsen; teller++) 
  // motor uit
  timer.setTimeout(1000L, []()               // INSTEAD OF DELAY
  {  
    digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, LOW);
  Luikstatus = 1;
  }); 
  
}

/*************************/
void OpenLuik(void) {
  /*************************/
  Serial.println("");
  Serial.println("Luik openen");
  while (digitalRead(magneetPin) == 0) { 
    digitalWrite(motortin1Pin, HIGH);
    digitalWrite(motortin2Pin, LOW);
  }
  
  digitalWrite(motortin1Pin, LOW);
  digitalWrite(motortin2Pin, LOW);
  Luikstatus = 0;
}

/*************************/
void VullenLuik(int Waarde) {
  /*************************/
  // meetwaarde array vullen met een opgegeven waarde
  for (int teller = 0; teller < LuikMetingen; teller++) {
    Licht[teller] = Waarde;
  }
}

/*************************/
void ProcesLuik(void) {
  /*************************/

  uint16_t gemiddelde = 0;

  Licht[intMeetMoment] = analogRead(ldrPin); 
  intMeetMoment++;                           
  if (intMeetMoment >= LuikMetingen) intMeetMoment = 0; 
  gemiddelde = 0;
  for (int teller = 0; teller < LuikMetingen; teller++) {
    gemiddelde += Licht[teller];
  }
  gemiddelde = gemiddelde / LuikMetingen;

  if ((gemiddelde <= LuikWelterusten) && (digitalRead(magneetPin) == 1)) { 
    SluitLuik();
  }

  if (gemiddelde >= LuikGoedemorgen) {
    OpenLuik();
  }

}
void SerialOutput(){

    intervalteller++;
    Serial.print("LDR: ");
    Serial.print(analogRead(ldrPin));
    Serial.print("  Luik: ");
    Serial.print(Luikstatus);
    Serial.print("  Magneet: ");
    Serial.print(digitalRead(magneetPin));
    Serial.print("  Array: ");
    Serial.print(Licht[0]);
    Serial.print("-");
    Serial.print(Licht[1]);
    Serial.print("-");
    Serial.print(Licht[2]);
    Serial.print("-");
    Serial.print(Licht[3]);
    Serial.print("-");
    Serial.println(Licht[4]);
}

/*************************/
void loop(void) {
  /*************************/

  Blynk.run();
  timer.run(); 
  intervalteller = 0;
  
  while (intervalteller < 60) {

    SerialOutput();
    
   
  }

  ProcesLuik();
   
}

I don’t understand the question.

Where?

Yes, if you define the interval as 1000ms.

This code needs to come out of your void loop.

Pete.