Combine timmer, dimmer and temperatur controller

please help me…why the instrument do not running well?

/**************************************************************
 * timeinput.ino Demonstrate interaction of Time library with
 * Blynk's TimeInput widget.
 * App project setup:
 * RTC widget (no pin required!!!)
 * V1 : Manual/Auto button
 * V2: On-off button
 
 * 
 * Time Input widget on V8 (All days)
 * Button selection for Time Input (All days) on V9
 * 
 * Time Input widget on V10 (Up to you)
 * Button selection for Time Input (Up to you) on V11
 * 
 **************************************************************/

#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <TimeLib.h>
#include <WidgetRTC.h>

#include <DHT.h>
#include <LiquidCrystal.h>
#define DHTPIN 3          // What digital pin we're connected to
#define DHTTYPE DHT11     // DHT 11


LiquidCrystal lcd(13, 8, 7, 6, 5, 4);
const int relay1 =  A0;
const int relay2 =  A1;
int humLowTrigger;
int tempHighTrigger;





char auth[] = "c2d143cab1xxxxbbfce665e9a2f9e";
char ssid[] = "kangxxhadi";
char pass[] = "poxxron";
#include <WidgetLED.h>



#define TestLED 9                 // on board LED pin assignment
char Date[16];
char Time[16];

long startsecondswd;            // weekday start time in seconds
long stopsecondswd;             // weekday stop  time in seconds
long nowseconds;                // time now in seconds
bool isFirstConnect = true;



int manual=0;
int oldstatus;


int alldays;
int uptoyou;


#include <SoftwareSerial.h>
SoftwareSerial EspSerial(10, 11); // RX, TX



#include  <TimerOne.h>          // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0;               // Variable to use as a counter volatile as it is in an interrupt
volatile boolean zero_cross=0;  // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 12;                // Output to Opto Triac
int brightness = 128;           // Dimming level (0-128)  0 = on, 128 = 0ff
int freqStep = 75;    // This is the delay-per-brightness step in microseconds.
int val = 0;

int autodimmer;



#define ESP8266_BAUD 9600
SimpleTimer timer;

WidgetRTC rtc;

ESP8266 wifi(&EspSerial);
DHT dht(DHTPIN, DHTTYPE);
void updateHum(int param);
void updateTemp(int param);



BLYNK_WRITE(V5) {
  updateHum(param.asInt());
}
BLYNK_WRITE(V6) {
  updateTemp(param.asInt());
}
void Readdata()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V15, h);
  Blynk.virtualWrite(V16, t);
  Blynk.virtualWrite(V25, humLowTrigger);
  Blynk.virtualWrite(V24, tempHighTrigger);

  
  if(h < humLowTrigger) {
      digitalWrite(relay1, LOW); 
      Blynk.virtualWrite(V26, 0);
  } else {
      digitalWrite(relay1, HIGH);
      Blynk.virtualWrite(V26, 255);
  }    
  if(t < tempHighTrigger) {
      digitalWrite(relay2, HIGH); 
      Blynk.virtualWrite(V27, 255);
  } else {
      digitalWrite(relay2, LOW);
      Blynk.virtualWrite(V27, 0);
  }
  
}
void updateHum(int param) {
humLowTrigger = param;

}
void updateTemp(int param){
tempHighTrigger = param;
}



void zero_cross_detect() {    
  zero_cross = true;               
  i=0;
  digitalWrite(AC_pin, LOW);       // turn off TRIAC (and AC)
}                                 

// Turn on the TRIAC at the appropriate time
void dim_check() {                   
  if(zero_cross == true) {              
    if(i>=brightness) {                     
      digitalWrite(AC_pin, HIGH); // turn on light       
      i=0;  // reset time step counter                         
      zero_cross = false; //reset zero cross detection
    } 
    else {
      i++; // increment time step counter                     
    }                                
  }                                  
}           





