Go Down

Topic: Waiting for Key pressed (keypad) (Read 8113 times) previous topic - next topic

RaphaelAmin

Hey people,

I'm working on a little project. It's an automatized music keyboard. I am using a 4x3 keypad and a LCD.

The doubt is... How can I make the program to wait if no key is pressed, and if it is it continues with the "if (key != NO_KEY) {...}."

I tried "while (key == NO_KEY);" before the "if" but it brings me to an infinite loop no matter if keys are pressed or not.

And if I leave just the "if" it goes on trough the program, and I dont want it to happen.

Thanks.

Catcher

Did you get an infinite loop because you didn't check the status of the keys inside the loop?
If you did then you should probably post the code.

Pseudocode:
Code: [Select]

boolean NO_KEY = true;

while(NO_KEY)
{
      if(any keys are pressed)
      {
           NO_KEY = false;
      }
}

RaphaelAmin

Its like this:

Code: [Select]

    int key = KeypadX.getKey();
    while(key == NO_KEY) {
      Serial.println("infinite while); // Just to check if it's actually in this infinite loop, and it is.
    }
    if (key != NO_KEY) {  // it works if there is a short function and I dont have to wait for the key
    switch(key) {
      case '1':
      lcd.print(key);
      break;
      }
    }


The NO_KEY is used by the library "keypad.h". I don't know how it exacly works. Actually I have no idea.  :P

Don't know if I made myself clear...

RaphaelAmin

It seems that while/for makes the keypad stop working. It seems to...

Anybody?

WizenedEE

How do you expect the key variable to change while you're looping? Put in the getKey function itself, or call it within the while loop, so that you get the current value.

Code: [Select]

int key = KeypadX.getKey();
while (key == NO_KEY) key = KeypadX.getKey();

Catcher

It seems you did not update the value of the "key" variable inside the while loop. The value is assigned before the loop and will always stay that way until you update it.

Code: [Select]

    int key = KeypadX.getKey();
    while(key == NO_KEY) {
      key = KeypadX.getKey(); //UPDATE VALUE
      Serial.println("Waiting for press"); //This will print as long as no keys are pressed
    }
   
    lcd.print(key);

mstanley

Quote
The NO_KEY is used by the library "keypad.h". I don't know how it exacly works. Actually I have no idea.


When I wrote the examples for using the keypad library I wanted people to know about NO_KEY.  That may have been a mistake.

Anyway, the code
Code: [Select]
if (key != NO_KEY)
{
     // This code in here will only run if you press a key.
}
is telling you that any code you put inside the brackets of this if statement will run only if a valid key has been pressed.

It may seem more logical to you to write it thus:
Code: [Select]

if (key)
{
     // This code in here will only run if you press a key.
}


-Mark Stanley

RaphaelAmin

Thank all of you!

It was so obvious but still for me it was a problem to understand. It's solved! the getkey attribution should be inside the while like you said. :)

So I can keep walking with my little project. I have 1 week to finish it.  :smiley-yell:

Thank you again!

Ajay777

#include<LiquidCrystal.h>
#include<Keypad.h>
#include<Servo.h>

int redled = 11;
int greenled = 12;
int yellowled = 13;
int buzzer = 10;
int pos = 0;
#define PasswordLength 8
char Data[PasswordLength];
char Master[PasswordLength] = "4758690";
byte datacount = 0, mastercount = 0;
bool paaIsGood;
char customKey;

const byte COLS = 3;
const byte ROWS = 4;
char keys [ROWS] [COLS] ={
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte rowPins[ROWS] = {5,4,3,2};
byte colPins[COLS] = {8,7,6};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS);

LiquidCrystal lcd(A0,A1,A2,A3,A4,A5);
Servo myservo;

void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
  myservo.attach(9);
  pinMode(redled, OUTPUT);
  pinMode(greenled, OUTPUT);
  pinMode(yellowled, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

void loop()
{
  digitalWrite(redled, HIGH);
  digitalWrite(greenled, HIGH);
  digitalWrite(yellowled, HIGH);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Initialising");
  for(int n = 13; n<= 16;n++){
    lcd.setCursor(n,0);
    lcd.print(".");
    delay(250);
  }
   
  digitalWrite(redled, LOW);
  digitalWrite(greenled, LOW);
  digitalWrite(yellowled, LOW);

  delay(1000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("PLEASE ENTER THE");
  lcd.setCursor(0,1);
  lcd.print("    PASSWORD");
  delay(2000);

  lcd.noDisplay();
  delay(150);
  lcd.display();
  delay(150);
  lcd.noDisplay();
  delay(150);
  lcd.display();
  delay(150);
  lcd.noDisplay();
  delay(150);
  lcd.display();
  delay(150);

  delay(1000);

  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("PASSWORD: ");
  lcd.setCursor(0,1);
 
 char customKey = keypad.getKey();
 if(customKey){
     Data[datacount] = customKey;
     tone(buzzer, 3000);
     digitalWrite(yellowled, HIGH);
     lcd.setCursor(datacount,1);
     lcd.print("*");
     datacount ++;
}


    digitalWrite(yellowled, LOW);
 
  if(datacount == PasswordLength)
  {
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("ACCESS ");
    if(!strcmp(Data,Master))
    {
      lcd.setCursor(8,0);
      lcd.print("GRANTED");
      Serial.println("ACCESS GRANTED");
      for(pos = 0;pos<180;pos++)
      {
        myservo.write(pos);
        digitalWrite(greenled,HIGH);
        delay(15);
      }
    }
      else
      {
        lcd.clear();
        lcd.setCursor(0,0);
        lcd.print("INVALID PASSWORD");
        lcd.setCursor(0,1);
        lcd.print("Try Again");
        digitalWrite(redled, HIGH);
      }
    }
}
    void clearData()
    {
      while(datacount != 0)
      {
        Data[datacount --]=0;
      }
      return;
    }

   






my code is not stopping at the if(customKey)
 can anyone help me....

Ajay777

my code is continuously looping at skipping at custom Key

Could anyone help me with this?

thanks

PaulS

Quote
my code is continuously looping at skipping at custom Key
So why do you have pages and pages of improperly posted code, if your keypad is not working?

Get rid of all the crap not related to reading from the keypad. Address that issue FIRST.
The art of getting good answers lies in asking good questions.

UKHeliBob

Code: [Select]
char customKey;  //a global variable
Code: [Select]
 char customKey = keypad.getKey();  //a completely different variable with the same name local to loop()
Code: [Select]
 if (customKey)  //which of the two variables will this test ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up