2 Replies Latest reply on Nov 2, 2017 1:57 AM by obulorn

    Arduino- Wait For Key Press without Library

    obulorn

      I need help, I have a circuit setup with input (74hc165) and output (74hc595) shift registers.  Am trying out with 4x4 matrix keypad (for thats what i have available for now) but this will end up 8x8 matrix keypad. I have put a firmware together and that is running ok and returning the key press. But my challenge is;  how can I make Arduino loop function wait for a keypress. Presently it continously returns the last key pressed. I played around it without luck; I adopted the code below from online, this only wait for the initial time(ie without any key pressed), the moment a key is press, it runs with it  and return that key until another is press.

       

      const unsigned int DebounceDelay = 15;
       const long AutoRepeatDelay = 125;
       const unsigned int KeyWait = 25;
      
      int WaitKey()
      {
          int key;
          //    wait for all keys to be released 
          key = KeyDown();
      
          if (key == 0)
          {
              long repeatTime = millis() + AutoRepeatDelay;
              do
              {
                          // wait a bit
                  delay(KeyWait);
                  //    see if a key is still down 
                  if (KeyDown() != 0)
                  {
                      //    debounce the key release 
                      delay(DebounceDelay);
                      if (KeyDown() != 0)
                      {
                          key = 0;
      
                          break;
                      }
                      else if (millis()>repeatTime)
                              {
                                  
                                  //    effect auto-repeat
                                  break;
      
                              }
                      //}
                  }
      
              } while (true);
      
              /// }
      
             //    wait for a key to be pressed 
              while (key != 0)
              {
                  key = KeyDown();
                  if (key == 0)
                  {
                      //    debounce the key down
                      delay(DebounceDelay);
                  }
                  else
                  {
                      delay(KeyWait);
                  }
              }
          
          }
      return key;
          }