how to transfer void keypadEvent(KeypadEvent eKey) from void loop to void setup?

i have here the sample code:

#include <Password.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
 
Password password = Password( "123456" ); //This is our password
LiquidCrystal lcd(14, 13, 12, 11, 10, 9);
 
const byte ROWS = 4; // 4 rows
const byte COLS = 3; // 3 columns
// Define the Keymap
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
 
byte rowPins[ROWS] = {5, 4, 3, 2};// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte colPins[COLS] = {8, 7, 6};// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
 
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
 
void setup()
{
  pinMode(15, OUTPUT);
  lcd.begin(16, 2);
  lcd.print(" Enter Password");
  keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
 
void loop()
{
  lcd.setCursor(0, 1);
  keypad.getKey();
}
 
//check the keypad events
void keypadEvent(KeypadEvent keyPress)
{
  switch (keypad.getState())
  {
    case PRESSED:
    lcd.print(keyPress); //print the keypress
    switch (keyPress)
    {
      case '#':
            checkPassword();
          break;
 
          case '*':
            password.reset();
            digitalWrite(15, LOW);
            lcd.setCursor(0,1);
            lcd.print("                ");
          break;
 
          default:
          password.append(keyPress);
     }
  }
}
 
//check the entered password
void checkPassword(){
  if (password.evaluate()) //if password is correct
  {
    lcd.setCursor(0,1);
    lcd.print("Correct Password"); //print a message on serial monitor
    digitalWrite(15, HIGH); //turn ON an LED
    password.reset(); //reset password
  }
  else
  {
    lcd.setCursor(0,1);
    lcd.print(" Wrong Password"); //if password is wrong
    digitalWrite(15, HIGH); //blink an LED
    delay(100);
    digitalWrite(15, LOW);
    delay(100);
    digitalWrite(15, HIGH);
    delay(100);
    digitalWrite(15, LOW);
    password.reset(); //reset password
  }
}

i'm wondering how to transfer the void keypadEvent function to void setup. I tried to cut and paste but it doesn't work. I'm planning to put something inside the void loop. I'm making an authorization to our thesis project. What i wanna do is to have the authorization in the void setup before it will proceed to the main program that i will put inside the void loop. please help! i'd really appreciate it..A LOT! thanks!! :smiley:

you may instead want to create a simple state machine that advances once the password is entered...

BulldogLowell:
you may instead want to create a simple state machine that advances once the password is entered...

can u please show me what the code will look like after?

Here is some pseudocode to get you started

Set CurrentState to 0

start of loop()
  switch (currentState)
  case 0
    code here to read and verify password
    when it is valid set currentState to 1
    break

  case 1
    any code you want to execute when the password is valid
    break
  
  end of switch
end of loop

yoojluzon:
can u please show me what the code will look like after?

this is an example of what I meant...

int myNumber, myBase;
int state;
boolean message;

void setup()
{
  Serial.begin(115200);
}
void loop()
{
  
  if (state == 0)
  {
    if (!message) Serial.println(F("Enter the Number to Convert: "));
    message = true;
    if (Serial.available())
    {
      myNumber = Serial.parseInt();
      state = 1;
      message = false;
    }
  }
  if (state == 1)
  {
    if (!message) Serial.println(F("Enter the base"));
    message = true;
    if (Serial.available())
    {
      myBase = Serial.parseInt();
      state = 2;
      message = false;
    }
  }
  if (state == 2)
  {
    if (!message)
    {
      if (myNumber != 0)
      {
        convert10tob(myNumber, myBase);
      }
      else
      {
        Serial.println(0);
      }
    }
    state = 0;
    Serial.println(" ");
  }
}
void convert10tob(int N, int b)
{
  if (N == 0)
    return;
  int x = N % b;
  N /= b;
  if (x < 0)
    N += 1; 
  convert10tob(N, b);
  Serial.print(x < 0 ? x + (b * -1) : x);
  return;
}

but @UKHeliBob had a great approach, too

i have revised my codes and here's the structure:

#include <Keypad.h>
#include <Password.h>
#include <EEPROM.h>
#include <SD.h>
#include <LiquidCrystal.h>
#include "SIM900.h"
#include <SoftwareSerial.h>
#include "sms.h"
SMSGSM sms;

//the declarations of variables i used in this part
//also the assignment of the pins are in this part

void setup(){

readPassEeprom();

Serial.print("Enter Passcode:");

}

void keypadEvent(KeypadEvent keyPress)
{
 //switch loop
//checkPassword();
//changePassword();
//startDevice();
}

void checkPassword()
{
 //if else
}

void changePassword()
{
 //if else
}

void startDevice()
{
 //if else
}

void readPassEeprom()
{
//if else
}

void loop(){
keypad.getKey();

//the main part of the program (i'm making a counter with the use of pushbuttons)
//i want the whole program to require the correct password/passcode before granting access to this part
//the pushbuttons must not count when pressed unless the correct passcode is given above.

}

what i have right now is a working program but i'm stuck with the authorization. When i'm asked to "Enter Passcode:" then without giving inputs of the passcode, i tried to press the pushbuttons and it counted (which should not suppose to happen unless authorized by giving the correct passcode. I'm a bit confused on how to do it. I was thinking of using the while loop but i'm not sure how to do it.

Your help will greatly be appreciated.
Thank You so much!!! :smiley:

yoojluzon:
what i have right now is a working program but i'm stuck with the authorization. When i'm asked to "Enter Passcode:" then without giving inputs of the passcode, i tried to press the pushbuttons and it counted (which should not suppose to happen unless authorized by giving the correct passcode. I'm a bit confused on how to do it. I was thinking of using the while loop but i'm not sure how to do it.

Your help will greatly be appreciated.
Thank You so much!!! :smiley:

I don't see the advice offered above in your code...