Password in Switch Case

Hi... I'm trying to make a password triggered event, but it doesn't quite work. The LED just turns on after uploading the code. I found some other tutorials on how to make a password triggered even, thats more detailed than this, but still wondering why this doesn't work? Any ideas?

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

//KEYPAD SETUP
const byte ROWS = 4; //DEFINE NUMBER OF ROWS ON KEYPAD
const byte COLS = 3; //DEFINE NUMBER OF COLUMNS ON KEYPAD
char keys[ROWS][COLS] = { //ARRAY FOR KEYPAD KEYS
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};//END ARRAY FOR KEYPAD KEYS

byte rowPins[ROWS] = {3, 10, 9, 2}; //PINS WHERE THE ROWS OF THE KEYPAD IS CONNECTED
byte colPins[COLS] = {17, 16, 15}; //PINS WHERE THE COLUMNS OF THE KEYPAD IS CONNECTED

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//MAKING KEYMAP FOR KEYPAD

const unsigned long debounceValue = 50UL; //SETTING FOR DEBOUNCE TIME IN MILLISECONDS
unsigned long debounceTime; //VARIABLE FOR DEBOUNCING KEYS PRESSED

boolean pass1 = 0;
boolean pass2 = 0;
boolean pass3 = 0;
boolean pass4 = 0;

const byte irsens = 7;      //DEFINE IR Sensor VARIABLE ON PIN7
const byte led = 12;      //DEFINE led ON PIN12


byte currentState = 1;        //MAKE VARIABLE CURRENTSTATE, SET IT TO START IN STATE 1 IN AUTOMODE

//INITIALIZE SETUP
void setup()
{
  Serial.begin(9600);        //SET BAUD RATE

  debounceTime = millis(); //DEBOUNCETIME = CURRENT TIME

  pinMode(irsens, INPUT);

  pinMode(led, OUTPUT);

  keypad.addEventListener(keypadEvent); //INITIALIZE CHECKUP ON KEYPAD USAGE
} // END OF SETUP

//INITIALIZE THE MAIN LOOP
void loop()
{
  char key = keypad.getKey();

  if (millis() - debounceTime >= debounceValue)
  {
    debounceTime = millis(); //re-initialize to the current time
  }



  switch (currentState)
  {
    //***************************
    case 1:
      if (pass1)
      {
        Serial.println ("Here!");
        pass1 = !pass1;
      }
      currentState = 2;
      break;
    //***************************
    case 2:
      if (pass2)
      {
        pass2 = !pass2;
      }
      currentState = 3;
      break;
    //***************************
    case 3:
      if (pass3)
      {
        pass3 = !pass3;
      }
      currentState = 4;
      break;
    //***************************
    case 4:
      if (pass4)
      {
        pass4 = !pass4;
      }
      currentState = 5;
      break;
    //***************************
    case 5:
      digitalWrite (led, HIGH);

      currentState = 5;
      break;
    //***************************
    default:
      currentState = 1;              //Back to state 1
  } // END SWITCH/CASE===========================================================
} //END IF STATEMENT=============================================================
//}//END MAIN LOOP=============================================================================================




void keypadEvent(KeypadEvent key) {

  //MANUAL MODE OPERATION PARAMETERS
  if (keypad.getState() == PRESSED) {

  if (key == '1') 
  {
     // if (key == '1') {
        pass1 = 1;
     // }
    }

    if (key == '2') 
    {
     // if (key == '2') {
        pass2 = 1;
      //}
    }

    if (key == '3') 
    {
      //if (key == '3') {
        pass3 = 1;
      //}
    }

    if (key == '4') 
    {
      //if (key == '4') {
        pass4 = 1;
      //}
    }


  }

}//END KEYPADEVENT FUNCTION

Never mind! :slight_smile: Got it.

Well, thought I had it... however, something is off with my math obviously, cause I have a hard time figuring out how to change my variables, to obtain what I want this program to do. So I would appreciate a little help. :slight_smile:

Like I said, Im trying to make a password function.. I want it to work like this:

When I press the key leading to where the password is needed, I need to put in 4 digits in the correct order for the LED to light up in the end. If a wrong key is pressed, I should return to State1 and start all over.

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

//KEYPAD SETUP
const byte ROWS = 4; //DEFINE NUMBER OF ROWS ON KEYPAD
const byte COLS = 3; //DEFINE NUMBER OF COLUMNS ON KEYPAD
char keys[ROWS][COLS] = { //ARRAY FOR KEYPAD KEYS
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};//END ARRAY FOR KEYPAD KEYS

