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

Timer function


#1

Hello I created this sketch:

void fan()
{  if (abilitation_fan==HIGH){digitalWrite (FAN, !HIGH);   //switch on the fan
                                        analogWrite (SPEED_FAN,speed );      //setting the speed
                                          }                                                                             
   else {digitalWrite (FAN, !LOW);       //switch off the fan
         speed=speed1;}}                    //set default speed


BLYNK_WRITE(V16){                                           //TIMER 1, timer function
  if (param.asInt()==HIGH){abilitation_fan=HIGH;  
                                          speed=speed2; }              //set speed2                 
  else {abilitation_fan=LOW; }}                  


BLYNK_WRITE(V17){                                           //TIMER 2, timer function
  if (param.asInt()==HIGH){abilitation_fan=HIGH;  
                                          speed=speed3; }              //set speed2                 
  else {abilitation_fan=LOW; }}                    

The idea is switch on and off the fan at different speeds at tifferent times…

But I have problems with the timer function. Sometimes when is true the time condition of V16 or V17 the speed isn’t correct or the fan switch on and off immediatly.

If I use a simple button function instead of the timer function there isn’t problems…

Suggestions?


#2

we need all the code to help you


#3
#define BLYNK_PRINT Serial
#define FAN 22   
#define SPEED_FAN 13   
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
char auth[] = "........";
char ssid[] = ".......";
char pass[] = ".........";

#define EspSerial Serial1
#define ESP8266_BAUD 115200
BlynkTimer timer;

bool abilitation_fan;
int speed, speed1, speed2, speed3, speed4;
speed=0;
speed1=150;
speed2=200;
speed3=250;

void fan();

ESP8266 wifi(&EspSerial);

void fan()
{  if (abilitation_fan==HIGH){digitalWrite (FAN, !HIGH);   //switch on the fan
                                        analogWrite (SPEED_FAN,speed );      //setting the speed
                                          }                                                                             
   else {digitalWrite (FAN, !LOW);       //switch off the fan
         speed=speed1;}}                    //set default speed


BLYNK_WRITE(V16){                                           //TIMER 1, timer function
  if (param.asInt()==HIGH){abilitation_fan=HIGH;  
                                          speed=speed2; }              //set speed2                 
  else {abilitation_fan=LOW; }}                  


BLYNK_WRITE(V17){                                           //TIMER 2, timer function
  if (param.asInt()==HIGH){abilitation_fan=HIGH;  
                                          speed=speed3; }              //set speed2                 
  else {abilitation_fan=LOW; }}    

void setup()
{
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass, "192.168.0.108", 8080);
  pinMode(FAN, OUTPUT);
  pinMode(SPEED_FAN, OUTPUT);
  timer.setInterval(1500L, fan);

}

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

#4

@Gunner, thanks !


#5

not sure, but maybe timer is too short
try 3000 ms instead of 1500


#6

ok, i can try, but i don’t understand why it works with a button function and doesn’t works with timer function?


#7

because you can’t push the button every 1,5 secondes :yum:


#8

ok but the time selected in the timer function has a wide range… for example starts at 12.00 and stops at 14.00


#9

What widgets do you have attached to V16 & V17, and how are you expecting them to work in practice?

Why do you have void fan() declared twice?

Pete.


#10

yes I’ve declared twice, my error.
V16 and V17 are timer widgets.
the idea is to start the fan with speed2 when timer1 is true and speed3 when timer 2 is true.


#11

So are you saying that V16 = Timer 1 and V17 = Timer 2?
What logic should apply if both are true?

Pete.


#12

yes.
random :D… each timer will be configured with different hour


#13

Sorry, I don’t understand. Are you saying that both timers can NEVER be true at the same time, but they could be false at the same time?

Pete.


#14

yes, sorry for my english :sweat_smile:


#15

I’ve tidied up the formatting on this a bit, but I don’t understand why you’re setting the speed to speed1, rather than 0 when the fan is off.

Also, I don’t understand why you’re calling the void fan() function with a timer every 1.5 seconds. I would have though that calling it directly from the BLYNK_WRITE(V16) and V17 functions, as both of these functions will be triggered when the value changes from torque to false or false to true.

Putting some serial.Print functions in to your code, to show you the values of the various variables would help you to understand what’s happening.

Pete.


#16

you are right, i can call the function directly inside the timer function… i’ll modify the code…

but now, I’ve tried to call the fan() function each 3000ms and seems works properly… but i don’t understand why…


#17

This is why I call fan() each 3000ms, I’ve create a button (V1) to start the fan “one Time” for 1 hour.
The time is controlled by the function time_fan…

BLYNK_WRITE(V1){                                                     
        abilitation_fan = param.asInt();                           
       if (abilitation_fan==HIGH) {previousMillis = millis();      
                                      speed=speed1;}
}   

void fan()
{  if (abilitation_fan==HIGH){digitalWrite (FAN, !HIGH);   //switch on the fan
                                            analogWrite (SPEED_FAN,speed );      //setting the speed
                                            if (autom==LOW) time_fan();
                                           }
                                                                             
   else {digitalWrite (FAN, !LOW);       //switch off the fan
           autom=LOW; 
           speed=speed1;}}                    //set default speed


BLYNK_WRITE(V16){                                           //TIMER 1, timer function
  if (param.asInt()==HIGH){abilitation_fan=HIGH;  
                                           autom=HIGH; 
                                          speed=speed2; }              //set speed2                 
  else {abilitation_fan=LOW; }
}                  


BLYNK_WRITE(V17){                                           //TIMER 2, timer function
  if (param.asInt()==HIGH){abilitation_fan=HIGH;  
                                           autom=HIGH; 
                                          speed=speed3; }              //set speed2                 
  else {abilitation_fan=LOW; }}    

void time_fan(){      
      unsigned long currentMillis = millis();
      if ((unsigned long)(currentMillis - previousMillis) >= 3600000) {previousMillis = currentMillis;
                                                                                                        abilitation_fan=LOW;}   
}

the “advanced” idea is:
start the fan automaticaly with speed2 when timer1 is true and speed3 when timer 2 is true
start the fan “manually” when i press V1 for an hour


#18

Sharing that information, and your complete code, at the beginning would have been useful.

A better way would be to start a countdown timer when the V1 button is pressed, that runs for 1 hour then calls a stop_fan function which turns the fan off.

Pete.


#19

is implemented in blynk a countdown function?


#20

Yes, search and ye shal find!

Pete.