I've been trying to achieve serial communication between my arduino-based project and my pc ,i need to send commands to arduino over serial and use "if and else" to call desired function (in parseMessage() function).
I can't use delay() since I'm using interrupts for multiplexing and bit-angle modulation so i had to do serial communication another way around, this is closest I've got to succeess but still I'm getting character skips and unstability in general. and as you know coding is all great except for when you don't know what's wrong with your code, so help me please gods of the internet! ;)
The reason I'm using '#' as end of the string declearation is that I can't be sure that all characters of my command sent to arduino is there when Serial.read() asks for it, there might be more on the way and since atmega328 is faster than serial port Serial.available() might actually return -1 in middle of transmission.
ps : oh, and I can't use String class, It's very expensive, this atmega328 is already sweating under 8x8 RGBLED multiplexing and 4bit-angle modulation and he is gonna have to do even more in future.
ps : and I'm still learning English so pardon me if there is something wrong with the grammer I'm using.
void setup() { Serial.begin(9600); } bool dataRTP = false; // data ready to parse void loop() { readSerial(); } char message[64]; int index = 0; void readSerial() { if (Serial.available() > 0) while (Serial.available() > 0) if (Serial.peek() != '#') // i'm using '#' as end of string declearation. message[index++] = Serial.read(); else { message[index++] = '\n'; dataRTP = true; break; } while (Serial.read() != -1) {} // flushing any possible characters off of if (dataRTP) // UARTS buffer. parseMessage(); } void parseMessage() { // just testing here, actual code would be like : Serial.print(message); // if (!strcmp(message, "this expression")) index = 0; // callthisfunction(); dataRTP = false; // else ... etc }