-1

I am trying to pass two values into my Arduino script from a Bluetooth serial connection.

The first part of the string received is the LED number, the second is the LED color in the form of a hex code (0x000000).

The first variable is converted and working fine, but when I try to pass the color value I get an error saying the operand has to be CRGB or String.

I don't know why it is throwing an error when I am passing it a string. Is there a way to force upload past this error because it is a mistake, or am I missing something?

Here is my code:

#include "BluetoothSerial.h"
#include <FastLED.h>

#define DATA_PIN 22
#define LED_TYPE WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS 120
#define BRIGHTNESS 23
#define x inputFromOtherSide.substring(3,6).toInt()
   
CRGB leds[NUM_LEDS];
BluetoothSerial SerialBT;
    
void setup() {
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
  
  Serial.begin(115200);
  Serial.println("Serial Port Started");
  SerialBT.begin("ESP32test");
  Serial.println("Bluetooth Started");
}
    
void loop() {
  String inputFromOtherSide;
 
  if (SerialBT.available()) {
    inputFromOtherSide = SerialBT.readString();
    Serial.println("Data Recieved: ");
    Serial.println(inputFromOtherSide);
    delay(500);
    Serial.println(inputFromOtherSide.substring(3,6));
    Serial.println(inputFromOtherSide.substring(11));

    leds[x] = inputFromOtherSide.substring(11);
    FastLED.show();
  }
}
ocrdu
  • 2,172
  • 6
  • 15
  • 22

1 Answers1

0

leds[x] is of type CRGB, and will accept uint8_t raw[3], or three bytes, as a value, like so:

leds[x] = 0xAA99BB;

You are sending it a String object, which it doesn't understand.

If you are sure there are exactly 3 bytes in inputFromOtherSide.substring(11), you could try this:

leds[x] = inputFromOtherSide.substring(11).data();

This could work, depending on what you send across the serial line. If you send 6 chars that spell out a 3-byte number in hex, you will have to convert that into a three-byte hex number first.

I would prefer using:

leds[x].setRGB(170, 153, 187); // 0xAA99BB

Also see here for six ways to set the LED colour.

ocrdu
  • 2,172
  • 6
  • 15
  • 22