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

AWOL

Yes, that's right.
The flag is simply a global "bool" variable called, say, "keypadEnabled" and initialised to true.
Or ""keypadDisabled" and initialised to false.
"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

Alright, I get that. So if
Code: [Select]
boolean panelDisabled = false;
then
Code: [Select]

    case 'D':
      if (panelDisabled == LOW){
        //Code to ignore keystrokes
    }else{
      panelDisabled = !panelDisabled;
      break;
     }


If that's right, all I have to do now is figure out what code it takes to ignore those keystrokes
"The impossible is merely a matter of time and will!" - Artemis Zogg

AWOL

Code: [Select]
if (panelDisabled == LOW){

I'd prefer 'false', but, whatever.
"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

Mate, you're incredible! If I was in the UK, I'd shout you a pint! Thanks so much  XD
"The impossible is merely a matter of time and will!" - Artemis Zogg

TrippySquidsman

I've hit a wall again! Been working all day (between studies) on trying to understand this damn C++ stuff. I can't seem to get my head around what code I need to write in order to make the arduino ignore input from the keypad. I've again scoured the web, but to no avail. Please help!  :smiley-sweat:

Code: [Select]

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

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

boolean panelDisabled = false;

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 solenoidPin = A1; //maps solenoidPin 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
  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 'D':
       panelDisabled = !panelDisabled;
       password.reset();
       Serial.println("Panel disabled!");
       digitalWrite(ledA4Pin, HIGH);
       digitalWrite(led10Pin, LOW);
       digitalWrite(led11Pin, LOW);
       digitalWrite(led12Pin, LOW);
       digitalWrite(ledA2Pin, LOW);
       switch (key){   

         case 'E':
           password.reset();
           panelDisabled = !panelDisabled;
           Serial.println("Panel enabled!");
           digitalWrite(ledA4Pin, LOW);
           digitalWrite(led10Pin, LOW);
           digitalWrite(led11Pin, LOW);
           digitalWrite(led12Pin, LOW);
           digitalWrite(ledA2Pin, HIGH);
           break;
       
         case 'D':
           password.reset();
           Serial.println("Panel aready disabled!");
           break;

        default :
           password.reset();
           Serial.println("Panel is disabled, press E to reenable");
           break;
       }
       
    case 'A':
      guessPassword();
      digitalWrite(led10Pin, LOW);
      digitalWrite(led11Pin, LOW);
      digitalWrite(led12Pin, LOW);
      break;

    case 'B':
      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;
    }
  case RELEASED:
    switch (key){
    default :
      digitalWrite(led10Pin, LOW);
      analogWrite(speakerPin, 0);
      break;
    }
    break;
  }
}

/*void disablePanel(KeypadEvent key){
  switch (keypad.getState()){
  case PRESSED:
   panelDisabled = !panelDisabled;
   password.reset();
   Serial.println("Panel disabled!");
   digitalWrite(ledA4Pin, HIGH);
   digitalWrite(led10Pin, LOW);
   digitalWrite(led11Pin, LOW);
   digitalWrite(led12Pin, LOW);
   digitalWrite(ledA2Pin, LOW);
   switch (key){   

     case 'E':
        password.reset();
        panelDisabled = !panelDisabled;
        Serial.println("Panel enabled!");
        digitalWrite(ledA4Pin, LOW);
        digitalWrite(led10Pin, LOW);
        digitalWrite(led11Pin, LOW);
        digitalWrite(led12Pin, LOW);
        digitalWrite(ledA2Pin, HIGH);
        break;
       
     case 'D':
        password.reset();
        Serial.println("Panel aready disabled!");
        break;

      default :
        password.reset();
        Serial.println("Panel is disabled, press E to reenable");
        break;
    }
  case RELEASED:
    switch (key){
    default :
      digitalWrite(led10Pin, LOW);
      analogWrite(speakerPin, 0);
      break;
    }
    break;
  }
} */
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 times!");
  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);
  }
}







- TrippySquidsman (Tom)
"The impossible is merely a matter of time and will!" - Artemis Zogg

PaulS

Code: [Select]
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad

Why is this called twice?

