Reading keypad/checking for key press in setup function

Hi. Is there a way to check if a key is pressed while in setup function?

I'm using a 4*4 keypad.

Using if keypressed didn't work, so i tried to read the state of pins used for the keypad (so if a state is HIGH i can say a key is pressed).

When checking the states with digitalRead(), i get 1,1,1 and 1 for rows and fluctuating values of columns (i can get 0,0,0,0 ; 0,1,1,0; or even 1,1,1,1 without pressinng any key).

So i can't see how can i check if a keypad is pressed while in setup function.

Any ideas? thanks.

hi fjtheknight,

You’ll have to post your code for us to help. We need to see it to make sure that you have set things up correctly.

Click on the </> button above the edit window and paste your code in the block.

When you call getKey(), it determines if any key is pressed AT THAT INSTANCE. It does NOT wait for a key to be pressed. Are you expecting that it will?

To add to Paul's comment. If you need to wait, just use getKey in a while loop till a key is pressed.

Hey, thanks for the quick reply.

patduino:
hi fjtheknight,

You’ll have to post your code for us to help. We need to see it to make sure that you have set things up correctly.

Click on the </> button above the edit window and paste your code in the block.

My code is a little bit big. The keypad is is working fine whith every thing in loop function :slight_smile:

PaulS:
When you call getKey(), it determines if any key is pressed AT THAT INSTANCE. It does NOT wait for a key to be pressed. Are you expecting that it will?

That’s why i tied to check if a column digital pin is HIGH (=keypressed in that column) at that exact moment (so if you are pressing while the program checks, it will read a HIGH value),but as mentioned before, i didn’t get a constant LOW value if all keys are not pressed. Maybe that’s related to the keypad library.

sterretje: To add to Paul's comment. If you need to wait, just use getKey in a while loop till a key is pressed.

Thanks for your suggestion. What i want to achieve is something like that:

void setup() {

if (keypressed) {
change some conditions//else keep default values

}

void loop() {

do somthing with those conditions
}

I think i can do that in loop function, but i wanted to make it in setup function (so it feels like you set your conditions before you start working :D )

I think i can do that in loop function, but i wanted to make it in setup function (so it feels like you set your conditions before you start working :D )

byte key = keypad.getKey();
while(key == NO_KEY)
{
   key = keypad.getKey();
}

Or, you could use the waitForKey() method. 8)

void setup()
{
...
...
bool keypressed.=false;

while(millis() < 5000)
{
  if(keypad.getKey() != NO_KEY)
  {
    keypressed = true;
    break;
  }
}
if(keypressed == true)
{
  ...
  ...
}

This will give around 5 seconds (depending how long the code before it takes) to press a key.

Not compiled or tested.

PaulS:

byte key = keypad.getKey();

while(key == NO_KEY)
{
  key = keypad.getKey();
}




Or, you could use the waitForKey() method. 8)

Thanks for the suggestion, ut waitForKey() method stops everything until a key is pressed, i just need to change some variales values if key is pressed and work with default values if not.

sterretje:

void setup()

{


bool keypressed.=false;

while(millis() < 5000)
{
  if(keypad.getKey() != NO_KEY)
  {
    keypressed = true;
    break;
  }
}
if(keypressed == true)
{
  …
  …
}



This will give around 5 seconds (depending how long the code before it takes) to press a key.

Not compiled or tested.

I think that’s what i’m looking for :slight_smile: will try it quickly. Thanks

sterretje:

void setup()

{


bool keypressed.=false;

while(millis() < 5000)
{
  if(keypad.getKey() != NO_KEY)
  {
    keypressed = true;
    break;
  }
}
if(keypressed == true)
{
  …
  …
}



This will give around 5 seconds (depending how long the code before it takes) to press a key.

Not compiled or tested.

Hey. That worked :smiley: thanks ^^