I am trying to get rid of a delay if there's no serial data being sent for example:
void colours(){
fill_solid(leds, NUM_LEDS, CRGB(varR1, varG1, varB1)); //put this here so when we start or break out of fx, colour is populated
if (Serial.available() > 0) {
dataIn = Serial.readString();
delay(100);//give the data time to be read before we can read info from it.
if (dataIn.startsWith("A")) {
String stringR = dataIn.substring(dataIn.indexOf("R") + 1, dataIn.indexOf("G"));
varR1 = stringR.toInt();
String stringG = dataIn.substring(dataIn.indexOf("G") + 1, dataIn.indexOf("B"));
varG1 = stringG.toInt();
String stringB = dataIn.substring(dataIn.indexOf("B") + 1, dataIn.indexOf("E"));
varB1 = stringB.toInt();
if ((stringR.length() > 0) && (stringG.length() > 0)&& (stringB.length() > 0)){
fill_solid(leds, NUM_LEDS, CRGB(varR1, varG1, varB1));
}
/*
//debug - to check incoming remote commands work.
char buf [100]; // must be large enough for the whole string
sprintf (buf, "Red: %d, Green: %d, Blue: %d", varR1, varG1, varB1);
String sum = (buf);
Serial.println(sum);
*/
}
else if (dataIn.startsWith("B")) {
String stringBright = dataIn.substring(dataIn.indexOf("E") + 1, dataIn.indexOf("F"));
BRIGHTNESS = stringBright.toInt();
FastLED.setBrightness(BRIGHTNESS);
/*
//debug - to check incoming remote commands work.
char buf [100]; // must be large enough for the whole string
sprintf (buf, "Brightness: %d", BRIGHTNESS);
String sum = (buf);
Serial.println(sum);
*/
}
else if (dataIn.startsWith("FX")) {
FastLED.clear();
ledMode = 1;
}
}
if (dataIn.length() > 0){
Serial.print(dataIn);
dataIn = "";
}
delay(250); //this seems to be the best time to allow fastled code to run and give enough time for data to be sent to the leds.
}
If I change the code and put the delay here:
if (dataIn.length() > 0){
Serial.print(dataIn);
delay(250); //this seems to be the best time to allow fastled code to run and give enough time for data to be sent to the leds.
dataIn = "";
}
The delay doesn't seem to work properly and only the first 2 string characters are read. I realised after posting it wasn't the serial buffer I needed to flush, I just needed to clear the dataIn string. Now I just have this delay issue.