Code: [Select]
    switch (key){   

     case 'D':
       panelDisabled = !panelDisabled;
       password.reset();
       Serial.println("Panel disabled!");

Every time the D key is pressed you will toggle the state of the panelDisabled variable (enabled to disabled or disabled to enabled), but print out that the panel is disabled.

Once that toggling occurs, you have another whole switch statement that looks at the value of the one key that triggered the callback. Why?

When the D key is pressed, set the value of panelDisabled to true, print whatever is appropriate, diddle the LEDs, and return.

When the E key is pressed, set the value of panelDisabled to false, print whatever is appropriate, diddle the LEDs, and return.

When any other key is pressed, look at the value of panelDisabled. If false, return. Otherwise, process the key.

The easiest way to do this is like this:

Code: [Select]
if(key == 'D')
   // disable the input
else if(key == 'E')
   // enable the input
else if(panelEnabled)
{
   switch(key)
   {
      // all the cases that have not yet been handled
   }
}

This code could go in a function, handleKeyPress(key), that gets called whey the keypad state is PRESSED.

TrippySquidsman


Code: [Select]
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad

Why is this called twice?


lol code typo? I probably forgot to delete one of them when I was messing around with creating new functions and whatnot



Code: [Select]
    switch (key){   

     case 'D':
       panelDisabled = !panelDisabled;
       password.reset();
       Serial.println("Panel disabled!");

Every time the D key is pressed you will toggle the state of the panelDisabled variable (enabled to disabled or disabled to enabled), but print out that the panel is disabled.


I've optimized that now. I've removed the whole disablePanel function and put it into the keypadEvent function, like this:
Code: [Select]

void keypadEvent(KeypadEvent key){
  switch (keypad.getState()){
  case PRESSED:
    digitalWrite(ledA2Pin,LOW);
    switch (key){   

    case 'D':
      if (panelDisabled == false){
        panelDisabled = !panelDisabled;
        Serial.println("Panel disabled!");
      }
      else{
        password.reset();
        Serial.println("Panel already disabled!");
        digitalWrite(ledA4Pin, HIGH);
        digitalWrite(led10Pin, LOW);
        digitalWrite(led11Pin, LOW);
        digitalWrite(led12Pin, LOW);
        digitalWrite(ledA2Pin, LOW);
      }
        switch (key){   

        case 'E':
          password.reset();
          panelDisabled = !panelDisabled;
          Serial.println("Panel enabled!");
          digitalWrite(ledA4Pin, LOW);
          digitalWrite(led10Pin, LOW);
          digitalWrite(led11Pin, LOW);
          digitalWrite(led12Pin, LOW);
          digitalWrite(ledA2Pin, HIGH);
          break;

        case 'D':
          password.reset();
          Serial.println("The panel is aready disabled. Press E to reenable");
          break;

        default :
          password.reset();
          Serial.println("Panel is disabled, press E to reenable");
          break;
        }
       break;

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

    case 'B':
      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;
    }
  case RELEASED:
    switch (key){
    default :
      digitalWrite(led10Pin, LOW);
      analogWrite(speakerPin, 0);
      break;
    }
    break;
  }
}




Once that toggling occurs, you have another whole switch statement that looks at the value of the one key that triggered the callback. Why?


I'm trying create a new switch statement that responds only to case 'D': and default : I was under the impression that if anything other than 'D' was pressed, it would be ignored. This worked, to a certain extent, but it also resulted in this switch statement being terminated and returning to the keypadEvent function, totally defeating the purpose of having that switch statement.



When the D key is pressed, set the value of panelDisabled to true, print whatever is appropriate, diddle the LEDs, and return.

When the E key is pressed, set the value of panelDisabled to false, print whatever is appropriate, diddle the LEDs, and return.

When any other key is pressed, look at the value of panelDisabled. If false, return. Otherwise, process the key.

The easiest way to do this is like this:

Code: [Select]
if(key == 'D')
   // disable the input
else if(key == 'E')
   // enable the input
else if(panelEnabled)
{
   switch(key)
   {
      // all the cases that have not yet been handled
   }
}

This code could go in a function, handleKeyPress(key), that gets called whey the keypad state is PRESSED.


That's so incredibly neat! However, that puts me back to square one - how do I disable the input? What's the code to do that? That is the purpose of those crazily placed switch statements. I've redone the script, and I can post it if you want. In the meantime, I'll look at trying to implement your example. Thanks heaps!  :smiley-mr-green:
"The impossible is merely a matter of time and will!" - Artemis Zogg

PaulS

Code: [Select]
how do I disable the input?
That's what the panelEnabled flag is all about. But, that flag by itself doesn't mean squat. You have to look at the flag every time a key is pressed. If it is true, process the keypress. If not, ignore it. The keypad library won't do it for you.

Go Up