void setup()
{
  lcd.begin(16, 2);
  dht.begin();

  // Print a message to the LCD.
  lcd.print("Temp:  Humidity:");     

  
  pinMode(AC_pin, OUTPUT);                          // Set the Triac pin as output
  attachInterrupt(0, zero_cross_detect, RISING);    // Attach an Interupt to Pin 2 (interupt 0) 
  Timer1.initialize(freqStep);                      // Initialize TimerOne library 
  Timer1.attachInterrupt(dim_check, freqStep);   


  
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  
  Blynk.begin(auth, wifi, ssid, pass);

   pinMode(relay1, OUTPUT);
  digitalWrite(relay1, LOW);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay2, HIGH);
  
  int humLowTrigger = 65;
  int TempHighTrigger = 20;


  pinMode(TestLED, OUTPUT);
  digitalWrite(TestLED, LOW); // set LED OFF
  int mytimeout = millis() / 1000;
  while (Blynk.connect() == false) { // try to connect to server for 10 seconds
    if((millis() / 1000) > mytimeout + 8){ // try local server if not connected within 9 seconds
       break;
    }
  }
  rtc.begin();
  timer.setInterval(10000L, activetoday);  // check every 10 SECONDS if schedule should run today 
  timer.setInterval(30000L, reconnectBlynk);  // check every 30s if still connected to server 

}




BLYNK_CONNECTED() {
if (isFirstConnect) {
  Blynk.syncAll();
  Blynk.notify("TIMER STARTING!!!!");
isFirstConnect = false;
  }
}


void activetoday(){        // check if schedule should run today
  if(year() != 1970){

   
   if (alldays==1) { 
    Blynk.syncVirtual(V8); // sync timeinput widget  
   }
   if (uptoyou==1) { 
    Blynk.syncVirtual(V10); // sync timeinput widget  
   }
  }
}

void checklastbuttonpressed (){
   
    if(alldays==1){ oldstatus=4; }
    if(uptoyou==1){ oldstatus=5; }
  
}


void restorelastbuttonpressed (){
   
    if(oldstatus==4){ alldays=1; Blynk.virtualWrite(V9, 1);}
    if(oldstatus==5){ uptoyou=1; Blynk.virtualWrite(V11, 1);}   
    if(oldstatus==6){ 
    
      alldays=0;
      uptoyou=0;
    
      Blynk.virtualWrite(V9, 0);
      Blynk.virtualWrite(V11, 0);
      }
 }


BLYNK_WRITE(V3)//      slider brillo
{
 int brillo = param.asInt(); 
 if (autodimmer == 0) // If auto dimmer is OFF then set the dimmer according slider value
 brightness=brillo;

    
}

BLYNK_WRITE(V4)  // ON DIMMER AUTO
{
  autodimmer = param.asInt();
  if (autodimmer == 1) // If Auto dimmer is ON then calls function Dimmer()
  {
    DimmerFunction(); 
  
  }
}


void DimmerFunction()
{
  if (autodimmer >= 1)
  {
  
    for (int i=5; i <= 128; i++) {
   brightness=i;
   delay(20);
   }
   delay(20);

   for (int i=128; i >= 5; i--) {
   brightness=i;
   delay(20);
     }
   delay(20);}
   else  
  {
    // Do nothing
  }

}



BLYNK_WRITE(V1)  // Manual/Auto selection
{
  if (param.asInt()==1) {
    manual=1;
    
  

    checklastbuttonpressed ();
  
    alldays=0;
    uptoyou=0;
   
    Blynk.virtualWrite(V9, 0);
    Blynk.virtualWrite(V11, 0);
    
  } else {
    restorelastbuttonpressed ();
    manual=0;
   
    
}
}



void resetManual()
{
Blynk.virtualWrite(V1, 0);   //Turn OFF Manual Mode Widget
Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget Device
digitalWrite(TestLED, LOW); // set LED OFF
}


BLYNK_WRITE(V2)  // ON-OFF Manual 
{
    if (param.asInt()==1) {  // boton encendido  
    
   
    
        if (manual==0){  //está en modo automático     
         checklastbuttonpressed ();
         manual=1;
     
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
        
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         
         } else {             //está en modo manual 
       
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
        
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, HIGH); // set LED ON 
         Blynk.virtualWrite(V2, 1);   //Turn ON Button Widget
         }                           
         }else {
      
    
  

         if (manual==0){      //modo automático
         checklastbuttonpressed ();
         manual=1;
       
         alldays=0;
         uptoyou=0;
         Blynk.virtualWrite(V1, 1);
 
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         } else {  
         
         alldays=0;
         uptoyou=0;   
         Blynk.virtualWrite(V1, 1);
         Blynk.virtualWrite(V9, 0);
         Blynk.virtualWrite(V11, 0);
         digitalWrite(TestLED, LOW); // set LED OFF
         Blynk.virtualWrite(V2, 0);   //Turn OFF Button Widget
         }  
         }
}


BLYNK_WRITE(V9)  // All days selected
{
  if (param.asInt()==1) {

    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    alldays=1;
    uptoyou=0; 
    
    Blynk.virtualWrite(V11, 0);
  } else {
    alldays=0;
  }
}

