Go Down

Topic: Ignore keypad input (Read 1 time) previous topic - next topic

TrippySquidsman

Hello everyone!
     Sorry if there is already a post on this - I've searched the interwebs (and the Arduino forum) until my fingers bled  ;)
     I'm having trouble writing my first code (first real code, that is), which will enable me to supply power to a
     solenoid when the correct code is typed into a 4x4 alphanumeric keypad.

     Now, the problem is, is that I want to cram a couple of cool(ish) features into this sketch. The keypad is set out like this:
                                                                         __________
                                                                         |1  2  3  A |
                                                                         |4  5  6  B |
                                                                         |7  8  9  C |
                                                                         |G  F  E  D |
                                                                         --------------

     lol, that looks terrible... but you get the idea! The feature that is killing me is when I press the ''D" button, I want the arduino
     to ignore all further input from the keypad until the "E" button is pressed. I've only started learning C++ in the past few days,
     so it may be obvious that the gist of this script originated from somewhere else; I've simply changed the pin associations and
     a few other things. My problem is that when the "D" button is pressed, the script launches into an endless loop of "Panel disabled!"
     serial outputs, and can only be stopped by pressing the "E" key twice. I'll post the whole code here, but if you want to skip
     to the problematic area, go to line 119.

Code: [Select]

#include <Keypad.h>
#include <Password.h>

