// This #include statement was automatically added by the Spark IDE.
#include "Keypad.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 = "1234";
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'#','0','*'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = { D0, D1, D2, D3 };
// Connect keypad COL0, COL1 and COL2 to these Arduino pins.
byte colPins[COLS] = { D4, D5, D6 };
// Create the Keypad
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledpin D7
#define SolenoidPin A0
void setup()
{
pinMode(ledpin, OUTPUT);
pinMode(SolenoidPin, OUTPUT);
pinMode(D0, INPUT_PULLDOWN);
pinMode(D1, INPUT_PULLDOWN);
pinMode(D2, INPUT_PULLDOWN);
pinMode(D3, INPUT_PULLDOWN);
// 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);
}
void loop()
{
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '*':
digitalWrite(ledpin, LOW);
break;
case '#':
digitalWrite(ledpin, HIGH);
break;
default:
Serial.println(key);
}
}
}
void loop()
{
// Run the state machine:
// Locked State - Monitor keypad for valid Password code entry
if (LockState == LOCKED)
{
char key = kpd.getKey();
if (key == '*' || key == '#')
{
position = 0;
setLockState(LOCKED);
}
if (key != 0)
{
Serial.print(key);
Serial.print("-");
/*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!");
Serial.print(key);
Serial.print("-at-");
Serial.println(position);
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);
}
else if (LockState == PASSWORD_OK)
{
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.*/
}
}
// 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);
}
}