I wrote a little Arduino code which should switch on a LED by a text typed in the serial console. I have a bug in my code but I can't find the problem.
What option do you have selected in the Serial Monitor? Are you sending a carriage return and line feed, too? If so, those are stored in inSerial, too.
You also seem to expect that the serial data arrives instantly. It does not. The loop() function can run several hundred thousand times between the time the O arrives and the time the linefeed arrives. Even if you are not sending CR/LF, the loop() function can run several hundred thousand times between the time the O arrives and the time the N arrives.
You need some kind of end-of-packet marker (the CR/LF are fine for this), and you read and store data until the marker arrives. Do not store the marker.
Only test the data in the array when the end marker arrives, then reset the array and index.
But also your code will not work for me. You asked me, if I am sending \r or \n too. Sorry, but I don't know. Sorry for this question, but is the little menu at the bottom of the Arduino serial monitor to way to decided if I am sending \r and \n? I tested all four possibilities, but your code will not work for me.
SerialRead() only reads one character at a time so inSerial[i] will never contain two characters. You need to test the two consecutive characters to spot them. Something like this
if (inSerial[i] == '\n')
{
if (inSerial[i-1] == '\r')
{
Check_Protocol(inSerial);
}
}
Be careful if you try to use use
if (inSerial[i] == '\n' && inSerial[i-1] == '\r')
because on the first iteration inSerial[i-1] will be outside the bounds of the array, but you could use it if you skip the test when i == 0