I'm designing a code to improve debugging by creating break points. Let me explain what I mean.
Here's my ideia: at the beggining of the loop, the program waits for some data in the serial communication. Once it receive anything, it runs the loop ONCE and wait for something in the serial communication again.
This way, I can make the program to run loop by loop, like there was a breakpoint in the beggining.
The problem is: when I type something into the serial, it keeps running again and again because I can't clear the serial buffer so the program undestants that new data is arriving in the serial.
I've tried Serial.flush(), but culdn't afford to get how it works.
Serial.flush() is almost useless. It just stops and waits for any outgoing characters to finish sending.
To empty the Serial.available() you must use Serial.read(). For example...
while(Serial.available()) {
Serial.read();
}
//the incoming Serial buffer is now empty, but there may be a character mid-way through reception that will be available to read at any moment.
The alreadyread variable is inside the serial.available and is not, cannot, be set and reset properly. Therefore blocking access to serial.read. Remove all aspects of alreadread and try again.
Also be sure to check the example sketches for the serial library. They contain the basics of the concepts you are working with.
So, I want the program to wait 'till I type anything into the serial before repeating the loop. I mean that I'm typing random character in the serial monitor (one per try).
I added a serial print to check the serial read and it's not writing anything back.
I'll work on some variations to this code and post the results later.
Read even if there is nothing in the buffer to read. What a great idea. I wonder why more people don't to that.
If you are using the Serial Monitor to send data, it NEVER sends a NULL, so stopping reading when you read a NULL means that you will never stop reading.