#include <Wire.h>
#include <Keypad_I2C.h>

#include <Keypad.h>

#include <EEPROM.h>

char* secretCode = "123456";

int position = 0;

int position2 = 0;

boolean locked = true;

const byte rows = 4;

const byte cols = 4;

char keys[rows][cols] = {

 

  {'1','2','3','A'},

  {'4','5','6','B'},

  {'7','8','9','C'},

  {'*','0','#','D'}

 

};

byte rowPins[rows] = {0,1,2,3}; //connect to the row pinouts of the keypad

byte colPins[cols] = {4,5,6,7}; //connect to the column pinouts of the keypad

int i2caddress = 0x20; 

Keypad_I2C keypad = Keypad_I2C(makeKeymap(keys), rowPins, colPins, rows, cols, i2caddress );

 

int redPin = 11;

int greenPin = 12;

int bluePin = 10;

//int solenoidPin = 13;

int i = 0;

 

void setup()

{

  keypad.begin();
  
pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);

loadCode();

flash();

updateOutputs();

}

void loop()

{

 

char key = keypad.getKey();

if (key)

{

position2 ++;

digitalWrite(bluePin, HIGH);

delay(30);

digitalWrite(bluePin, LOW);

}

if (key == '*' && ! locked)

{

// unlocked and * pressed so change code

position = 0;

position2 = 0;

getNewCode();

updateOutputs();

}

if (key == '#')

{

locked = true;

position = 0;

position2= 0;

updateOutputs();

digitalWrite(bluePin, HIGH);

delay(300);

digitalWrite(bluePin, LOW);

}

if (key == secretCode[position])

{

position ++;

}

if (position == 6 & position2 == 6)

{

locked = false;

digitalWrite(bluePin, HIGH);

delay(300);

digitalWrite(bluePin, LOW);

updateOutputs();

}

delay(100);

}

 

void updateOutputs()

{

if (locked)

{

digitalWrite(redPin, HIGH);

digitalWrite(greenPin, LOW);

//digitalWrite(solenoidPin, HIGH);

}

else

{

digitalWrite(redPin, LOW);

digitalWrite(greenPin, HIGH);

//digitalWrite(solenoidPin, LOW);

 

delay (50);

for (int i=0; i <= 100; i++)

{

char key;

key = keypad.getKey();

if (key == '*')

{

// unlocked and * pressed so change code

position = 0;

position2 = 0;

getNewCode();

updateOutputs();

}

delay (50);

}

digitalWrite(redPin, HIGH);

digitalWrite(greenPin, LOW);

//digitalWrite(solenoidPin, HIGH);

locked = true;

position = 0;

position2= 0;

}

}

 

void getNewCode()

{

flash();

for (int i = 0; i < 6; i++ )

{

char key;

key = keypad.getKey();

while (key == 0)

{

key = keypad.getKey();

}

flash();

secretCode[i] = key;

}

saveCode();

flash();flash();

}

 

void loadCode()

{

if (EEPROM.read(0) == 9)

{

secretCode[0] = EEPROM.read(1);

secretCode[1] = EEPROM.read(2);

secretCode[2] = EEPROM.read(3);

secretCode[3] = EEPROM.read(4);

secretCode[4] = EEPROM.read(5);

secretCode[5] = EEPROM.read(6);

}

}

 

void saveCode()

{

EEPROM.write(1, secretCode[0]);

EEPROM.write(2, secretCode[1]);

EEPROM.write(3, secretCode[2]);

EEPROM.write(4, secretCode[3]);

EEPROM.write(5, secretCode[4]);

EEPROM.write(6, secretCode[5]);

EEPROM.write(0, 9);

}

 

void flash()

{

digitalWrite(redPin, HIGH);

digitalWrite(greenPin, LOW);

delay(100);

digitalWrite(redPin, LOW);

digitalWrite(greenPin, HIGH);

delay(100);

}

 

