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

Nodemcu 2switches 2 relays

#1

Hello, can someone help me with this sketch i want to use 2 buttons and 2 relays instead 1
lets say one button will be on D4 second on D5
one relay on D6 and second on D7

I found on forum this sketch this work perfect but i `am new in arduino word and i dont know how to change it.
Sorry for my English :sweat_smile:

Code:

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

#define VPIN V2

char auth[] = "Token";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wifi Name";
char pass[] = "Password";

void lightOn();
void lightOff();

boolean LampState = 0;
boolean SwitchReset = true;

const int TacSwitch = D5; 
const int RelayPin = D6; 

SimpleTimer timer;
WidgetLED VLED(V11);

void setup()      
{
  Serial.begin(115200);
  pinMode(RelayPin, OUTPUT);
  pinMode(TacSwitch, INPUT_PULLUP);
  delay(10);
  digitalWrite(RelayPin, LOW);
  Blynk.config(auth);
  timer.setInterval(100, ButtonCheck);
}
void loop()
{
  Blynk.run();
  timer.run();
}
void ButtonCheck() {
  boolean SwitchState = (digitalRead(TacSwitch));
  if (!SwitchState && SwitchReset == true) {
    if (LampState) {
      lightOff();
    } else {
      lightOn();
    }
    SwitchReset = false;
    delay(50);
  }
  else if (SwitchState) {
    SwitchReset = true;
  }
}
void ToggleRelay() {
  LampState = !LampState;
  if (LampState) {
       lightOn();
  }
  else lightOff();
}
void lightOn() {
    digitalWrite(RelayPin, LOW);
    LampState = 1;
    Blynk.virtualWrite(VPIN, HIGH); 
    VLED.off();
}
void lightOff() {
    digitalWrite(RelayPin, HIGH);
    LampState = 0;
    Blynk.virtualWrite(VPIN, LOW); 
    VLED.on();
}
BLYNK_WRITE(VPIN) {
  int SwitchStatus = param.asInt();
    if (SwitchStatus == 2){
    ToggleRelay();
  }
  else if (SwitchStatus){
    lightOn();
  }
  else lightOff();
}
0 Likes