New programmable LED

Hello,
here is my code which works perfectly. I press my on push button on my widget button, my relay changes position, and I have two LEDs which indicate the position, ON green, OFF red.

#include <SPI.h>

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "xxxx"
#define BLYNK_DEVICE_NAME "xxxx"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#include <SPI.h>
#include <Wire.h>

#include "BlynkEdgent.h"




// Set your LED and physical button pins here
const int relais1 = 14;
const int btn1 = 27;
const int ledV1 = 5;
const int ledR1 = 2;

int relais1Sate = LOW;
int btn1State = HIGH;





// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD






BLYNK_WRITE(V1) {
  relais1Sate = param.asInt();
  Serial.print("Widget Button V1 changed. Writing ");
  Serial.print(relais1Sate);
  Serial.print(" to Relay 1 and LED V1, and ");
  Serial.print(!relais1Sate);
  Serial.println(" to LED R1 ...");

  digitalWrite(relais1, !relais1Sate);
  digitalWrite(ledV1, !relais1Sate);
  digitalWrite(ledR1, relais1Sate);



}



void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW)
  {
    Serial.print("Location A, btn1 = ");
    Serial.print(digitalRead(btn1));
    Serial.print(" btn1State = ");
    Serial.println(btn1State);

    // btnState is used to avoid sequential toggles
    if (btn1State != LOW)
    {
      Serial.print("Location B, btn1 = ");
      Serial.print(btn1);
      Serial.print(", btn1State = ");
      Serial.println(btn1State);

      // Toggle LED state
      relais1Sate = !relais1Sate;

      Serial.print("Location C, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Relay 1 ...");

      digitalWrite(relais1, !relais1Sate);



      Serial.print("Location D, writing ");
      Serial.print(!relais1Sate);
      Serial.print(" to LED V1 and ");
      Serial.print(relais1Sate);
      Serial.println(" to LED R1 ...");

      digitalWrite(ledV1, !relais1Sate);
      digitalWrite(ledR1, relais1Sate);

      // Update Button Widget

      Serial.print("Location E, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Button Widget on pin V1 ...");

      Blynk.virtualWrite(V1, relais1Sate);

      btn1State = LOW;
    }
  }
  else
  {
    btn1State = HIGH;
  }







}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);


}

void checkPhysicalButton1();


void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

 


  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);
  pinMode(ledV1, OUTPUT);
  pinMode(ledR1, OUTPUT);




  timer.setInterval(100L, checkPhysicalButton1);




}


void loop() {
  BlynkEdgent.run();
    timer.run();
}

To save PINs on my esp32 I would like to replace my basic LEDs with ws2811 programmable LEDs. But I can not program, the LED lights up but does not change color do you have any idea.

#include <SPI.h>

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "xxxxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxxxx"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#include <SPI.h>
#include <Wire.h>

#include "BlynkEdgent.h"



    #include <FastLED.h>
    #define NUM_LEDS 4
    #define DATA_PIN 13

     CRGB leds[NUM_LEDS];




// Set your LED and physical button pins here
const int relais1 = 14;
const int btn1 = 27;


int relais1Sate = LOW;
int btn1State = HIGH;





// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD






BLYNK_WRITE(V1) {
  relais1Sate = param.asInt();
  Serial.print("Widget Button V1 changed. Writing ");
  Serial.print(relais1Sate);
  Serial.print(" to Relay 1 and LED V1, and ");
  Serial.print(!relais1Sate);
  Serial.println(" to LED R1 ...");

  digitalWrite(relais1, !relais1Sate);
      leds[1] = CRGB::Red,!relais1Sate;
      FastLED.show();
      leds[1] = CRGB::Green, relais1Sate;
     FastLED.show(); 



}



void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW)
  {
    Serial.print("Location A, btn1 = ");
    Serial.print(digitalRead(btn1));
    Serial.print(" btn1State = ");
    Serial.println(btn1State);

    // btnState is used to avoid sequential toggles
    if (btn1State != LOW)
    {
      Serial.print("Location B, btn1 = ");
      Serial.print(btn1);
      Serial.print(", btn1State = ");
      Serial.println(btn1State);

      // Toggle LED state
      relais1Sate = !relais1Sate;

      Serial.print("Location C, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Relay 1 ...");

      digitalWrite(relais1, !relais1Sate);
      leds[1] = CRGB::Red,!relais1Sate;
      FastLED.show();
      leds[1] = CRGB::Green, relais1Sate;
     FastLED.show(); 



      Serial.print("Location D, writing ");
      Serial.print(!relais1Sate);
      Serial.print(" to LED V1 and ");
      Serial.print(relais1Sate);
      Serial.println(" to LED R1 ...");



      // Update Button Widget

      Serial.print("Location E, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Button Widget on pin V1 ...");

      Blynk.virtualWrite(V1, relais1Sate);

      btn1State = LOW;
    }
  }
  else
  {
    btn1State = HIGH;
  }







}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);


}