byte rowPins[ROWS] = {3, 10, 9, 2}; //PINS WHERE THE ROWS OF THE KEYPAD IS CONNECTED
byte colPins[COLS] = {17, 16, 15}; //PINS WHERE THE COLUMNS OF THE KEYPAD IS CONNECTED

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//MAKING KEYMAP FOR KEYPAD


//LCD DISPLAY SETUP
LiquidCrystal lcd(7, 6, 5, 4, A0, 8);  //PINS WHERE THE LCD DISPLAY IS CONNECTED
/*
 * LCD RS pin to digital pin 7
 * LCD Enable pin to digital pin 6
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin A0
 * LCD D7 pin to digital pin 8
 * LCD R/W pin to ground
 * LCD VSS pin to ground
 * LCD VCC pin to 5V
 * 10K resistor:               VO on display connected directly to GND.
 * ends to +5V and ground      For optimising contract, use a potmeter as described
 * wiper to LCD VO pin (pin 3)
 */
const unsigned long debounceValue = 50UL; //SETTING FOR DEBOUNCE TIME IN MILLISECONDS
unsigned long debounceTime; //VARIABLE FOR DEBOUNCING KEYS PRESSED
boolean autoModeEnable = 0;          //SETTING BOOLEAN VARIABLE FOR TOGGLING BETWEEN MANUAL AND AUTOMODE
boolean is1pressed = 0;
boolean is2pressed = 0;
boolean is3pressed = 0;
boolean is4pressed = 0;
boolean initpassword = 0;

const byte led = 12;

unsigned long currentMillis;  //CURRENTMILLIS MUST BE UNSIGNED LONG TO AVOID STACK OVERFLOW
unsigned long RLYmillis;      //RLYMILLIS MUST BE UNSIGNED LONG TO AVOID STACK OVERFLOW
unsigned long RLYdelay = 2000;//RLYDELAY MUST BE UNSIGNED LONG TO AVOID STACK OVERFLOW - DECIDES THE AMOUNT OF TIME EACH RELAY SHOULD BE ON IN AUTOMODE

byte currentState = 1;        //MAKE VARIABLE CURRENTSTATE, SET IT TO START IN STATE 1 IN AUTOMODE

//INITIALIZE SETUP
void setup()
{

  Serial.begin(9600);        //SET BAUD RATE

  lcd.begin(20, 2);   // SETTING UP LCD'S NUMBER OF COLUMNS AND ROWS

  debounceTime = millis(); //DEBOUNCETIME = CURRENT TIME
  pinMode (led, OUTPUT);
  keypad.addEventListener(keypadEvent); //INITIALIZE CHECKUP ON KEYPAD USAGE
} // END OF SETUP

//INITIALIZE THE MAIN LOOP
void loop()
{
  char key = keypad.getKey();
  //================== is it time to check the switches?
  if (millis() - debounceTime >= debounceValue)
  {
    debounceTime = millis(); //re-initialize to the current time
  }
  if (initpassword == 1) {
    enterpassword();

  }//END MAIN LOOP=============================================================================================
}


//INITIALIZE THE AUTOMODE FUNCTION========================================================================
void enterpassword (void)
{
    switch (currentState)
    {
      //***************************
      case 1:
        is2pressed = 0;
        is3pressed = 0;
        is4pressed = 0;
        Serial.println ("CASE1");
        if (is1pressed == 1)
        {currentState = 2;}
        else
        {currentState = 1;}
        break;
      //***************************
      case 2:
        Serial.println ("CASE2");
        if (is1pressed == 1 && is2pressed == 1)
        {is1pressed = 0;
        currentState = 3;}
        else if (is2pressed == 0 && is1pressed == 1)
        {currentState = 2;}
        else 
       {currentState = 1;}
        break;
      //***************************
      case 3:
        Serial.println ("CASE33333333333333");
        if (is2pressed == 1 && is3pressed == 1)
        {is2pressed = 0;
        currentState = 4;}
        else if (is3pressed == 0 && is2pressed == 1)
        {currentState = 3;}
        else 
          {currentState = 1;}
        break;
      //***************************
      case 4:
        Serial.println ("CASE4");
        if (is3pressed == 1 && is4pressed == 1)
        {is3pressed = 0;
        currentState = 5;}
        else if (is4pressed == 0 && is3pressed == 1)
        {currentState = 4;}
        else 
        {currentState = 1;}
        break;
      //***************************
      case 5:
        digitalWrite (led, HIGH);
        break;
      //***************************
      default:
        currentState = 1;              //Back to state 1
  }
}