BLYNK_WRITE(V11)  // Up to you selected
{
  if (param.asInt()==1) {
  //  timer.setTimeout(50, resetTerminal);
    timer.setTimeout(50, resetManual);
    timer.setTimeout(50, checklastbuttonpressed);
    uptoyou=1;
   
    alldays=0;
   
    Blynk.virtualWrite(V9, 0);
  } else {
    uptoyou=0;
  }
}



BLYNK_WRITE(V8)//All days
{  
  if (alldays==1) {         
  
  
    TimeInputParam t(param);
  
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
    
    if (t.hasStartTime()) // Process start time
    {
      
    }
    if (t.hasStopTime()) // Process stop time
    {
   
    }
  
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
    
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);

    if(nowseconds >= startsecondswd){    
     
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
      //terminal.println("All Day Device NOT STARTED today");
      //terminal.flush();
        
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
   
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED, HIGH); // set LED ON  TEST!!!!!
        Blynk.virtualWrite(V2, 1);
       
      }          
    }
  }
  else{
      
   }
  
   }
}

BLYNK_WRITE(V10)//Up to you 
{  
  if (uptoyou==1) {         
    
  
    TimeInputParam t(param);
  
    
    int dayadjustment = -1;  
    if(weekday() == 1){
      dayadjustment =  6; // needed for Sunday, Time library is day 1 and Blynk is day 7
    }
    if(t.isWeekdaySelected(weekday() + dayadjustment)){ //Time library starts week on Sunday, Blynk on Monday
    
    if (t.hasStartTime()) // Process start time
    {
     
    }
    if (t.hasStopTime()) // Process stop time
    {
      
    }
   
  
     for (int i = 1; i <= 7; i++) {  // Process weekdays (1. Mon, 2. Tue, 3. Wed, ...)
        if (t.isWeekdaySelected(i)) {
        
        }
      } 
    nowseconds = ((hour() * 3600) + (minute() * 60) + second());
    startsecondswd = (t.getStartHour() * 3600) + (t.getStartMinute() * 60);
    //Serial.println(startsecondswd);  // used for debugging
    if(nowseconds >= startsecondswd){    
      
      if(nowseconds <= startsecondswd + 90){    // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED, HIGH); // set LED ON
        Blynk.virtualWrite(V2, 1);
        // code here to switch the relay ON
      }      
    }
    else{
     
         
    }
    stopsecondswd = (t.getStopHour() * 3600) + (t.getStopMinute() * 60);
   
    if(nowseconds >= stopsecondswd){
      digitalWrite(TestLED, LOW); // set LED OFF
      Blynk.virtualWrite(V2, 0);
      
      if(nowseconds <= stopsecondswd + 90){   // 90s on 60s timer ensures 1 trigger command is sent
        digitalWrite(TestLED, LOW); // set LED OFF
        Blynk.virtualWrite(V2, 0);
        // code here to switch the relay OFF
      }              
    }
    else{
      if(nowseconds >= startsecondswd){  
        digitalWrite(TestLED, HIGH); // set LED ON 
        Blynk.virtualWrite(V2, 1);
      
       
      }          
    }
  }
  else{
   
    // nothing to do today, check again in 30 SECONDS time    
  }
  //terminal.println();
}
}

void reconnectBlynk() {
  if (!Blynk.connected()) {
    if(Blynk.connect()) {
     BLYNK_LOG("Reconnected");
    } else {
      BLYNK_LOG("Not reconnected");
    }
  }
}

void loop()
{
  DimmerFunction(); 

  lcd.setCursor(0, 1);
  // read humidity
  float h = dht.readHumidity();
  //read temperature in Fahrenheit
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }

  lcd.print(t);
  lcd.setCursor(7,1);
  lcd.print(h);  
  if (Blynk.connected()) {
    Blynk.run();
  }
  timer.run();
}
1 Like

Everything in your void loop() is running hundreds/thousands times a second… thus there should ONLY be the timer.run() and the Blynk.Run() commands in there (the if ((Blynk.connected()) is also fine.

Since you are supposedly already using timers (albeit a different timer library then the “officially supported” SimpleTimer / BlynkTimer), move all the LCD stuff and dimmer() function into their own timed segments.

1 Like

thank you mister…problem cause from delay…i am replace value delay to be delay(10)…delay>20ms is make not stable…so now my esp8266 …connected very well …24 Hour…