Blynk project

Hi I am using blynk with the app buttons and physical push button. I got the following code off of the forum! It works great for on off now I want one blynk and physical button to turn on a fan and the second to turn the fan off and on only if the fan is turned on already. The only problem with the code I got was it didn't have any comments and I am struggling with exactly what is turning the relay(s) on and off. Right now I just have it running two relays but I would have them both controlling the same one ideally.

#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <SimpleTimer.h>

#define EspSerial Serial3 // Hardware Serial on Mega, Leonardo, Micro...
#define ESP8266_BAUD 115200 // Your ESP8266 baud rate:

char auth[] = "*********"; //off blynk project
char ssid[] = "8888888"; // Your WiFi credentials.
char pass[] = "********"; // Your WiFi credentials.

ESP8266 wifi(&EspSerial);

void lightOn();
void lightOff();
void lightOnOffOn();
void lightOnOffOff();

boolean FanONState = 0;
boolean FanONOFFState = 0; //added
boolean SwitchReset = true;
boolean SwitchResetONOFF = true; //added

const int FanONSwitch = 13; 
const int RelayPin = 12; 
const int FanONOFFSwitch = 11; //added
const int RelayPin2 = 10;

SimpleTimer timer;
WidgetLED VLED(V11);
WidgetLED VonoffLED(V12); //added

void setup()      
{
  //Serial.begin(115200);
  EspSerial.begin(ESP8266_BAUD); // Set ESP8266 baud rate
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  {
    pinMode(RelayPin, OUTPUT);
    pinMode(RelayPin2, OUTPUT);
    pinMode(FanONSwitch, INPUT_PULLUP);
    pinMode(FanONOFFSwitch, INPUT_PULLUP);  //added
    delay(10);
    digitalWrite(RelayPin, LOW);
    digitalWrite(RelayPin2, LOW);
    timer.setInterval(100, ButtonCheck);
    timer.setInterval(100, ButtonONOFFCheck); //added
  }
}
void loop()
{
  Blynk.run();
  timer.run();
}
void ButtonCheck() {
  boolean SwitchState = (digitalRead(FanONSwitch));
  if (!SwitchState && SwitchReset == true) {
    if (FanONState) {
      lightOff();
    } else {
      lightOn();
    }
    SwitchReset = false;
    delay(50);
  }
  else if (SwitchState) {
    SwitchReset = true;
  }
}
void ToggleRelay() {
  FanONState = !FanONState;
  if (FanONState) {
       lightOn();
  }
  else lightOff();
}
void ButtonONOFFCheck() {
  boolean SwitchStateONOFF = (digitalRead(FanONOFFSwitch));
  if (!SwitchStateONOFF && SwitchResetONOFF == true) {
    if (FanONOFFState) {
      lightOnOffOff();
    } else {
      lightOnOffOn();
    }
    SwitchResetONOFF = false;
    delay(50);
  }
  else if (SwitchStateONOFF) {
    SwitchResetONOFF = true;
  }
}
void ToggleRelayONOFF() {
  FanONOFFState = !FanONOFFState;
  if (FanONOFFState) {
       lightOnOffOn();
  }
  else lightOnOffOff();
}
void lightOn() {
    digitalWrite(RelayPin, HIGH);
    FanONState = 1;
    Blynk.virtualWrite(V5, HIGH); 
    VLED.off();
}
void lightOnOffOn() {
    digitalWrite(RelayPin2, HIGH);
    FanONOFFState = 1;
    Blynk.virtualWrite(V6, HIGH); 
    VonoffLED.off();
}
void lightOff() {
    digitalWrite(RelayPin, LOW);
    FanONState = 0;
    Blynk.virtualWrite(V5, LOW); 
    VLED.on();
}
void lightOnOffOff() {
    digitalWrite(RelayPin2, LOW);
    FanONOFFState = 0;
    Blynk.virtualWrite(V6, LOW); 
    VonoffLED.on();
}
BLYNK_WRITE(V5) {
  int SwitchStatus = param.asInt();
    if (SwitchStatus == 2){
    ToggleRelay();
  }
  else if (SwitchStatus){
    lightOn();
  }
  else lightOff();
}
BLYNK_WRITE(V6) {
  int SwitchStatusONOFF = param.asInt();
    if (SwitchStatusONOFF == 2){
    ToggleRelayONOFF();
  }
  else if (SwitchStatusONOFF){
    lightOnOffOn();
  }
  else lightOnOffOff();
}