void keypadEvent(KeypadEvent key) {
  if (keypad.getState() == PRESSED)
  {
    if (key == '#')
    {initpassword = 1;}
    if (key == '1')
    {is1pressed = 1;}
    if (key == '2')
    {is2pressed = 1;}
    if (key == '3')
    {is3pressed = 1;}
    if (key == '4')
    {is4pressed = 1;}
  }
}

initpassword can never get to 1.

Change:
boolean initpassword = 0;

to
boolean initpassword = 1;

and then see what happens.

Also, when asking for our help, it is useful to us if with each code change, you provide to us what is currently happening.

So, please make the change above and report back as to what is then happening.

boolean inipassword isn't the problem. I have no problem getting to State 1, and can also get to State 2, 3, 4 & 5. Problem is that it doesn't really matter too much in which order I press the keys, as long as I press the right ones..

I need the program to check if I'm also pressing them in the right order. And if not, start over from State 1. And if yes, then allow access to State 5 in the end.

But it gets a bit confusing, and I can't really see what I should change in order for that to happen.

I think you need to add a "zero" state. Your state 1 will reset the was-pressed* variables for all buttons except 1, then it checks 1. You never reset is1pressed back to not-pressed.

Create a zero state which resets all of the was-pressed back to not-pressed, and then immediately go into state 1 to start looking for the correct sequence.

  • You called the variables is-pressed but really they are reporting "was pressed some time in the past."

Ahh. OK.

Structurally, you shouldn't be modifying global state changes inside a method called keypadEvent. It means we have to read every line of code to check how things are working, rather than just reading the high level code.

But, back to your problem...

I think the easiest approach is to modify your state machine to something like:

void enterpassword (void)
{
    if (newKey == 0)
    {
        // no new keys to process
        return;
    }
   
    switch (currentState)
    {
      //***************************
      case 1:
        Serial.println ("CASE1");
        if (newKey == 1)
        {
            currentState = 2;
        }
        break;
      //***************************
      case 2:
        if (newKey == 2)
        {
            currentState = 3;
        }
        else
        {
            // wrong sequence. Reset our machine state
            currentState = 1;
        }

        break;
      ...
     }

     newKey = 0; // reset our newKey state since we've processed this key
}

then every time a new key is pressed, store it in newKey so that we can process it.

Would you take the time to implement those changes in the code for me, so I can see what you mean exactly, and how you'd implement it? Thinking about code structure and such... and if I can learn something new, I'm all for it.

You don't have to if you don't want though - but you probably already knew that. :slight_smile:

Well basically, I don't think you need the keypad event stuff. Just delete it and the corresponding global variables.

I haven't used the keypad, but it looks like your main loop:

void loop()
{
  char key = keypad.getKey();
  //================== is it time to check the switches?
  if (millis() - debounceTime >= debounceValue)
  {
    debounceTime = millis(); //re-initialize to the current time
  }
  if (initpassword == 1) {
    enterpassword();
  }
}

can be modified to something like:

void loop()
{
  //================== is it time to check the switches?
  if (millis() - debounceTime >= debounceValue)
  {
    debounceTime = millis(); //re-initialize to the current time

    char key = keypad.getKey();
    enterpassword(key);
  }
}

and then use my code above, with the slight change of:

void enterpassword (char newKey)
{
   ...
}

This is all untested by me, but hopefully you can understand the structural improvement by separating functions, and passing the needed data to isolated functions using parameters, rather than using global variables.

Thanks! But I DO need the keypad functions... otherwise, how can I use it to enter the password?

I will have a look at your improvements and see if I can figure out how to implement them. :slight_smile:

While waiting for a reply, however, I fixed my code, so it now works as I wanted it to. I'm pasting it below.. maybe someone else can use it for something.

#include <Keypad.h>
//KEYPAD SETUP
const byte ROWS = 4; //DEFINE NUMBER OF ROWS ON KEYPAD
const byte COLS = 3; //DEFINE NUMBER OF COLUMNS ON KEYPAD
char keys[ROWS][COLS] = { //ARRAY FOR KEYPAD KEYS
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};//END ARRAY FOR KEYPAD KEYS

byte rowPins[ROWS] = {3, 10, 9, 2}; //PINS WHERE THE ROWS OF THE KEYPAD IS CONNECTED
byte colPins[COLS] = {17, 16, 15}; //PINS WHERE THE COLUMNS OF THE KEYPAD IS CONNECTED

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//MAKING KEYMAP FOR KEYPAD

boolean is1pressed = 0;
boolean is2pressed = 0;
boolean is3pressed = 0;
boolean is4pressed = 0;
boolean initpassword = 0;

const byte led = 12;

byte currentState = 0;        //MAKE VARIABLE CURRENTSTATE, SET IT TO START IN STATE 1 IN AUTOMODE

//INITIALIZE SETUP
void setup()
{
  Serial.begin(9600);        //SET BAUD RATE

  pinMode (led, OUTPUT);
  keypad.addEventListener(keypadEvent); //INITIALIZE CHECKUP ON KEYPAD USAGE
} // END OF SETUP

//INITIALIZE THE MAIN LOOP
void loop()
{
  char key = keypad.getKey();
if (initpassword == 1) 
{enterpassword();}
 }

void enterpassword (void)
{
  switch (currentState)
  {
    case 0:
      Serial.println ("CLEARING!");
      is1pressed = 0;
      is2pressed = 0;
      is3pressed = 0;
      is4pressed = 0;
      currentState = 1;
    //***************************
    case 1:

      Serial.println ("CASE1");
      if (is1pressed == 0 && is2pressed == 0 && is3pressed == 0 && is4pressed == 0)
      {currentState = 1;}
      else if (is1pressed == 1 && is2pressed == 0 && is3pressed == 0 && is4pressed == 0)
      {currentState = 2;}
      else if (is2pressed == 1 || is3pressed == 1 || is4pressed == 1)
      {currentState = 0;}
      break;
    //***************************
    case 2:
      Serial.println ("CASE2");
      if (is1pressed == 1 && is2pressed == 0 && is3pressed == 0 && is4pressed == 0)
      {currentState = 2;}
      else if (is1pressed == 1 && is2pressed == 1 && is3pressed == 0 && is4pressed == 0)
      {currentState = 3;}
      else if (is1pressed == 0 || is3pressed == 1 || is4pressed == 1)
      {currentState = 0;}
      break;
    //***************************
    case 3:
      Serial.println ("CASE3");
      if (is1pressed == 1 && is2pressed == 1 && is3pressed == 0 && is4pressed == 0)
      {currentState = 3;}
      else if (is1pressed == 1 && is2pressed == 1 && is3pressed == 1 && is4pressed == 0)
      {currentState = 4;}
      else if (is1pressed == 0 || is2pressed == 0 || is4pressed == 1)
      {currentState = 0;}     
      break;
    //***************************
    case 4:
      Serial.println ("CASE4");
      if (is1pressed == 1 && is2pressed == 1 && is3pressed == 1 && is4pressed == 0)
      {currentState = 4;}
      else if (is1pressed == 1 && is2pressed == 1 && is3pressed == 1 && is4pressed == 1)
      {currentState = 5;}
      else if (is1pressed == 0 || is2pressed == 0 || is3pressed == 0 || is4pressed == 0)
      {currentState = 0;}
      break;
    //***************************
    case 5:
      digitalWrite (led, HIGH);
      break;
    //***************************
    default:
      currentState = 0;              //Clear button variables and start over
  }
}

void keypadEvent(KeypadEvent key) {
  if (keypad.getState() == PRESSED)
  {
    if (key == '#')
    {initpassword = !initpassword;}
    if (key == '1')
    {is1pressed = !is1pressed;}
    if (key == '2')
    {is2pressed = !is2pressed;}
    if (key == '3')
    {is3pressed = !is3pressed;}
    if (key == '4')
    {is4pressed = !is4pressed;}
  }
}

I didn't say you don't need the keypad function, I said you don't need the keypad event handler.

Currently, you handle keys in two ways, no?

  1. char key = keypad.getKey();
  2. void keypadEvent(KeypadEvent key) {...}

You only need one of these. The first should be enough, I would guess, so you don't need the keypadEvent handler. My code takes the key, and passes it to the state machine, thus separating those units of code except for the parameter. So, when you are reading the high level code, you can see what data is passed between them, rather than it being invisible in the global variables.

You're right... made some modifications to simplify the code now. Easier to read.. and it works... sort of.. When i enter the correct keypad sequence and confirm it at the end, it doesn't go to dostuff(); - and I have no idea why.

If I enter incorrect key sequence, it's okay all the way to case8, where I, for some reason, need to press an extra key in order to get to case 9 - and I have no idea why!

Can someone tell me why?

