Push a button on device light a LED on the Project DashBoard

Hi,

I’m trying to do a very simple thing.

I have one switch on my device and one LED on my Dashboard.

I would like to detect a switch change in state and do a single virtual write to the virtual pin to change the state of the LED on the dashboard?

I’d rather not use a timer, this works, but consumes needless bandwidth for a simple switch, and builds in a latency.

This is my attempt at doing this but it causes a flood error pretty fast.

define BUTTON1 V2 \virtual pin 2 with LED on dashboard

define BUTTON1_P 4 \switch connected to hardware pin 4

int B1V;

void loop()
{

Blynk.run();

B1V=digitalRead(BUTTON1_P);
if(B1V!=BUTTON1) Blynk.virtualWrite(BUTTON1,digitalRead(BUTTON1_P));
}

I’m not sure on which platform you are developing, but in case of Arduino, have you seen the LED widget example? You need to define said widget in your code.

You have logical error here. Should be something like that

  int prev = -1;
  int B1V;

  void loop() {
      Blynk.run();
 
      B1V = digitalRead(BUTTON1_P);
      if (B1V != prev) {
          if (B1V) {
                Blynk.virtualWrite(V2, 255);
          } else {
                Blynk.virtualWrite(V2, 0);
          }
      }
      prev = B1V;
 }

Thanks,

That seems to have fixed the issue. I guess I’ve been having an issue understanding that the virtual pins are an address and do not have a value.

Regards,
David