void checkPhysicalButton1();


void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);



  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);




  timer.setInterval(100L, checkPhysicalButton1);




}


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

does anyone have an idea for programming the addressable leds?

Change the number of LED’s to 6. I had the same issue a while ago. I had just 2 LED in the setup. So i had defined the same in the code. But that did not work until i changed it to 6 or more. Give it a try. I am not 100% sure that this will solve your problem. Or try changing the DATA pin or other WS2812 supporting library.

EDIT :

This should work. I am using this lib from a long time.

Thank you.

I am almost happy with the result.
I have programmed 1 push button in pin 27. Why do all my push buttons change the color of the LED?

#include <SPI.h>

// Fill-in information from your Blynk Template here
#define BLYNK_TEMPLATE_ID "xxxxxxx"
#define BLYNK_DEVICE_NAME "xxxxxx"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"

#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG

#define APP_DEBUG

#include <SPI.h>
#include <Wire.h>

#include "BlynkEdgent.h"



    #include <FastLED.h>
    #define NUM_LEDS 1
    #define DATA_PIN 13

     CRGB leds[NUM_LEDS];




// Set your LED and physical button pins here
const int relais1 = 14;
const int btn1 = 27;


int relais1Sate = LOW;
int btn1State = HIGH;





// Uncomment your board, or configure a custom board in Settings.h
//#define USE_WROVER_BOARD






BLYNK_WRITE(V1) {
  relais1Sate = param.asInt();
  Serial.print("Widget Button V1 changed. Writing ");
  Serial.print(relais1Sate);
  Serial.print(" to Relay 1 and LED V1, and ");
  Serial.print(!relais1Sate);
  Serial.println(" to LED R1 ...");

  digitalWrite(relais1, !relais1Sate);
      if (!relais1Sate){leds[0] = CRGB::Red;}
      if (relais1Sate){leds[0] = CRGB::Green ;}
     
     FastLED.show(); 



}



void checkPhysicalButton1()
{
  if (digitalRead(btn1) == LOW)
  {
    Serial.print("Location A, btn1 = ");
    Serial.print(digitalRead(btn1));
    Serial.print(" btn1State = ");
    Serial.println(btn1State);

    // btnState is used to avoid sequential toggles
    if (btn1State != LOW)
    {
      Serial.print("Location B, btn1 = ");
      Serial.print(btn1);
      Serial.print(", btn1State = ");
      Serial.println(btn1State);

      // Toggle LED state
      relais1Sate = !relais1Sate;

      Serial.print("Location C, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Relay 1 ...");

      digitalWrite(relais1, !relais1Sate);
      if (!relais1Sate){leds[0] = CRGB::Red;}
      if (relais1Sate){leds[0] = CRGB::Green ;}
     
     FastLED.show(); 



      Serial.print("Location D, writing ");
      Serial.print(!relais1Sate);
      Serial.print(" to LED V1 and ");
      Serial.print(relais1Sate);
      Serial.println(" to LED R1 ...");



      // Update Button Widget

      Serial.print("Location E, writing ");
      Serial.print(relais1Sate);
      Serial.println(" to Button Widget on pin V1 ...");

      Blynk.virtualWrite(V1, relais1Sate);

      btn1State = LOW;
    }
  }
  else
  {
    btn1State = HIGH;
  }







}

BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V1);


}

void checkPhysicalButton1();


void setup()
{
  Serial.begin(115200);
  delay(100);

  BlynkEdgent.begin();

  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);



  pinMode(relais1, OUTPUT);
  pinMode(btn1, INPUT_PULLUP);
  digitalWrite(relais1, relais1Sate);




  timer.setInterval(100L, checkPhysicalButton1);




}


void loop() {
  BlynkEdgent.run();
    timer.run();
}

Check the wiring of the push buttons. I guess they all are connected in parallel :face_with_monocle:.

@Madhukesh you were right, it was a connection error