This is the code I have been working.
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "1234";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 3, 4, 5};
byte colPins[cols] = {14, 15, 16};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int solenoidPin = 13;
int redPin = 8;
int bluePin = 9;
int greenPin = 10;
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(6, 7, 12, 17, 18, 19);
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(solenoidPin, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0, 0);
loadCode(); // load the code from EEPROM
updateOutputs();
screen();
}
void loop()
{
char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
lcd.print("?");
}
if (key == '*' )
{
// * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
screen();
}
if (key == '#' ) // manual locked when push '#'
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(100);
digitalWrite(bluePin, LOW);
screen();
}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("password correct");
lcd.setCursor(0, 1);
lcd.print("access granted");
updateOutputs();
screen();
}
}
void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, LOW);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, HIGH);
}
delay(1000);
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, LOW);
locked = true;
position = 0;
position2= 0;
}
void getNewCode()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("new password?");
lcd.setCursor(6, 1);
lcd.print("");
flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
lcd.print(key);
}
saveCode();
flash();flash();
}
void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}
void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}
void eraseCode() //
{
EEPROM.write(1, 2);
EEPROM.write(2, 2);
EEPROM.write(3, 5);
EEPROM.write(4, 5);
EEPROM.write(0, 2);
}
void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(100);
}
void screen()
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("LOCKED");
lcd.setCursor(0, 1);
delay(100);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("enter password");
lcd.setCursor(0, 1);
}
It works great but if you enter wrong 4 digit password it doesn’t automatically reset position.You need to press “#” to reset and re-enter the 4 digit password.
I modded that as this:
if (key == secretCode[position])
{
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("password correct");
lcd.setCursor(0, 1);
lcd.print("access granted");
updateOutputs();
screen();
}
else
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
screen();
}
}
}
But it doesn’t work for this case.What am I doing wrong?