I am making an arduino based Code Lock with muptiple passwords and function to add new passwords on the fly.
So here is the problem:
I am using ISIS and Simulino to simulate the Arduino. I have placed several Serial.println to debug my program and at the end where everything seems to be ok I deleted all of them and then something just stopped working. Then I started to delete them one by one to see which one fools me. So here is the code the s**t is Serial.println((char)currentEEPROMCell); located on row 128 (use Ctrl+F to find it 'couse the rows are not displayed here). As you see all others 'println's are commented only this one isn't.
I am also planing to make some sound effects and it will be 100% complete
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <EEPROM.h>
#define lockPin 2
const String newPasswordKey = "s*123#s";
const int errorDelayTime = 1500;
const byte ROWS = 4;
const byte COLS = 4;
char Keys[ROWS][COLS] = {
{'1','2','3','c'},
{'4','5','6','d'},
{'7','8','9','a'},
{'*','0','#','s'}
};
byte rowPins[ROWS] = {12, 11, 10, 9};
byte colPins[COLS] = {8, 7, 6, 5};
Keypad _keyboard = Keypad( makeKeymap(Keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal lcd(A5, A4, A3, A2, A1, A0);
String inputStr = "";
boolean checkStr = false;
byte currentCursorPosition = 0;
boolean makeNewPassword = false;
void setup(){
lcd.begin(16, 2);
//Serial.begin(9600);
pinMode(lockPin, OUTPUT);
}
void loop(){
char button = _keyboard.getKey();
if (button){
if (button == 'c'){
inputStr = "";
makeNewPassword = false;
lcd.clear();
}
else if (button == 'd'){
if(inputStr.length()>0){
byte currentPosition = inputStr.length()-1;
inputStr.setCharAt(currentPosition, ' ');
inputStr.trim();
lcd.setCursor(currentPosition, 1);
lcd.write(" ");
lcd.setCursor(currentPosition, 1);
}
}
else if (button == 'a'){
checkStr = true;
}
else{
inputStr += button;
lcd.setCursor(inputStr.length()-1, 1);
if(makeNewPassword){
lcd.print(button);
}
else{
lcd.print("*");
}
}
//Serial.println(inputStr);
}
if(inputStr.length()>=4 && checkStr == true){
int inputStrLen = inputStr.length();
if (inputStr == newPasswordKey){
lcd.clear();
lcd.write("New Password:");
lcd.setCursor(0,1);
makeNewPassword = true;
}
else if(makeNewPassword == true){
//Serial.println("saving new password");
if(inputStr.indexOf('s')>-1){
//ERROR
//Serial.println("forbidden character");
//sound signal
lcd.clear();
lcd.print("forbidden character");
delay(errorDelayTime);
lcd.clear();
}
else{
//read the new password and save it to the EEPROM
for(int address = 0; address<1024; address++){
if((EEPROM.read(address) == 0xFF) && (EEPROM.read(address+1) == 0xFF)){
//Serial.println("End of passwords!");
for(int Nchar=0; Nchar<inputStrLen; Nchar++){
//Serial.println("Writing in EEPROM");
EEPROM.write(address+1+Nchar, inputStr[Nchar]);
}
lcd.clear();
//sound signal
lcd.print("PASSWORD SAVED");
delay(errorDelayTime);
lcd.clear();
break;
}
if(address==1021){
//No space for new passwords
//sound signal
lcd.clear();
lcd.print("OUT OF MEMORY");
delay(errorDelayTime);
lcd.clear();
break;
}
}
}
//Serial.println("Password saved");
makeNewPassword == false;
}
else{
//Serial.println("Checking password");
//check passwords
byte currentEEPROMCell;
String EEPROMPassword = "";
for(int readAddress; readAddress < 1024; readAddress++){
//Serial.print("reaing from EEPROM("+ (String)readAddress +")=");
currentEEPROMCell = EEPROM.read(readAddress);
Serial.println((char)currentEEPROMCell);
if(currentEEPROMCell!= 0xFF){
EEPROMPassword += (char)currentEEPROMCell;
}
else{
if(inputStr == EEPROMPassword){
//sound signal
lcd.clear();
lcd.print("ACCESS GRANTED");
digitalWrite(lockPin, HIGH);
delay(2500);
digitalWrite(lockPin, LOW);
lcd.clear();
break;
}
else if(EEPROM.read(readAddress+1) == 0xFF){
//sound signal
lcd.clear();
lcd.print("ACCESS DENIED");
delay(errorDelayTime);
lcd.clear();
break;
}
else{
EEPROMPassword = "";
}
}
}
EEPROMPassword = "";
}
inputStr = "";
}
else if(checkStr == true){
//String is too short
lcd.clear();
lcd.print("SHORT PASSWORD");
delay(errorDelayTime);
lcd.clear();
inputStr = "";
}
checkStr = false;
}