#include<string.h>
int ledStrip = 3;
int serialReading = 0;
int brightness = 0;
char command[12];
int pos = 0;
int flag = 0;
void setup() {
pinMode(ledStrip, OUTPUT);
Serial.begin(9600);
}
void loop() {
while(Serial.available()>0)
{
char inByte = Serial.read();
command[pos] = inByte;
pos++;
flag = 1;
Serial.println("Inside while");
}
if(flag==1)
{
for(int i = 0; i<pos; i++)
{
Serial.print(command[i]);
command[i] = '0';
}
Serial.print('\n');
pos = 0;
flag = 0;
Serial.println("Inside if");
}
}
Say some byte is available in the serial buffer, so the execution should be up to while() loop until the condition inside while() turns out to be false. Only then the if statement should be executed.
But when I send some messages through the Serial monitor, I get something like this:
They are executing the code you wrote, and showing you exactly what we all are saying. Characters arrive s l o w e r than the processor can eat them, so it starves and leaves the while loop.
The sketch is taking characters out of the serial buffer much faster than characters are arriving from your PC. If you want to read a full line before processing it, only set 'flag' when the '\n' is put in the buffer.