Your function names, lightOn(), lightOff(), lightOnOffOn(), and lightOnOffOff() make almost no sense.

boolean FanONState = 0;
boolean FanONOFFState = 0; //added

The fan as ONE state. It is either on or off. You could have a boolean, fanOn, that would contain true if the fan was on, and false if the fan was not on. What the heck FanONOFFState is supposed to mean is a real mystery. Does 0 mean that the fan is ONOFF?

The ONOFF was the one that was to cycle on and off on timers.

daveeno:
The ONOFF was the one that was to cycle on and off on timers.

So, wouldn't fanCycling make more sense?

PaulS:
So, wouldn't fanCycling make more sense?

Sounds good! Now how about some code suggestions to make it do that.

I am trying to use a function something like below but doesn't want to work. Not in front of my hardware right now but I think it turns off after the interval but doesn't turn back on.

void onoffcycle(){
      FanONOFFState = 0;
      Blynk.virtualWrite(V6,LOW);
      VonoffLED.on();
      currentMillis = millis();
      if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED
        previousMillis = currentMillis;
        
        if (i == LOW){
          i = HIGH;
        } else {
          i = LOW;
        }
        digitalWrite(RelayPin2, i);
    }
    
}

EDIT Below is the full code.

#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
#include <SimpleTimer.h>


#define EspSerial Serial3 // Hardware Serial on Mega, Leonardo, Micro...
#define ESP8266_BAUD 115200 // Your ESP8266 baud rate:

char auth[] = "47bebb6a25174f50adfa881b7156a63b"; //off blynk project
char ssid[] = "DavesFarm"; // Your WiFi credentials.
char pass[] = "41314131"; // Your WiFi credentials.

ESP8266 wifi(&EspSerial);

void lightOn();
void lightOff();
void lightOnOffOn();
void lightOnOffOff();
void onoffcycle();


boolean FanONState = 0;
boolean FanONOFFState = 0; //added
boolean SwitchReset = true;
boolean SwitchResetONOFF = true; //added

const int FanONSwitch = 13; 
const int RelayPin = 12; 
const int FanONOFFSwitch = 11; //added
const int RelayPin2 = 10;
int i;

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long interval = 1000;           // interval at which to blink (milliseconds)


SimpleTimer timer;
WidgetLED VLED(V11);
WidgetLED VonoffLED(V12); //added

void setup()      
{
  //Serial.begin(115200);
  EspSerial.begin(ESP8266_BAUD); // Set ESP8266 baud rate
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  {
    pinMode(RelayPin, OUTPUT);
    pinMode(RelayPin2, OUTPUT);
    pinMode(FanONSwitch, INPUT_PULLUP);
    pinMode(FanONOFFSwitch, INPUT_PULLUP);  //added
    delay(10);
    digitalWrite(RelayPin, HIGH);
    digitalWrite(RelayPin2, HIGH);
    timer.setInterval(200, ButtonCheck);
    timer.setInterval(200, ButtonONOFFCheck);
    
  }
}
void loop()
{
  Blynk.run();
  timer.run();
  
  
  
}
void ButtonCheck() {
  boolean SwitchState = (digitalRead(FanONSwitch));
  if (!SwitchState && SwitchReset == true) {
    if (FanONState) {
      lightOff();
    } else {
      lightOn();
    }
    SwitchReset = false;
    delay(50);
  }
  else if (SwitchState) {
    SwitchReset = true;
  }
}
void ToggleRelay() {
  FanONState = !FanONState;
  if (FanONState) {
       lightOn();
  }
  else lightOff();
}
void ButtonONOFFCheck() {
  boolean SwitchStateONOFF = (digitalRead(FanONOFFSwitch));
  if (!SwitchStateONOFF && SwitchResetONOFF == true) {
    if (FanONOFFState) {
      onoffcycle();
    } else {
      lightOnOffOn();
    }
    SwitchResetONOFF = false;
    delay(50);
  }
  else if (SwitchStateONOFF) {
    SwitchResetONOFF = true;
  }
}
void ToggleRelayONOFF() {
  FanONOFFState = !FanONOFFState;
  if (FanONOFFState) {
       lightOnOffOn();
  }
  else onoffcycle();
}
void lightOn() {
    digitalWrite(RelayPin, HIGH);
    FanONState = 1;
    Blynk.virtualWrite(V5, HIGH); 
    VLED.off();
}
void lightOnOffOn() {
    digitalWrite(RelayPin2, HIGH);
    FanONOFFState = 1;
    Blynk.virtualWrite(V6, HIGH); 
    VonoffLED.off();
}
void lightOff() {
    digitalWrite(RelayPin, LOW);
    FanONState = 0;
    Blynk.virtualWrite(V5, LOW); 
    VLED.on();
}
void onoffcycle(){
      FanONOFFState = 0;
      Blynk.virtualWrite(V6,LOW);
      VonoffLED.on();
      currentMillis = millis();
      if (currentMillis - previousMillis >= interval) { // save the last time you blinked the LED
        previousMillis = currentMillis;
        
        if (i == LOW){
          i = HIGH;
        } else {
          i = LOW;
        }
        digitalWrite(RelayPin2, i);
    }
    
}
void lightOnOffOff() {
    digitalWrite(RelayPin2, LOW);
    FanONOFFState = 0;
    onoffcycle();
    Blynk.virtualWrite(V6, LOW); 
    VonoffLED.on();
}
BLYNK_WRITE(V5) {
  int SwitchStatus = param.asInt();
    if (SwitchStatus == 2){
    ToggleRelay();
  }
  else if (SwitchStatus){
    lightOn();
  }
  else lightOff();
}
BLYNK_WRITE(V6) {
  int SwitchStatusONOFF = param.asInt();
    if (SwitchStatusONOFF == 2){
    ToggleRelayONOFF();
  }
  else if (SwitchStatusONOFF){
    lightOnOffOn();
  }
  else onoffcycle();
}

