Blynk, Touch Sensor and Relay

sorry PeteKnight :slight_smile: and also sorry vor insulting you :wink:

so you mean

  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(130L, checkPhysicalButton2);

that the update not run at the same time?

one question if I may ask anymore - Led 0 and Led 11 on the neopixel strip lights green - if I push the touchsensor it turns to red, but if I release it turns back to green, but… if relay on = led green / if relay off = led red

Any tip for me?

void checkPhysicalButton2()
{
  if (digitalRead(btnTwoPin) == LOW) {
    // btnState is used to avoid sequential toggles
    if (btnTwoState != LOW) {

      // Toggle LED state
      relayTwoState = !relayTwoState;
      digitalWrite(relayTwoPin, relayTwoState);

      // Update Button Widget
      Blynk.virtualWrite(V1, relayTwoState);
    }
    btnTwoState = LOW;
    strip.setPixelColor(11, 0, 127, 0);
    strip.show();
  } else {
    btnTwoState = HIGH;
    strip.setPixelColor(11, 127, 0, 0);
    strip.show();
  }
}

Ok found it out by myself :slight_smile:

And here is the complete working code

I check the state of the of the “relayOneState” - this gives a number of 0 or 1, and if the “relayOneState == 1” than turn Pixel 1 to green - if not turn to red. This have to go inside of the “checkPhysikalButton”

maybe someone got a cleaner and smother idea for my sketch

#define BLYNK_PRINT Serial

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

const int relayOnePin = 12; //connected to nodeMCU Pin D6 and relay IN1
const int relayTwoPin = 14; //connected to nodeMCU Pin D5 and relay IN2
const int btnOnePin = 13;   //connected to nodeMCU Pin D7 Touch Sensor 1 IN
const int btnTwoPin = 15;   //connected to nodeMCU Pin D8 Touch Sensor 2 IN

const int neoPin = 5; // WS2812b connectet to nodeMCU Pin D1

Adafruit_NeoPixel strip = Adafruit_NeoPixel(12, neoPin, NEO_GRB + NEO_KHZ800); //Set the Neopixel Strip to 12 Pixels on pin 5

// Your Wifi Credentials and Auth Tokens
char auth[] = "x";
char ssid[] = "x";
char pass[] = "x";
char server[] ="x";

BlynkTimer timer;
void checkPhysicalButton1();
void checkPhysicalButton2();

int relayOneState = LOW;
int btnOneState = HIGH;

int relayTwoState = LOW;
int btnTwoState = HIGH;

BLYNK_CONNECTED() 
{
  Blynk.syncVirtual(V0);
  Blynk.syncVirtual(V1);
}

// Button 1
BLYNK_WRITE(V0) {
  relayOneState = param.asInt();
  digitalWrite(relayOnePin, relayOneState);
}

void checkPhysicalButton1()
{
  if (digitalRead(btnOnePin) == LOW) {
    
      if (btnOneState != LOW) {
        relayOneState = !relayOneState;
        digitalWrite(relayOnePin, relayOneState);
        Blynk.virtualWrite(V0, relayOneState);
      }
       
    btnOneState = LOW;
    } 
  
    else {
      btnOneState = HIGH;
    }
    
    //Led 1 Color Change
    if (relayOneState == 1) {
      strip.setPixelColor(0, 64, 224, 208);
      strip.show();
    }
    
    else {
      strip.setPixelColor(0, 127, 0, 0);
      strip.show();
    }
}

// Button 2
BLYNK_WRITE(V1) {
  relayTwoState = param.asInt();
  digitalWrite(relayTwoPin, relayTwoState);
}

void checkPhysicalButton2()
{
  if (digitalRead(btnTwoPin) == LOW) {
    
    if (btnTwoState != LOW) {
      relayTwoState = !relayTwoState;
      digitalWrite(relayTwoPin, relayTwoState);
      Blynk.virtualWrite(V1, relayTwoState);
    }
    
    btnTwoState = LOW;   
  }
  
    else {
      btnTwoState = HIGH;
    }

    //LED 2 Color Change
    if (relayTwoState == 1) {
      strip.setPixelColor(11, 64, 224, 208);
      strip.show();
    }
    
    else {
      strip.setPixelColor(11, 127, 0, 0);
      strip.show();
    }
}

void setup()
{
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass, server);

  strip.begin();
  strip.show();
  strip.setBrightness(64);


  pinMode(relayOnePin, OUTPUT);
  pinMode(btnOnePin, INPUT_PULLUP);
  digitalWrite(relayOnePin, relayOneState);
  
  pinMode(relayTwoPin, OUTPUT);
  pinMode(btnTwoPin, INPUT_PULLUP);
  digitalWrite(relayTwoPin, relayTwoState);

  timer.setInterval(100L, checkPhysicalButton1);
  timer.setInterval(130L, checkPhysicalButton2);

}


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