1. 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.
    Dismiss Notice

Mega - clear keyboard buffer problem

Discussion in 'Arduino' started by flat5, Nov 29, 2016.

  1. flat5

    flat5 Member

    Joined:
    Oct 26, 2008
    Messages:
    866
    Likes:
    8
    Location:
    Amsterdam
    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 (text):

    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 = "";
      }
    }
     
     
  2. djsfantasi

    djsfantasi Member

    Joined:
    Mar 15, 2011
    Messages:
    161
    Likes:
    21
    Location:
    Metro Boston
    Use the Serial.available function to keep reading until there are no more characters in the buffer.
     
  3. flat5

    flat5 Member

    Joined:
    Oct 26, 2008
    Messages:
    866
    Likes:
    8
    Location:
    Amsterdam
    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.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. flat5

    flat5 Member

    Joined:
    Oct 26, 2008
    Messages:
    866
    Likes:
    8
    Location:
    Amsterdam

    Accepting the situation and will kludge around it.
     
  6. flat5

    flat5 Member

    Joined:
    Oct 26, 2008
    Messages:
    866
    Likes:
    8
    Location:
    Amsterdam
    This works.
    Code (text):

    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: Dec 1, 2016

Share This Page