I really can't help you until you adopt meaningful names. lightOnOffOff() just makes no sense.

What, EXACTLY, is pin 13 supposed to do? What, EXACTLY, is pin 12 supposed to do?

  Blynk.begin(auth, wifi, ssid, pass);
  {
    pinMode(RelayPin, OUTPUT);

{ What { is { that { curly { brace { there { for?

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

Jamming one function up against another one, and then filling it full of white space just doesn't cut it.

int i;

One letter global variable names show a complete lack of imagination. They provide not a single clue what the value means.

My apologies, I thought this forum was also for newbies. I thank you for the pointers. Maybe soften the pointers unless you are hoping to discourage newbies we are not computers yet, we do have feelings and make mistakes LOL. For others interested the below loop was working but to work with Blynk I had to add a function to make it work. So it was more of a Blynk issue. I'm sure there could be a better way of doing all this. But like I said I'm a newbie....

Loop

if (cycleState == 0){  
      unsigned long currentMillis = millis();
      if ((unsigned long)currentMillis - previousMillis >= (i ?(VsliderState*60000L):(VsliderStateON*60000L))) { 
        previousMillis = millis();
        digitalWrite(RelayPin, i = !i);   
    }    
    }else{
      digitalWrite(RelayPin, FanONState);
      digitalWrite(systemStatusled, !FanONState);
    }

Added timer

timer.setInterval(100, updateMillis);

Below is full code to see how it fits together. Abit of an explanation, One physical and virtual button turn the system on and a statusLed. The second set of buttons turns on an on/off cycle only if the system is on. The statusLed stays on. The sliders (app only) control the on off times in minutes.

#include <SPI.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <TimeLib.h>
//#include <SimpleTimer.h>

#define EspSerial Serial // Hardware Serial on Mega, Leonardo, Micro...
#define ESP8266_BAUD 115200 // Your ESP8266 baud rate:

char auth[] = "****"; //off blynk project
char ssid[] = "****"; // Your WiFi credentials.
char pass[] = "****"; // Your WiFi credentials.

ESP8266 wifi(&EspSerial);

void fan_Off();
void fan_On();
void fancycle_Off();
void fancycle_On();
void onoffcycle();

boolean FanONState = 0; //initialize variable to hold fan state and set initial state
boolean Fan_cycleState = 0; 
boolean SwitchReset = 1;
boolean SwitchReset_cycle = 1; 
boolean i = 1;
boolean cycleState = 1;

const int FanONSwitch = 13; 
const int RelayPin = 12; 
const int Fan_cycleSwitch = 11; 
const int systemStatusled = 10;

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long interval;
int VsliderState;
int VsliderStateON;

BlynkTimer timer;
WidgetLED VLED(V11);

void setup()      
{
  EspSerial.begin(ESP8266_BAUD); // Set ESP8266 baud rate
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  delay(10);
  pinMode(systemStatusled, OUTPUT);
  pinMode(RelayPin, OUTPUT);
  pinMode(FanONSwitch, INPUT_PULLUP);
  pinMode(Fan_cycleSwitch, INPUT_PULLUP);  //added
  delay(10);
  digitalWrite(RelayPin, HIGH);
  digitalWrite(systemStatusled, LOW);
  timer.setInterval(100, ButtonCheck);
  timer.setInterval(100, Button_cycleCheck);
  timer.setInterval(100, updateMillis);
  timer.setInterval(500, onoffcycle);
  Blynk.syncAll();
}
void loop()
{
  Blynk.run();
  timer.run();
}
void ButtonCheck() {
  boolean SwitchState = (digitalRead(FanONSwitch));
  if (!SwitchState && SwitchReset == true) {
    if (FanONState) {
      fan_On();
    } else {
      fan_Off();
    }
    SwitchReset = false;
    delay(50);
  }
  else if (SwitchState) {
    SwitchReset = true;
  }
}
void ToggleRelay() {
  FanONState = !FanONState;
  if (FanONState) {
       fan_Off();
  }
  else fan_On();
}
void Button_cycleCheck() {
  boolean SwitchState_cycle = (digitalRead(Fan_cycleSwitch));
  if (!SwitchState_cycle && SwitchReset_cycle == true) {
    if (Fan_cycleState) {
      fancycle_On();
    } else {
      fancycle_Off();
    }
    SwitchReset_cycle = false;
    delay(50);
  }
  else if (SwitchState_cycle) {
    SwitchReset_cycle = true;
  }
}
void ToggleRelay_fancycle() {
  Fan_cycleState = !Fan_cycleState;
  if (Fan_cycleState) {
       fancycle_Off();
  }
  else fancycle_On(); //changed
}
void fan_Off() {
    digitalWrite(RelayPin, HIGH);
    FanONState = 1;
    Blynk.virtualWrite(V5, HIGH); 
    VLED.off();
    fancycle_Off();
    digitalWrite(systemStatusled,LOW);
}
void fancycle_Off() {
    digitalWrite(RelayPin, HIGH);
    Fan_cycleState = 1;
    Blynk.virtualWrite(V6, HIGH); 
    cycleState = 1;
    digitalWrite(systemStatusled,LOW);    
}
void fan_On() {
    digitalWrite(RelayPin, LOW);
    FanONState = 0;
    Blynk.virtualWrite(V5, LOW); 
    VLED.on();
    digitalWrite(systemStatusled,HIGH);
}
void updateMillis(){
  unsigned long currentMillis = millis();
}
void onoffcycle(){
    if (cycleState == 0){  
      unsigned long currentMillis = millis();
      if ((unsigned long)currentMillis - previousMillis >= (i ?(VsliderState*60000L):(VsliderStateON*60000L))) { //this makes it run VsliderState one time and VsliderStateOn the next loop
        previousMillis = millis();
        digitalWrite(RelayPin, i = !i);   
    }    
    }else{
      digitalWrite(RelayPin, FanONState);
      digitalWrite(systemStatusled, !FanONState); //! makes it write the opposite of the state because the StatusLed turns on with write HIGH and Relay turns on with write LOW
    }
}
void fancycle_On() {
    digitalWrite(RelayPin, HIGH);
    Fan_cycleState = 0;
    Blynk.virtualWrite(V6, LOW);
    digitalWrite(systemStatusled,HIGH);
    if (FanONState == 0){ 
      cycleState = 0;
      onoffcycle();
      }else{
        fancycle_Off();
      }
}
BLYNK_WRITE(V5) {
  int SwitchStatus = param.asInt();
    if (SwitchStatus == 2){
    ToggleRelay();
  }
  else if (SwitchStatus){
    fan_Off();
  }
  else fan_On();
}
BLYNK_WRITE(V6) {
  int SwitchStatus_fancycle = param.asInt();
    if (SwitchStatus_fancycle == 2){
    ToggleRelay_fancycle();
  }
  else if (SwitchStatus_fancycle){
    fancycle_Off();
  }
  else fancycle_On();
}
BLYNK_WRITE(V4) {
  VsliderState = param.asInt();
}
BLYNK_WRITE(V3) {
  VsliderStateON = param.asInt();
}