Password password = Password( "1337" );
int alarmCounter = 0;

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'0','F','E','D'}
};
byte rowPins[ROWS] = {
  5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {
  9, 8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

byte led10Pin = 10; //maps ledPins to physical pins
byte led11Pin = 11;
byte led12Pin = 12;
byte ledA2Pin = A2;
byte ledA3Pin = A3;
byte ledA4Pin = A4;

int speakerPin = A5; //maps speakerPin to analog pin 5
int motorPin = A1; //maps motorPin to analog pin 1

void setup(){
  Serial.begin(9600);
  pinMode(led10Pin, OUTPUT); // sets the digital pin as output
  pinMode(led11Pin, OUTPUT); // sets the digital pin as output
  pinMode(led12Pin, OUTPUT); // sets the digital pin as output
  pinMode(ledA2Pin, OUTPUT); // sets the digital pin as output
  pinMode(ledA3Pin, OUTPUT); // sets the digital pin as output
  pinMode(ledA4Pin, OUTPUT); // sets the digital pin as output
  pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
  digitalWrite(led10Pin, LOW); // sets the LED off
  digitalWrite(led11Pin, LOW); // sets the LED off
  digitalWrite(led12Pin, LOW); // sets the LED off
  digitalWrite(ledA2Pin, LOW); // sets the LED off
  digitalWrite(ledA3Pin, LOW); // sets the LED off
  digitalWrite(ledA4Pin, LOW); // sets the LED off
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  digitalWrite(ledA2Pin,HIGH); //sets status led to HIGH
  Serial.println("System Ready.");
}

void loop(){
  char key = keypad.getKey();
}

//take care of some special events
void keypadEvent(KeypadEvent key){
  switch (keypad.getState()){
  case PRESSED:
    digitalWrite(ledA2Pin,LOW);
    switch (key){

    case 'E':
      enablePanel();
      digitalWrite(ledA4Pin, LOW);
      digitalWrite(ledA2Pin, HIGH);
      digitalWrite(led10Pin, LOW);
      digitalWrite(led11Pin, LOW);
      digitalWrite(led12Pin, LOW);
      break;     

    case 'D':
      disablePanel();
      digitalWrite(ledA4Pin, HIGH);
      digitalWrite(ledA2Pin, LOW);
      digitalWrite(led10Pin, LOW);
      digitalWrite(led11Pin, LOW);
      digitalWrite(led12Pin, LOW);
      break;

    case 'B':
      guessPassword();
      digitalWrite(led10Pin, LOW);
      digitalWrite(led11Pin, LOW);
      digitalWrite(led12Pin, LOW);
      break;

    case 'A':
      password.reset();
      resetSound();
      digitalWrite(led10Pin, LOW);
      digitalWrite(led11Pin, LOW);
      digitalWrite(led12Pin, LOW);
      digitalWrite(ledA2Pin, HIGH);
      break;

    default :
      if (key != NO_KEY){
        Serial.print("Key Pressed = ");
        Serial.println(key);
      }
      password.append(key);
      digitalWrite(led10Pin, HIGH);
      analogWrite(speakerPin, 128);
      break;
    }
    break;
  case RELEASED:
    switch (key){
    default :
      digitalWrite(led10Pin, LOW);
      analogWrite(speakerPin, 0);
      break;
    }
    break;
  }
}

void disablePanel(){
  digitalWrite(ledA4Pin, HIGH);
 
  char key = keypad.getKey();
  Serial.println("Panel disabled!");
  password.reset();
  switch(key){
  case 'E':
    enablePanel();
    break;
  default:
    disablePanel();
    break;
  }
}

void enablePanel(){
  Serial.println("Panel enabled!");
  password.reset();
}

void guessPassword(){
  if (password.evaluate()){
    digitalWrite(led10Pin,HIGH);
    digitalWrite(ledA2Pin,HIGH);
    Serial.println("Password Correct");
    openSound();
    digitalWrite(motorPin,HIGH);
    delay(3000);
    digitalWrite(led10Pin,LOW);
    digitalWrite(ledA2Pin,LOW);
    digitalWrite(motorPin,LOW);
    delay(500);
    digitalWrite(ledA2Pin,HIGH);
    password.reset();
    alarmCounter = 0;
  }
  else{
    digitalWrite(led10Pin,LOW);
    Serial.println("Password Incorrect!");
    password.reset();
    alarmCounter++;
    if (alarmCounter >= 3){
      alarmSound();
      Serial.println("Alarm still active!");
      Serial.println("Entering another wrong password will sound alarm again!");
      Serial.println("To reset alarm please enter the right password!");
    }
    else{
      errorSound();
    }
  }
}

void alarmSound() {
  Serial.println("Wrong password 3 time!");
  Serial.println("Sounding ALARM!!!");
  for (int aCount=0;aCount<5;aCount++){
    analogWrite(speakerPin, 128);
    digitalWrite(led10Pin, HIGH);
    digitalWrite(led11Pin, HIGH);
    digitalWrite(led12Pin, LOW);
    digitalWrite(ledA2Pin, LOW);
    delay(500);
    digitalWrite(led10Pin, LOW);
    digitalWrite(led11Pin, LOW);
    digitalWrite(led12Pin, HIGH);
    digitalWrite(ledA2Pin, HIGH);
    delay(500);
    analogWrite(speakerPin, 0);
    digitalWrite(led10Pin, HIGH);
    digitalWrite(led11Pin, HIGH);
    digitalWrite(led12Pin, LOW);
    digitalWrite(ledA2Pin, LOW);
    delay(500);
    digitalWrite(led10Pin, LOW);
    digitalWrite(led11Pin, LOW);
    digitalWrite(led12Pin, HIGH);
    digitalWrite(ledA2Pin, HIGH);
    delay(500);
  }
}

void resetSound() {
  Serial.println("Password reset");
  int n = 100;
  for (int vez=0;vez<6;vez++){
    for (int _=0;_<100;_++){
      delayMicroseconds(n);
      digitalWrite(speakerPin, HIGH);
      digitalWrite(led10Pin, HIGH);
      digitalWrite(ledA2Pin,HIGH);
      delayMicroseconds(n);
      digitalWrite(speakerPin, LOW);
      digitalWrite(led10Pin, LOW);
    }
    delay(20);
  }
}

void errorSound() {
  int n = 2000;
  for (int vez=0;vez<1;vez++){
    for (int _=0;_<200;_++){
      delayMicroseconds(n);
      digitalWrite(speakerPin, HIGH);
      digitalWrite(led12Pin, HIGH);
      digitalWrite(ledA4Pin, HIGH);
      delayMicroseconds(n);
      digitalWrite(speakerPin, LOW);
      digitalWrite(led12Pin, LOW);
      digitalWrite(ledA4Pin, LOW);
    }
    delay(500);
    digitalWrite(ledA2Pin,HIGH);
  }
}

void openSound() {
  int n = 300;
  for (int vez=0;vez<3;vez++){
    for (int _=0;_<100;_++){
      delayMicroseconds(n);
      digitalWrite(speakerPin, HIGH);
      delayMicroseconds(n);
      digitalWrite(speakerPin, LOW);
    }
    delay(100);
  }
}



     Thanks in advance for all the help!
"The impossible is merely a matter of time and will!" - Artemis Zogg

AWOL

Don't you need to test getState before reading the keycode in "disablePanel"?
Like you do here:
Code: [Select]
switch (keypad.getState()){
  case PRESSED:
    digitalWrite(ledA2Pin,LOW);
    switch (key){
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TrippySquidsman

#2
Mar 31, 2011, 01:30 pm Last Edit: Mar 31, 2011, 01:32 pm by TrippySquidsman Reason: 1
Holey crap that was quick!

Yeeaahh... do you mean test to see what the last keypress was? I was thinking of that, but I still can't think of a way to get the arduino to ignore the keystrokes even if I knew what the last pressed key was. I seriously don't know much about the language - this is my way of learning!  :smiley-sweat:


EDIT: lol my browser must of crapped out - I didn't get the code. Hang ten...
"The impossible is merely a matter of time and will!" - Artemis Zogg

AWOL

Another way of doing it would be simply set/reset a flag in your main loop that marked the keypad as disabled.
If the flag says the keypad is disabled, simply don't act on a keypress.
The you don't need "disablepanel" and "enablePanel"
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

TrippySquidsman

That's perfect! Only one problem: how do I do that?  key.reset() wouldn't work...
I'm guessing that I'd change  " case 'D': "  to perform the action instead of redirecting to "disablePanel"
"The impossible is merely a matter of time and will!" - Artemis Zogg

Go Up