LED widget won't work

I have a float switch attached to my Arduino and already have an if statement to turn on a plug socket when it changes state. I also want an LED on Blynk to come on but can’t seem to find the right way of coding it. Can you help?

    #define BLYNK_PRINT Serial1

    #include <BlynkSimpleStream.h>

    #define BLYNK_RED       "#D3435C"
    #define BLYNK_GREEN     "#23C48E"

    char auth[] = "65b59ec2bb95487dbfe59f0c4d5f4166";

    **WidgetLED led2(V2);**

    void setup() {
      Serial1.begin(9600);
      Serial.begin(9600);
      Blynk.begin(Serial, auth);

      pinMode(51, INPUT_PULLUP);
      pinMode(13, OUTPUT);

    }

    void loop() 
    {
      Blynk.run();
      
      int sensorVal = digitalRead(51);

      Serial.println(sensorVal);

      if (sensorVal == HIGH) 
      {
        digitalWrite(13, LOW);
        **led2.setColor(BLYNK_RED);**
      } 
      else 
      {
        digitalWrite(13, HIGH);
        **led2.setColor(BLYNK_GREEN);**
      }
    }

I have marked with stars what I thought would work but it doesn’t. Can you help?

The command is led2.setValue(); for intensity and led2.on(); led2.off(); to turn it on and off.

And Blynk.setProperty(V2, “color”, BLYNK_RED); for the custom colour.
This assumes you have defined BLYNK_RED as a HEX value: #define BLYNK_RED "#D3435C"
Otherwise this will work as well: Blynk.setProperty(V2, “color”, “#D3435C”);

1 Like

Check this post for more virtual RGB LED fun :wink:

Thanks for your reply. I had tried that and just did again and it is still not working. The arduino PIN 13 is working but the LED on Blynk is not coming on.

I now have this…

    #define BLYNK_PRINT Serial1

    #include <BlynkSimpleStream.h>

    WidgetLED led2(V2);
    BlynkTimer timer;
    bool ledStatus = false;

    #define BLYNK_RED       "#D3435C"
    #define BLYNK_GREEN     "#23C48E"

    char auth[] = "65b59ec****************4d5f4166";


    void setup() 
    {
      Serial1.begin(9600);
      Serial.begin(9600);
      Blynk.begin(Serial, auth);

      pinMode(51, INPUT_PULLUP);
      pinMode(13, OUTPUT);
    }



    void loop() 
    {
      Blynk.run();
      
      int sensorVal = digitalRead(51);

      if (sensorVal == HIGH) 
      {
        digitalWrite(13, LOW);
        led2.on();
      } 
      else 
      {
        digitalWrite(13, HIGH);
        led2.off();
      }
    }

Don’t worry…, it is working… it just takes about 5 seconds for it to register and the LED to light up. Sorry to waste your time.

You have everything running thousands of times a second in your void loop(), so what is probably happening is flood errors and delays. Break everything out but the blynk.run() and add in a timer.run() then put your sensor reads, pin writes and led commands in a timed loop.

http://docs.blynk.cc/#troubleshooting-flood-error

http://docs.blynk.cc/#blynk-firmware-blynktimer

Also, when posting your code here, format it like this for proper forum viewing.

Thanks for the advice. However will this timer continue to trigger events even if Blynk goes offline? Eg. The Arduino become unconnected with the network/server? I need some functions to continue as timed even if Blynk gets disconnected.

I thought I did post it properly. I highlight the code and then click the ‘code’ button. Is they not write?

Generally Blynk IoT works best when online and talking to all it’s components (APP ↔ SERVER ↔ HARDWARE), however there are ways of coding “offline” hardware operation and even reconnection with different commands, but they do seem best suited for the ESP/WiFi device and connection types.

But as long as the sketch is not stuck at the “trying to connect” state then yes, all timers and loops in your sketch will keep working.

Unfortunately that <Preformatted Text> button method doesn’t always display C++ code in a clean and properly highlighted way, thus the three backticks and initial “cpp” method at the beginning, followed by three more backticks at the end, ensures proper visuals… as directed in the initial Welcome Topic, that most people ignore the first time and never see again :stuck_out_tongue_winking_eye: