need help to have 3 trials to input secretcode and display on lcd

i have a security lock that will be use at doors or vaults that has a NUMERIC KEYPAD, SOLENOID LOCK, A LCD shield and fingerprint reader. i have study the c++ but really dont get it.i need someone who can add the coding…please help me

case 1: initially the device is in “LOCK” state . then if i want it to “UNLOCK” you have 3 trials to input secretcode
case 2: display it on LCD using LiquidCrystal.lcd

this is my coding…

#include <Keypad.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

// Define the states for the lock state machine
#define LOCKED 2
#define PASSWORD_OK 1
#define UNLOCKED 0

// State Variables:   Initialize to the locked state
int LockState = LOCKED;
long StartTime = 0;
int position = 0;

// Define your password key sequence here
char* secretCode = "1423";

// Keypad key matrix:
const byte rows = 4; 
const byte cols = 3; 
char keys[rows][cols] = 
{
   {'1','2','3'},
   {'4','5','6'},
   {'7','8','9'},
   {'*','0','#'}
};

// Keypad pin definitions
byte rowPins[rows] = {2, 3, 4, 5}; 
byte colPins[cols] = {6, 7, 8};  

// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

// More pin definitions:
int LedPin = 10;
int SolenoidPin = 11;

// Define a Fingerprint sensor on pins 12 & 13
int getFingerprintIDez();
SoftwareSerial mySerial(13, 12);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);


void setup()                    
{
   pinMode(LedPin, OUTPUT);
   pinMode(SolenoidPin, OUTPUT);
   
   // Flash hello
   for (int i = 0; i < 3; i++)
   {
     digitalWrite(LedPin, HIGH);
     delay(100);
     digitalWrite(LedPin, LOW);
     delay(100);
   }
   
   // Initialize state and communicatins
   setLockState(LOCKED); 
   Serial.begin(9600); 
   finger.begin(57600);

   // Connect to the sensor
   if (finger.verifyPassword()) 
   {
      Serial.println("Found fingerprint sensor!");
   } 
   else 
   {
      Serial.println("Did not find fingerprint sensor :(");
      while (1);
   }
}


void loop()                    
{
   // Run the state machine:
   
   // Locked State - Monitor keypad for valid Password code entry
   if (LockState == LOCKED)
   {
      char key = keypad.getKey();

      if (key == '*' || key == '#')
      {
         position = 0;
         setLockState(LOCKED);
      }
      if (key != 0)
      {
         if (key == secretCode[position])  // Valid key in Password sequence
         {
            Serial.print("Matched ");   
            Serial.print(key);   
            Serial.print("-at-");   
            Serial.println(position);   
            position ++;
         }
         else  // Invalid key - start all over again
         {
            Serial.println("Invalid Code!");   
            position = 0;
         }
      }

      // Let the LED 'breathe' while we wait
      analogWrite(LedPin, sin((millis() % 3142) / 1000.0) * 255);

      if (position == 4)  // Password successfully entered - advance state
      {
         setLockState(PASSWORD_OK);
         position = 0;
      }
      delay(100);
   }

   // PASSWORD_OK state - Now wait for a valid fingerprint reading
   else if (LockState == PASSWORD_OK)
   { 
      if (getFingerprintIDez() != -1)
      {
         setLockState(UNLOCKED); // Valid fingerprint - advance state to UNLOCKED
      }
      if (millis () - StartTime > 5000) 
      {
         setLockState (LOCKED); // Time-out - go back to the LOCKED state
      }
   }

   // UNLOCKED state - hold the solenoid open for a limited time
   else if (LockState == UNLOCKED)
   { 
      for (int i = 0; i < 30; i++)
      {
         // Flash the led to indicate the lock is open
         digitalWrite(LedPin, LOW);
         delay(50);
         digitalWrite(LedPin, HIGH);
         delay(50);
      }
      setLockState (LOCKED);  // Time-out - go back to the locked state.
   }
}



// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() 
{
   uint8_t p = finger.getImage();
   if (p != FINGERPRINT_OK)  return -1;

   p = finger.image2Tz();
   if (p != FINGERPRINT_OK)  return -1;

   p = finger.fingerFastSearch();
   if (p != FINGERPRINT_OK)  return -1;

   // found a match!
   Serial.print("Found ID #"); Serial.print(finger.fingerID); 
   Serial.print(" with confidence of "); Serial.println(finger.confidence);
   return finger.fingerID; 
}