#include <Keypad.h>
//KEYPAD SETUP
const byte ROWS = 4; //DEFINE NUMBER OF ROWS ON KEYPAD
const byte COLS = 3; //DEFINE NUMBER OF COLUMNS ON KEYPAD
char keys[ROWS][COLS] = { //ARRAY FOR KEYPAD KEYS
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};//END ARRAY FOR KEYPAD KEYS

byte rowPins[ROWS] = {3, 10, 9, 2}; //PINS WHERE THE ROWS OF THE KEYPAD IS CONNECTED
byte colPins[COLS] = {17, 16, 15}; //PINS WHERE THE COLUMNS OF THE KEYPAD IS CONNECTED
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte currentState = 0;
const byte led = 12;

void setup() {
  Serial.begin(9600);
  pinMode (led, OUTPUT);
}

void loop() {

  passwordentry();
  digitalWrite (led, LOW);
}

void passwordentry(void) {
  char key = keypad.getKey();
  switch (currentState)
  {
    case 0:
      if (key == NO_KEY) {
        Serial.println ("Enter Password");
        currentState = 0;
      }
      else if (key == '1') {
        currentState = 1;
      }
      else if (key != '1') {
        currentState = 5;
      }
      break;
    //***************************
    case 1:
      if (key == NO_KEY) {
        Serial.println ("CASE1");
        currentState = 1;
      }
      else if (key == '2') {
        currentState = 2;
      }
      else if (key != '2') {
        currentState = 6;
      }
      break;
    //***************************
    case 2:
      if (key == NO_KEY) {
        Serial.println ("CASE2");
        currentState = 2;
      }
      else if (key == '3') {
        currentState = 3;
      }
      else if (key != '3') {
        currentState = 7;
      }
      break;
    //***************************
    case 3:
      if (key == NO_KEY) {
        Serial.println ("CASE3");
        currentState = 3;
      }
      else if (key == '4') {
        currentState = 4;
      }
      else if (key != '4') {
        currentState = 8;
      }
      break;
    //***************************
    case 4:
      if (key == NO_KEY) {
        Serial.println ("Press * to confirm");
        currentState = 4;
      }
      else if (key == '*') {
        Serial.println ("Password accepted!");
        dostuff();
      }
      else if (key != '*') {
        currentState = 0;
      }
      break;
    //***************************
    case 5:
      if (key == NO_KEY) {
        Serial.println ("fake 1");
        currentState = 5;
      }
      else if (key = key) {
        currentState = 6;
      }
      break;
    //***************************
    case 6:
      if (key == NO_KEY) {
        Serial.println ("fake 2");
        currentState = 6;
      }
      else if (key = key) {
        currentState = 7;
      }
      break;
    //***************************
    case 7:
      if (key == NO_KEY) {
        Serial.println ("fake 3");
        currentState = 7;
      }
      else if (key = key) {
        currentState = 8;
      }
      break;
    //***************************
    case 8:
      if (key == NO_KEY) {
        Serial.println ("fake 4");
        currentState = 8;
      }
      else if (key = key) {
        currentState = 9;
      }
      break;
    //***************************
    case 9:
      if (key == NO_KEY) {
        Serial.println ("Press * to confirm");
        currentState = 9;
      }
      else if (key == '*') {
        Serial.println ("Password rejected!");
        delay(2000);
        currentState = 0;
      }
      else if (key != '*') {
        currentState = 0;
      }
      break;

    //***************************
    default:
      currentState = 0;              //Clear button variables and start over
  }
}

void dostuff (void) {
  digitalWrite (led, HIGH);
}

EDIT> Figured out those problems above... however, when I get to dostuff(); the led lights up, but serial monitor keeps pestering me about pressing * to confirm my password, which means it goes back into case4. How do I avoid that?

You can set a variable to say "password accepted" and then move to a different state, like back to state 0 or maybe there's a state 5 that waits for the next time that you require a password.

Then the doStuff() should be outside the big switch-case and after te stuff has been done, you can set that other variable back to "password not accepted".

EDIT: SOLVED IT!

Final working code below:

#include <Keypad.h>
//KEYPAD SETUP
const byte ROWS = 4; //DEFINE NUMBER OF ROWS ON KEYPAD
const byte COLS = 3; //DEFINE NUMBER OF COLUMNS ON KEYPAD
char keys[ROWS][COLS] = { //ARRAY FOR KEYPAD KEYS
  {'1', '2', '3'},
  {'4', '5', '6'},
  {'7', '8', '9'},
  {'*', '0', '#'}
};//END ARRAY FOR KEYPAD KEYS

