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
It looks like the LED colors are being shown before before Bluetooth inputs are received.
I would move
FastLED.show()to the end ofloop().