I'm trying to switch an LED on and off only with one character from my keyboard. I was simply unable to find a code like this, however I'm sure it is possible
This isn't work, but I'm not sure why, It always gives me 0 through serial monitor. I have an idea why but I'm not sure about it and I'm also not 100% sure how this communication works when I press "a".
Sure. byteRead still has the value 'a' so it still gets into that case and triggers a toggle. It will just keep doing that every time loop repeats until byteRead gets a new value. Try setting byteRead to something else in that case statement. Or try sending a different character.
"That delay(1) is in that code to compensate for the time it takes for the character to be received by the hardware USART. You have to take into consideration that the serial data is being received at, more than likely, a slower speed than it takes for the microcontroller to process instructions.
Picture this: if the microcontroller and the USART were in a foot race, the microcontroller would win every time.
This means that you have to introduce delays to let the USART catch up to the microcontroller.
I assume that you're communicating at some speed faster than 9600 bps, because that code would not work if you were at 9600 bps or lower. This is because it takes over 1 millisecond to receive each character at 9600 bps.
I would recommend using what I posted above, as that is a logical way of receiving the data (although it could be improved by adding time-outs and other fail-safes).
"
This means that you have to introduce delays to let the USART catch up to the microcontroller.
Introducing a delay, and waiting for the data to arrive are not the same thing.
Think about it; at 9600 bits per second, the minimum delay between characters is a bit more than a millisecond, and the maximum delay is potentially infinite(or at least until the heat-death of the Universe).
Don't use delay ().