0

The code I have works for the most part but when I send data via bluetooth received over software serial, too quickly, it is not parsed correctly.

I'm a bit stuck with what can be done to a void the incorrect formatting, since it works correctly if I send data slowly.

I could have a buffer to not send data more than once every set amount of time, but more responsiveness is preferred.

I am sending a string "center:255,0,255;" it saves the command in a char array untill it sees the ":", then it saves the red in a char array breaking on "," then same for the blue and green. when it reaches the ";" it send the four arrays to print to a console. Later it the data will be used to light specific leds based on the command, but when I send data too quickly it gets parsed incorrectly, for example:

command: center
red:195
green:0
blue:255
command: center
red:208
green:0center:221
blue:0,255
command: center
red:246
green:0
blue:255

the 'green:0center:221' isnt correct, its either not seeing the "," or seeing the ";" too soon. Not sure how to avoid it.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(6,7); // RX, TX 
void setup() { 
 Serial.begin(9600);
 mySerial.begin(57600); //required when using the hm-10 with an arduino nano
 mySerial.listen();
} 
void loop() {
 while (mySerial.available () > 0) {
 processIncomingByte (mySerial.read ()); 
 }
}
const unsigned int MAX_INPUT = 50;
void processIncomingByte (const byte inByte) {
 static int parse_the_string = 0;
 static char command [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 ':':
 command [input_pos] = 0; // terminating null byte
 input_pos = 0;
 parse_the_string ++; 
 break;
 default:
 if (input_pos < (MAX_INPUT - 1))
 command [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 (command, 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 * command, const char * red, const char * green, const char * blue)
 {
 // for now just display it
 Serial.print ("command: ");
 Serial.println (command);
 Serial.print ("red:");
 Serial.println (red);
 Serial.print ("green:");
 Serial.println (green);
 Serial.print ("blue:");
 Serial.println (blue);
 }
asked Nov 11, 2018 at 19:03
0

1 Answer 1

0

Set higher baud rate for Serial and in Serial Monitor. It slows down the processing and the mySerial buffer overflows.

answered Nov 11, 2018 at 19:16
1
  • thats embarrassing, how simple >_< Thank you so much Commented Nov 11, 2018 at 20:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.