How do I check if a button is held down on reset?

Okay, so I have a keypad installed, and I have things stored in EEPROM, and I want the Arduino to reset the EEPROM Memory if, and only if a certain button was held down during power-on or reset.

How do I do this?

Just read the button/key in your setup function.


Rob

Umm... I don't think its as simple as that... I'm using the this keypad.h found here: http://www.arduino.cc/playground/Code/Keypad

Any ideas on how I can check if the * key is pressed down while in the setup routine?

Tnx!

I'm not sure, all I know is when i put this inside the setup it doesn't work...

void setup(){
  Serial.begin(9600); //initialise the serial port (9600 baud)
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  char key = keypad.getKey();
  if (key == '*') {
     Serial.println("Password Reset");
  }
}

Any idea's why that won't work?

Yes, it works perfect inside the loop code.

Here’s the full code:
(Don’t mind the messy coding, I haven’t gotten around to cleaning it up yet)

//////////////////////////////////////////////////////////////////
//©2011 bildr
//Released under the MIT License - Please reuse change and share
//Reads the keys pressed on a keypad 
//Thanks to Alexander Brevig for his original keypad code.
/////////////////////////////////////////////////////////////////

#include <Keypad.h>  //needs to be in the library folder (Arduino/Libraries)

String password = "1234";
String passwordEntered;
int changePWD = 0;
int Auth = 0;


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

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

void setup(){
  Serial.begin(9600); //initialise the serial port (9600 baud)
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
  char key = keypad.getKey();
  if (key == '*') {
     Serial.println("Password Reset");
  }
}
  
void loop(){
  char key = keypad.getKey();
  if (key != NO_KEY){
    Serial.println(key); //print to serial the key that has been pressed
  }
}

//take care of some special events
void keypadEvent(KeypadEvent key){
  switch (keypad.getState()){
    case PRESSED:
    switch (key){
        case '#':
                if (changePWD == 1) {
                 resetChangepassword();
                 Serial.println("Canceled");
                 tone(9, 2048,1000);
                 break;
                }
                
                if (changePWD == 2) {
                  resetChangepassword();
                  Serial.println("Canceled");
                  tone(9, 2048,1000);
                  break;
                }
                
                if (changePWD == 3) {
                  resetChangepassword();
                  Serial.println("Canceled");
                  tone(9, 2048,1000);
                  break;
                }
                
                if (changePWD == 0) {
                checkPassword();
                resetPassword();
                break;
                }
                
          case '*':
                  if (changePWD == 1) {
                    changePWD = 2;
                    Serial.println("Enter Password");
                    tone(9, 2048,150);
                    delay(200);
                    tone(9, 2048,150);
                    delay(200);
                    tone(9, 2048,150);
                    break;
                  }
                  
                  if (changePWD == 2) {
                    resetChangepassword();
                    Serial.println("Canceled");
                    tone(9, 2048,1000);
                    break;
                  }
                  
                  if (changePWD == 3) {
                    password = passwordEntered;
                    Serial.println("Password Changed to:");
                    Serial.println(passwordEntered);
                    resetChangepassword();
                        tone(9, 2048,150);
                        delay(200);
                        tone(9, 2048,150);
                        delay(200);
                        tone(9, 2048,500);
                        delay(600);
                        tone(9, 2048,150);
                        delay(200);
                        tone(9, 2048,150);
                    break;
                  }
                  
                  if (changePWD == 0) {
                  changePassword();
                  resetPassword();
                  break;
                  }
                  
        default:
                if (changePWD == 1) {
                 resetChangepassword();
                 tone(9, 2048,1000);
                 break;
                }
                if (changePWD == 2) {
                 changePWD = 3; 
                }
                tone(9, 2048,150); 
                passwordEntered = passwordEntered + key;
                break;
      }
  }
}

void checkPassword() {
  if (password == passwordEntered) {
    Serial.println("Success");
    tone(9, 2048,150);
    delay(200);
    tone(9, 2048,150);
    delay(200);
    tone(9, 2048,150);
    delay(200);
    tone(9, 2048,150);
    delay(200);
    tone(9, 2048,150);
  } else {
    Serial.println("Wrong");
    tone(9, 2048,1000);
  }
}

void changePassword() {
  if (password == passwordEntered && changePWD == 0) {
    changePWD = 1;
    Serial.println("One more *");
    tone(9, 2048,150);
  } else {
    changePWD = 0;
    Serial.println("Wrong");
    tone(9, 2048,1000);
  }
}

void resetPassword() {
  passwordEntered = "";
}

void resetChangepassword() {
  passwordEntered = "";
  changePWD = 0;
}

Adding a delay works! Thanks guys!