Problem with Serial.available() not returning to false

Hello,

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.

Does anyone have an ideia to make this right?

I'm leaving my cuyrrent code below.

Thank's!

bool jaLeu     = false;
bool prossegue = false;
bool led       = false;

void setup() {
  pinMode(13, OUTPUT);    

  Serial.begin(9600);

  Serial.println("Iniciando programa...");

}

void loop() {

  if ( Serial.available() ) {
    if ( !jaLeu ) {
      prossegue = true;
      jaLeu = true;
    }
  }
  else {
    jaLeu = false;
  }

  if (prossegue) {
    led = !led;
    digitalWrite(13,led);
    prossegue = 0;
    Serial.println("LED TOGGLED");
  } 
  Serial.flush();
}

Read the data

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.

Thank's for the answers!

I added a char variable only to read the serial data, but it's still not working.

Any other idea?

PS: I'm still triyng to make this work, no matter what way. Please, let me know if you have another algorithym fot this.

Here's my code now. I translated the variables names so it makes more sense in english

char trash;
bool alreadyRead = false;
bool repeat      = false;
bool led         = false;

void setup() {
  pinMode(13, OUTPUT);    

  Serial.begin(9600);

  Serial.println("Starting program...");

}

void loop() {

  if ( Serial.available() ) {
    if ( !alreadyRead ) {
      trash = Serial.read();
      repeat = true;
      alreadyRead = true;
    }
  }
  else {
    alreadyRead = false;
  }

  if (repeat) {
    led = !led;
    digitalWrite(13,led);
    repeat = 0;
    Serial.println("LED TOGGLED");
  } 
  Serial.flush();
}

Try putting

trash = Serial.read();

into a loop. Keep reading until Serial.available() is zero.

As mentioned, there may be more than just one character, make sure you read them all.

Thank's for the answers!

So, I thought " while (trash != 0 ) " would garantee that all serial data is read. However, it's still not working.

Here's how my loop looks now:

void loop() {

  if ( Serial.available() ) {
    if ( !alreadyRead ) {
      while ( trash != 0 ){
        trash = Serial.read();
      }
      repeat = true;
      alreadyRead = true;
    }
  }
  else {
    alreadyRead = false;
  }

  if (repeat) {
    led = !led;
    digitalWrite(13,led);
    repeat = 0;
    Serial.println("LED TOGGLED");
  } 
  Serial.flush();
}

Did you check what Serial.read returns when there is nothing to read?

Why not?

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.

Give us an example of what you would type into serial monitor and what results to expect from that entry.

Thank's for the answers!

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.

This is how my loop looks right now:

void loop() {

  if ( Serial.available() ) {
    if ( !alreadyRead ) {
      while ( trash != 0 ){
        trash = Serial.read();
        Serial.println(trash);
      }
      repeat = true;
      alreadyRead = true;
    }
  }
  else {
    alreadyRead = false;
  }

  if (repeat) {
    led = !led;
    digitalWrite(13,led);
    repeat = 0;
    Serial.println("LED TOGGLED");
  } 
  Serial.flush();
}
      while ( trash != 0 ){
        trash = Serial.read();
        Serial.println(trash);
      }

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.

Thank's to all the answers!

I wrote a variation of my code taking everything you all told me in consideration.

I stopped trying to read the buffer state and focoused on the data itself.

It worked perfectly!

Now I can run loop-by-loop.

Thank you all again!

Here's my final code:

int counter = 0;

bool repeat = false;
bool alreadyRead = false;

char command;

void setup() {
  Serial.begin(9600);
}

void loop() {
  do {
    if (Serial.available()) {
      command = Serial.read();
      alreadyRead = true;
    }
    else {
      alreadyRead = false;
    }
  }
  while (!alreadyRead);

  if (command == 'a') {
    // Everything inside this if will wait for permission to repeat
    counter++;
    Serial.println(counter);
  }
}