RGB strip, 240v relay and push button

Would this block of script read the output state from the code rather than the button? It is pulling the state from lampState and using that to determine if the output is LOW or HIGH.

BlynkTimer timer;             // works like a second loop
void checkPhysicalButton();

int lampState = HIGH;
int btnState = HIGH;

BLYNK_CONNECTED() { // connect to BLYNK Cloud

  Blynk.syncVirtual(V0); // pull latest state

  BLYNK_WRITE(V0) {
    lampState = param.asInt();
    digitalWrite(lampOut, lampState);
  }

void checkPhysicalButton()
{
  if(lampState == LOW) 
  {
    Blynk.virtualWrite(V0, HIGH); 
  }
   else
   {
    Blynk.virtualWrite(V0, LOW);
   }

The example I pointed you to uses a physical momentary push button, and on the app it uses the button widget set as a switch. In the example it will change the state of the relay every time the momentary button is pulled low. it then updates the switch in the app to reflect the state of the relay.

I though this is what you needed?

Sorry I must’ve read the code wrong then. Yes that is what I am after, the only difference is I need it to be when the physical button is pulled high. Does it also reverse the state of the output when the button widget is pressed?

Pushing the physical button changes the state of the relay (actually the pin connected to the relay). It will either turn it on or off. At the same time it will update the switch in the APP to reflect the state of the relay pin. You can also turn the relay pin on or off using the switch on the app.

As for making it respond to when the button is high, I think you only need to change this line of code

if (digitalRead(btnPin) == LOW)

to

if (digitalRead(btnPin) == HIGH)
1 Like