How to Change the colour of LED's with fastLED and a ESP32

481 Views Asked by At

so I developed an app which will send a series of integers over Bluetooth (for RED, GREEN & BLUE) to a ESP32 which will then change the colour of 3 LED's (WS2811) based on the numbers it receives. It works the first time I send them but when I try to change the colour of the LED's a second time nothing happens.

By using the serial monitor of the Arduino IDE I have verified that the numbers are being received by the ESP32 every time I send the numbers, but I cannot understand why the LEDs are not changing colour after the first send.

The code is as follows :


#include <Arduino.h>
#include <fastled_config.h>

#define NUM_LEDS      3 // was 100
#define LED_TYPE   WS2811
#define COLOR_ORDER   RGB
#define DATA_PIN        4
//#define CLK_PIN       4
#define VOLTS          12
#define MAX_MA       4000
CRGBArray<NUM_LEDS> leds;

#define LED 2


int myRGB[30];
int counter =0;
int display =-1;



#include "BluetoothSerial.h" 


// init Class:
BluetoothSerial ESP_BT; 


// Parameters for Bluetooth interface
int incoming;

void setup() {
  Serial.begin(115200);
  ESP_BT.begin("ESP32_Control"); //Name of your Bluetooth interface -> will show up on your phone


  delay( 3000 ); //safety startup delay
  FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
    .setCorrection(TypicalLEDStrip);
}

void loop() {


delay(1000);
Serial.println(myRGB[1]);
Serial.println(myRGB[2]);
Serial.println(myRGB[3]);

leds[0].r = myRGB[1];
leds[0].g = myRGB[2];
leds[0].b = myRGB[3];

leds[1].r = myRGB[4];
leds[1].g = myRGB[5];
leds[1].b = myRGB[6];

leds[2].r = myRGB[7];
leds[2].g = myRGB[8];
leds[2].b = myRGB[9];

FastLED.show();

  
  // -------------------- Receive Bluetooth signal ----------------------
  if (ESP_BT.available()) 
  {

    
    incoming = ESP_BT.read(); //Read what we receive 
digitalWrite(LED, HIGH);

counter ++;
myRGB[counter] = incoming;

  if (counter > 29) counter = 0;
Serial.print("counter :" );
Serial.println(counter);
Serial.print( myRGB[counter]);
  

    
    }
  } // end loop
2

There are 2 best solutions below

0
Brian Connolly On

It looks like the LED colors are being shown before before Bluetooth inputs are received.

I would move FastLED.show() to the end of loop().

    FastLED.show();
} // end loop
0
dazz500 On

sorry for the late reply. Ok the following code works, it not really much different to the original code apart from this :

enter code hereif (counter < 9) myRGB[counter] = incoming;

enter code here }

Which I'm guessing did make a difference (although it shouldn't have)

#include <Arduino.h>
#include <fastled_config.h>


#define NUM_LEDS      3 // was 100
#define LED_TYPE   WS2811
#define COLOR_ORDER   RGB
#define DATA_PIN        4
//#define CLK_PIN       4
#define VOLTS          12
#define MAX_MA       4000
CRGBArray<NUM_LEDS> leds;

#define LED 2


int myRGB[30];
int counter =0;
int displayCounter =-1;
 



#include "BluetoothSerial.h" 


// init Class:
BluetoothSerial ESP_BT; 


// Parameters for Bluetooth interface
int incoming;

void setup() {

  
Serial.println("ESP started !!");


  Serial.begin(115200);
  ESP_BT.begin("ESP32_Control"); //Name of your Bluetooth interface -> will show up on your phone


  delay( 3000 ); //safety startup delay
  FastLED.setMaxPowerInVoltsAndMilliamps( VOLTS, MAX_MA);
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS)
    .setCorrection(TypicalLEDStrip);
    
}

void loop() {

delay(20); // WITHOUT THIS LINE MORE LIKELY TO GET INCORRECT DATA FROM BLUETOOTH

displayCounter ++;
if (displayCounter < 4){
  
  Serial.println("*****  the following values going into led[0] ( R G B )  *** ");
Serial.println(myRGB[1]);
Serial.println(myRGB[2]);
Serial.println(myRGB[3]);
Serial.println("*****  the following values going into led[1] ( R G B )  *** ");
Serial.println(myRGB[4]);
Serial.println(myRGB[5]);
Serial.println(myRGB[6]);

Serial.println("*****  the following values going into led[2] ( R G B )  *** ");
Serial.println(myRGB[7]);
Serial.println(myRGB[8]);
Serial.println(myRGB[9]);

}


leds[0].r = myRGB[1];
leds[0].g = myRGB[2];
leds[0].b = myRGB[3];

leds[1].r = myRGB[4];
leds[1].g = myRGB[5];
leds[1].b = myRGB[6];

leds[2].r = myRGB[7];
leds[2].g = myRGB[8];
leds[2].b = myRGB[9];
FastLED.show();

  
  // -------------------- Receive Bluetooth signal ----------------------
  if (ESP_BT.available()) 
  {

displayCounter = -1;
    
    incoming = ESP_BT.read(); //Read what we receive 
//digitalWrite(LED, HIGH);


counter ++;
  if (counter < 9)
  myRGB[counter] = incoming;
      
    }

else {counter = 0;
  // Serial.println("End of bluetooth data");
}



  } // end loop