byte rowPins[ROWS] = {3, 10, 9, 2}; //PINS WHERE THE ROWS OF THE KEYPAD IS CONNECTED
byte colPins[COLS] = {17, 16, 15}; //PINS WHERE THE COLUMNS OF THE KEYPAD IS CONNECTED
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
byte currentState = 0;
const byte led = 12;
boolean passaccepted = 0;

void setup() {
  Serial.begin(9600);
  pinMode (led, OUTPUT);
}

void loop() {
  if(passaccepted == 0){
passwordentry();
}}

void passwordentry(void) {
  char key = keypad.getKey();
  switch (currentState)
  {
    case 0:
      if (key == NO_KEY) {
        Serial.println ("Enter Password");
        currentState = 0;
      }
      else if (key == '1') {
        currentState = 1;
      }
      else if (key != '1') {
        currentState = 5;
      }
      break;
    //***************************
    case 1:
      if (key == NO_KEY) {
        Serial.println ("CASE1");
        currentState = 1;
      }
      else if (key == '2') {
        currentState = 2;
      }
      else if (key != '2') {
        currentState = 6;
      }
      break;
    //***************************
    case 2:
      if (key == NO_KEY) {
        Serial.println ("CASE2");
        currentState = 2;
      }
      else if (key == '3') {
        currentState = 3;
      }
      else if (key != '3') {
        currentState = 7;
      }
      break;
    //***************************
    case 3:
      if (key == NO_KEY) {
        Serial.println ("CASE3");
        currentState = 3;
      }
      else if (key == '4') {
        currentState = 4;
      }
      else if (key != '4') {
        currentState = 8;
      }
      break;
    //***************************
    case 4:
      if (key == NO_KEY) {
        Serial.println ("Press * to confirm");
        currentState = 4;
      }
      else if (key == '*') {
        Serial.println ("Password accepted!");
        passaccepted = !passaccepted;
        delay(2000);
        dostuff();
      }
      else if (key != '*') {
        currentState = 0;
      }
      break;
    //***************************
    case 5:
      if (key == NO_KEY) {
        Serial.println ("fake 1");
        currentState = 5;
      }
      else if (key = key) {
        currentState = 6;
      }
      break;
    //***************************
    case 6:
      if (key == NO_KEY) {
        Serial.println ("fake 2");
        currentState = 6;
      }
      else if (key = key) {
        currentState = 7;
      }
      break;
    //***************************
    case 7:
      if (key == NO_KEY) {
        Serial.println ("fake 3");
        currentState = 7;
      }
      else if (key = key) {
        currentState = 8;
      }
      break;
    //***************************
      case 8:
      if (key == NO_KEY) {
        Serial.println ("Press * to confirm");
        currentState = 8;
      }
      else if (key == '*') {
        Serial.println ("Password rejected!");
        delay(2000);
        currentState = 0;
      }
      else if (key != '*') {
        currentState = 0;
      }
      break;

    //***************************
      default:
      currentState = 0;              //Clear button variables and start over
  }
}



void dostuff () {
    digitalWrite (led, HIGH);
}

Congratulations, and the code is cleaner, but:

this

      else if (key = key) {

is wrong. I don't know what you are trying to do, but that is an assignment.

You may have been trying to do this:

      else if (key == key)

but that is also wrong, since it will always be true.

What you should probably be doing is this:

      else {

Also,

to again not have invisible behaviour, you should change this:

void loop() {
  if(passaccepted == 0){
passwordentry();
}}

void passwordentry(void) {

to

void loop() {
  if (passaccepted == false) {
     passaccepted = passwordentry();
  }
}

// returns true if the password is successfully entered
boolean passwordentry(void) {
    ....
}

and then return true or false from your subroutine.

Thanks!

Changed the key = key thing to just else { (and it works great!)

However, the way to toggle the boolean, as in your example, doesn't work... so for now I' sticking with the other thing. Why doesn't it work though?

cajodk:
Thanks!

Changed the key = key thing to just else { (and it works great!)

However, the way to toggle the boolean, as in your example, doesn't work... so for now I' sticking with the other thing. Why doesn't it work though?

Well, it's a bit hard for me to know without you posting the code as you have it.

But, did you remember to remove this line from your password function:

        passaccepted = !passaccepted;

If you post your cleaned up code using return values that doesn't work we can try to fix it.

By the way, congratulations on persevering, You seem to be understanding the changes and the reasons for them.

Thats the problem... didnt think of that. Works now.. :slight_smile: Just need to walk it through to fully understand it.