// Set the state and the time of the state change
void setLockState(int state)
{
   LockState = state;
   StartTime = millis ();
   if (state == LOCKED)
   {
      Serial.println("Locked!");
      digitalWrite(LedPin, HIGH);
      digitalWrite(SolenoidPin, LOW);  
   }

   else if (state == PASSWORD_OK)
   {
      Serial.println("PASSWORD_OK!");
      digitalWrite(LedPin, LOW);  
   }    
   else if (state == UNLOCKED)
   {
      Serial.println("Unlocked!");
      digitalWrite(LedPin, LOW);
      digitalWrite(SolenoidPin, HIGH);      
   }
}

i really need help and im newbie in this subject

Your description of the state machine is pretty flawed: Did you even bother to draw a state diagram. I attached a state diagram that represents a minimum

You also should consider implementing you state machine differently. Have a look at my state machine library here: http://playground.arduino.cc/Code/SMlib

I’ll be posting you some code later

security lock SM state diagram.png

i really dont understand.. the diagram you show to me are likely project need i built. like i say, i dont have basic on programming and electronic and i was study it.. if you dont mind can u give ur code to me.. and i really appreciate it. firstly i want to create set password but to complecated to me and i decide to use 3x trial.. i really hope that u can help me about the code

The code is NOT important. The diagram is. So spend some time studying it. And drawing state diagrams is more about describing actions than actual programming. Another reason to give it some time. Once done and verified the code will actually write itself (almost).

thank so much.. i will spent some time to draw my diagram.. can i give it to u later of my diagram??

Of course you can.

here is a example based on the diagram i made:

#include <SM.h>
#include <Keypad.h>
//#include <Adafruit_Fingerprint.h>//skipped for this example
//#include <SoftwareSerial.h>//skipped for this example

SM Vault(idleH, idleB);//main control loop
SM led(ledOffH);//blinkin led
int blinkCount;

// Define your password key sequence here
char secretCode[] = "*1423#";
int codePos;
enum {SUCCESS = -1, UNDETERMIND, FAILURE1, TRY1, FAILURE2, TRY2, FAILURE3 };
int codeState;


// Keypad key matrix:
const byte rows = 4; 
const byte cols = 3; 
char keys[rows][cols] = {{'1','2','3'},
                         {'4','5','6'},
                         {'7','8','9'},
                         {'*','0','#'}};

// Keypad pin definitions
byte rowPins[rows] = {2, 3, 4, 5}; 
byte colPins[cols] = {6, 7, 8};  

// Instantiate the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

// More pin definitions:
const int LedPin = 10;
const int SolenoidPin = 11;
const int fingerPin = 9;//for simulating sensor

void setup(){
  pinMode(LedPin, OUTPUT);
  pinMode(SolenoidPin, OUTPUT);
  // Initialize communicatins
  Serial.begin(115200);
  Serial.println("Vault lock system 0.1"); 
}//setuo()

void loop(){
  EXEC(Vault);
}//loop()

//State functions for vault

State idleH(){
  Serial.println("Vault is Locked");
  digitalWrite(SolenoidPin, 0);
  led.Set(welcome1H, welcome1B);
  codeState = UNDETERMIND;
}//idleH()

State idleB(){
  EXEC(led);
  if(digitalRead(fingerPin)) Vault.Set(fingerprintH, fingerprintB);
}//idleB()

State fingerprintH(){
  Serial.println("fingerprint ok");
}//fingerprintH()

State fingerprintB(){
  Vault.Set(codeH, codeB);//just bypassing here because of no sensor
}//fingerprintB()

State codeH(){
  Serial.print("Enter code");
}//codeH()

