-------------------------Goal------------------------------------
I'm trying to use ASCII symbols to relay instructions to the Arduino via serial.
For instance, one of my goals is to control an RGB LED strip. The commands are to be as follows:
i) initialize: After catching an 'i', it starts storing data in the buffer.
s) stop: it will continue to store data in the buffer until the buffer is full or it receives 's'.
r/g/b) red/green/blue: This redirects the pointer to one of these variables using a switch.
Then it takes a string of numbers, converts them to an int, and stores them in the address pointed to by the pointer.
So, for instance I would send a string of bytes: ir255sig192sib64s
Initialize. Set to Red. Store 255. Stop. (Actual order: Initialize and store. Stop. Read stored data which says: Set to Red. Store 255.)
Initialize. Set to Green. Store 192. Stop.
Initialize. Set to Blue. Store 64. Stop.
--------------/Goal-----------------------
But before I really dig in deep, I have a quick question.
When Serial.read() is called, does it go straight to the next byte in the serial buffer?
If so, it could complicate things, because it wouldn't check the value of the byte before passing it into the buffer. It would check a value, then pass the NEXT value into the buffer regardless of what it is. Sync issues galore.
And is there a way I can bypass this functionality and check the same byte several times?
Here's my code thus far:
char pBuffer[256] = {0};
int i = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
if(Serial.read() = 'i')
{
i = 0;
while( (Serial.read() != 's') & (Serial.available() < 256) )
{
pBuffer[i] = Serial.read();
i++;
}
Serial.flush();
}
Serial.flush();
}
Edit: I was just thinking: Would it work to store Serial.read() in a variable and then check the variable multiple times?