I have a phone app in react native sending data over bluetooth to light a digital led stip.
When I change the slider the value is added to an array. if the array is greater than 0, it will shift from the array and send the value every 20ms.
The issue is that even if I send only 10 values a second it seems to process incorrectly and incorrect colors are displayed.
For testing purposes I have an array, 360 values, all are the same: 42. I am sending the values of the array one by one every 20ms. (even at 100ms interval the issue still occurs).
sendBatchValues = (deviceObject, writeService, writeChar) => {
for (let i = 0; i < 361; i++) {
this.dataToSend.push(45);
}
this.theInterval = setInterval(this.intervalCallBack, 20);
}
intervalCallBack = (value) => {
let currentSliderValueToSend = this.dataToSend.shift();
this.sendDataThroughService(currentSliderValueToSend, this.props.bluetooth.deviceObject, this.props.bluetooth.connectionData.writeServiceUUID, this.props.bluetooth.connectionData.writeCharacteristicUUID)
if (this.dataToSend.length == 0) {
clearInterval(this.theInterval);
}
}
sendDataThroughService = (value, deviceObject, writeService, writeChar) => {
value = Math.floor(value);
let hsaValue = this.hslToRgb(value/360);
let fullCommandRGB = "c:" + hsaValue[0] + "," + hsaValue[1] + "," + hsaValue[2] + ";";
let encodedString = btoa(fullCommandRGB);
if(Object.keys(deviceObject).length > 0) {
deviceObject.writeCharacteristicWithoutResponseForService(writeService, writeChar, encodedString);
}
}
and on the arduino I am processing the data by splitting on : , and ;
first value is the command, second third and forth and the red green and blue. I send those values to the function that changes the led strip.
void loop() {
while (mySerial.available () > 0) {
processIncomingByte (mySerial.read ());
}
if (_patternLoop != NULL) {
// Serial.println("pointer has been set - now looping a pattern");
_patternLoop();
}
}
const unsigned int MAX_INPUT = 50;
void processIncomingByte (const byte inByte) {
static int parse_the_string = 0;
static char commandString [MAX_INPUT];
static char red [MAX_INPUT];
static char green [MAX_INPUT];
static char blue [MAX_INPUT];
static unsigned int input_pos = 0;
switch (parse_the_string) {
case 0:
switch (inByte) {
case ':':
commandString [input_pos] = '\0'; // terminating null byte
input_pos = 0;
parse_the_string ++;
break;
default:
if (input_pos < (MAX_INPUT - 1))
commandString [input_pos++] = inByte;
break;
}
break;
case 1:
switch (inByte) {
case ',': // end of text
red [input_pos] = '\0'; // terminating null byte
input_pos = 0;
parse_the_string ++;
break;
default:
if (input_pos < (MAX_INPUT - 1))
red [input_pos++] = inByte;
break;
}
break;
case 2:
switch (inByte) {
case ',': // end of text
green [input_pos] = '\0'; // terminating null byte
input_pos = 0;
parse_the_string ++;
break;
default:
if (input_pos < (MAX_INPUT - 1))
green [input_pos++] = inByte;
break;
}
break;
case 3:
switch (inByte) {
case ';': // end of text
blue [input_pos] = '\0'; // terminating null byte
// terminator reached! process input_line here ...
process_data (commandString, red, green, blue);
input_pos = 0;
parse_the_string = 0;
break;
case '\r': // discard carriage return
break;
default:
if (input_pos < (MAX_INPUT - 1))
blue [input_pos++] = inByte;
break;
} // end of switch
break;
default:
break;
}
}
void process_data (const char * commandString, const char * red, const char * green, const char * blue) {
int redValue = atoi(red) - 255;
int greenValue = atoi(green) - 255;
int blueValue = atoi(blue) - 255;
redValue = abs(redValue);
greenValue = abs(greenValue);
blueValue = abs(blueValue);
manuallyChangeBladeColor(redValue, greenValue, blueValue);
}
I uploadeda video showing that, when sending the same value 360 times every 20ms, it glitches and shows the wrong value.
What options do I have to improve this/ avoid the glitch.