Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Mega - clear keyboard buffer problem

Status
Not open for further replies.

flat5

Member
This is part of a Morse code test program.
After sending a select group of characters, the user types in the answers if this option is enabled.
If the user types in more characters than expected this causes trouble.
My attemps to remove extra bytes from the keyboard buffer have failed or just not helped.
Calling Serial.read() to clear the keyboard buffer does not help.
I don't know what else to do.
Code:
void type_tstring() // option - type in testString after a test run.
// **Problem** if more chars are entered than testStrRow. Serial.read() does not help to clear keyboard buffer.
{
  type_ans_len++; // do nothing till all Morse chars are sent (tString)
  if (type_ans_len == testStrRow) // now enter your answers (testStrRow is tString.length()
  {
  unsigned int i = 0; flag = 10; typeIn = "";
  Serial.println(F("Enter your answers or Space if you don't know a char"));
  do
  {
  if (keypress == 27 || i == testStrRow) break;
  i++; getChars(true); // returns char keypress
  typeIn = typeIn + keypress + " ";
  } while (i < testStrRow);

//  any Serial.read() routine here does not help

  Serial.println("your answers: " + typeIn); type_ans_len = 0; flag = 0; //typeIn = "";
  }
}
 
Done that.
Somehow it is not good enough.
If I set the prog to expect 2 chars and I give it three even a line like this will not work.
Serial.read(); Serial.read(); Serial.read(); Serial.read(); Serial.read();
or
keypress = Serial.read; keypress = Serial.read; keypress = Serial.read; keypress = Serial.read;
Something else has to be done. A different approach. I don't know what else to think.
 
This works.
Code:
void type_tstring() // option - type in testString after a test run.
{
  type_ans_len++; // do nothing till all Morse chars are sent (tString)
  if (type_ans_len == testStrRow) // now enter your answers (testStrRow is tString.length()
  {
  unsigned int i = 0; flag = 10; typeIn = ""; type_ans_len = 0;
  Serial.println(F("Enter your answers or Space if you don't know a char - ESC to quit"));
  do
  {
  if (keypress == 27 || i == testStrRow) break;
  i++; getChars(true); // returns char keypress
  typeIn = typeIn + keypress + " ";
  } while (i < testStrRow && keypress != 27);
  flag = 0;
  if (keypress != 27)
  {
  Serial.println("your answers: " + typeIn);
  }
  do
  {
  if (keypress == 27) return;
  keypress = Serial.read();
  } while (Serial.available() > 0);
  }
} // back to send_select()
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top