/***************************************************
Biometric Box Sketch for the optical Fingerprint sensor
This sketch implements a two-level security scheme requiring the
user to enter a passcode via the keypad before scanning a fingerprint
for access.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Bill Earl for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include <Keypad.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;
int ButtonState = 0;
int LedpinState=0;
int SolenoidPinState=0;
int position = 0;
int LedPin = 10;
int SolenoidPin = 11;
int Button = 13;
// Define your password key sequence here
char* secretCode = "1423";
// Keypad key matrix:
const byte rows= 4; //number of rows on the keypad
const byte cols= 4; //number of columns on the keypad
//keymap defines the key pressed according to the row and columns just as appears on the keypad
char keys[rows][cols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
//Code that shows the the keypad connections to the arduino terminals
byte rowPins[rows] = {9,8,7,6}; //Rows 0 to 3
byte colPins[cols]= {5,4,3,2}; //Columns 0 to 3
//initializes an instance of the Keypad class
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
// More pin definitions:
void setup()
{
pinMode(LedPin, OUTPUT);
pinMode(SolenoidPin, OUTPUT);
pinMode(Button, INPUT);
}
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;
}
}
if (position == 4) // Password successfully entered - advance state
{
setLockState(PASSWORD_OK);
position = 0;
digitalWrite(SolenoidPin,HIGH);
}
delay(100);
}
// PASSWORD_OK state - Now wait for a valid fingerprint reading
else if (LockState == PASSWORD_OK)
{
if (digitalRead(Button)==HIGH);
{
setLockState(UNLOCKED);
digitalWrite(SolenoidPin, LOW);// avance à UNLOCKED
digitalWrite(LedPin, HIGH);//allume la led
}
}
else
{
{ setLockState (LOCKED);
digitalWrite(SolenoidPin, HIGH);// Time-out - go back to the locked state.
digitalWrite(LedPin, LOW);
}
}
}
Où se trouve fonction "setLockState"?
void setLockState(int state)
{
LockState = state;
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);
}
}