State codeB(){
  if(char ch = keypad.getKey()){//get char from keypad, non blocking
    Serial.print('*');//secret char
    if(ch != secretCode[codePos++]){//wrong char
      Serial.println();
      codePos = 0;//reset char pointer
      codeState++;//increase failure
    }else if(codePos > 5){//all chars read
      Serial.println();
      codePos = 0;//reset char pointer
      codeState = SUCCESS;
    }//else if
  }//if(CH)
  switch(codeState){//evaluate code, UNDETERMIND and TRYx fall trough
    case FAILURE1:
    case FAILURE2: Serial.println("Wrong code, try again");
       Vault.Restart();//back to codeH
       codeState++;//Set codeState to next try
       break;
    case FAILURE3: Serial.println("Wrong code, final try");
       Vault.Set(timeout);
       break;
    case SUCCESS:  Vault.Set(openH, openB);
  }//switch(codeState)
}//codeB()

State timeout(){
  if(Vault.Timeout(2000)) Vault.Set(idleH, idleB);
}//timeout()

State openH(){
  Serial.println("Vault open");
  digitalWrite(SolenoidPin, 1);
}//openH()

State openB(){
  if(Vault.Timeout(2000)) Vault.Set(idleH, idleB);
}//openB()

//State functions for led
State ledOffH(){
  digitalWrite(LedPin, 0);
  blinkCount = 0;
}//ledOffH()

State welcome1H(){
  digitalWrite(LedPin, 1);
  blinkCount++;
}//welcome1H()

State welcome1B(){
  if(led.Timeout(300)) 
    if(blinkCount<3) led.Set(welcome2);
    else led.Set(ledOffH);
}//welcome1()

State welcome2(){
  digitalWrite(LedPin, 0);
  if(led.Timeout(300)) led.Set(welcome1H, welcome1B);
}//welcome2()

thanks for that… i have study your code and try understanding it… i can understand ur flow but not overall… i try compile it and it have no problem… can i ask u the flow of ur code…

//#include <Adafruit_Fingerprint.h>//skipped for this example
//#include <SoftwareSerial.h>//skipped for this example

why it have comment symbol //

and what mean of…

led.Set(welcome1H, welcome1B)

are they have two user?? the rest i can understand it…
and thank to u so much coz helping me… ur example was help me a lot…

apiz:

//#include <Adafruit_Fingerprint.h>//skipped for this example

//#include <SoftwareSerial.h>//skipped for this example

inserting a comment on the beginning of a line is a quite commen method of temporarily disabeling the code on that line. I had to skip that because i had no fingerprint reader to test with

apiz:
and what mean of…

led.Set(welcome1H, welcome1B)

are they have two user?? the rest i can understand it…
and thank to u so much coz helping me… ur example was help me a lot…

I have created another state machine just for blinking the the led. It is an example how my library can be used for creating concurrent programs. That is programs that do several things simultaneously

ok. i understand now.. i just need to remove comment symbol in order to function it right.. if i want to display it on LCD i need to add LiquidCrystal.h library and then the coding need to modified , change Serial.println('......') to lcd.print('....') right?? ..

can i use ur code and modified it little bit??

Serial.println('......') to lcd.print('....') right?? ..

Wrong. You need double quotes. And only one question mark.

Feel free to use my code or modify it. But this code just represnts a minimum of functionality. Often it easier to write from scratch than to make severe changes. And once you have drawn a state diagram and verified it (yourself or by others) writing the code should not be that hard

Thank so much… for LCD print that just i giving example… but it is true that i need to change serial.println to lcd.print??

here i attach my diagram… can u feel free to verified it…
again thank you very much

Some things to think about with your diagram

  • Circles are states. These should have some kind of action associated with them. That is the program should do something
  • Arrows are transitions. Mostly these are triggered by external events and not the program. The exception is timers. But almost every transition should be associated with a condition

When i study your diagram i note the following

  • The transition from standby to check password is not clear. You could check for the'*' character to make things easy
  • After scanning the fingerprint you have a transition to "check another fingerprint". After that you just take a timeout without actually processing the second check

You should take some time to work these issues trough before attempting to code (there might be others i haven't noticed).

I have not used LCD but i believe you can change the code as you suppose. There might be some smaller issues depending on the smaller size of the display and you might want to play a little with cursor placement.

ok, thank so much.. tes, actually i want to make a set user with is can change password but i need to make slowly and one by one.. i'm not very well on c++, that why i make a default password.. if i pass it i will proceed to change password. this is my first attempt. from ur answer i have note i was make so much mistake.. im really glad that u are very helpful..

i will try from scratch of my coding and i will show u the result ..