Hello everyone,
I've built a password lock+lcd+relay system where the lcd displays various instructions for the user and statuses...but I've ran across a problem! My code compiles perfectly and uploads with no issues. It also runs on the arduino nano, 4x4 keypad, and 20x4 LCD I2C great-that is until you unplug the power and re-connect it. I can't think of how else to explain it so sorry but basically it runs fine when first uploaded but doesn't run if you "reset" the Nano. The LCD is blank and the connected LED's have no sign of use.
Here is my code:
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BACKLIGHT_PIN 9
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // set the LCD address to 0x38
const byte ROWS = 4;
const byte COLS = 4;
char pass[] = "123ABC"; // 6 digit password only (no less or no more)
int OP=10;
int green=12;
int red=11;
char key1;
char key2;
char key3;
char key4;
char key5;
char key6;
char dumpkey;
char keyOK;
char ok[]="D";
char offkey;
char off[]="C";
int z;
char keys[ROWS][COLS] =
{
{'1','4','7','*'},
{'2','5','8','0'},
{'3','6','9','#'},
{'A','B','C','D'}
};
byte rowPins[ROWS] = {6,7,8,A0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2,3,4,5}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
Serial.begin(9600);
pinMode(OP,OUTPUT);
pinMode(green,OUTPUT);
pinMode(red,OUTPUT);
digitalWrite(OP,LOW);
lcd.begin (20,4);
{
lcd.backlight();
lcd.noBacklight();
delay(250);
}
lcd.backlight();
}
void loop()
{
top:
Serial.println("");
lcd.clear();
Serial.println("[Press D = Enter]");
lcd.setCursor(0, 0);
lcd.print("Enter PIN + D to Arm");
Serial.print("Enter the password: ");
key1=keypad.waitForKey();
if(key1 == pass[0])
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
{
z=1;
Serial.print("*");
lcd.setCursor(0, 1);
lcd.print("*");
goto A;
}
}
else
{
goto dump;
}
A:
key2=keypad.waitForKey();
if(key2 == pass[1])
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
{
z=2;
Serial.print("*");
lcd.setCursor(1, 1);
lcd.print("*");
goto B;
}
}
else
{
goto dump;
}
B:
key3=keypad.waitForKey();
if(key3 == pass[2])
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
{
z=3;
Serial.print("*");
lcd.setCursor(2, 1);
lcd.print("*");
goto C;
}
}
else
{
goto dump;
}
C:
key4=keypad.waitForKey();
if(key4 == pass[3])
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
{
z=4;
Serial.print("*");
lcd.setCursor(3, 1);
lcd.print("*");
goto D;
}
}
else
{
goto dump;
}
D:
key5=keypad.waitForKey();
if(key5 == pass[4])
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
{
z=5;
Serial.print("*");
lcd.setCursor(4, 1);
lcd.print("*");
goto E;
}
}
else
{
goto dump;
}
E:
key6=keypad.waitForKey();
if(key6 == pass[5])
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
{
z=6;
Serial.print("*");
lcd.setCursor(5, 1);
lcd.print("*");
goto ok;
}
}
else
{
goto dump;
}
ok:
keyOK=keypad.waitForKey();
if(keyOK == ok[0])
{
digitalWrite(OP,HIGH);
digitalWrite(green,HIGH);
Serial.println("");
Serial.println("Relay Activated, Press 'C' to Deactivate.\n");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("****PIN Accepted****");
delay(500);
lcd.setCursor(0,1);
lcd.print("Controller Armed");
delay(500);
lcd.setCursor(0,2);
lcd.print("Ready to Launch");
delay(500);
lcd.setCursor(0,3);
lcd.print("Press 'C' to Disarm");
}
else
{
Serial.println("");
Serial.println("Press 'D' to Enter");
goto ok;
}
off:
offkey=keypad.waitForKey();
if(offkey==off[0])
{
digitalWrite(OP,LOW);
digitalWrite(green,LOW);
Serial.println("Relay Deactivated.\n");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Controller Disarmed");
lcd.setCursor(0,1);
lcd.print("Enter PIN + D to Arm");
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
goto top;
}
else
{
Serial.println("Press 'C' to Deactivate\n");
goto off;
}
dump:
if(z==0)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
goto error;
}
if(z==1)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
goto error;
}
if(z==2)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
goto error;
}
if(z==3)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
goto error;
}
if(z==4)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
goto error;
}
if(z==5)
{
digitalWrite(green,HIGH);
delay(100);
digitalWrite(green,LOW);
Serial.print("*");
dumpkey=keypad.waitForKey();
Serial.print("*");
goto error;
}
error:
Serial.println("");
Serial.print("Wrong password, Wait for 30 seconds.");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Incorrect PIN");
lcd.setCursor(0,1);
lcd.print("Wait for 30 seconds");
digitalWrite(red,HIGH);
delay(10000);
delay(10000);
delay(10000);
digitalWrite(red,LOW);
goto top;
}
UPDATE: I have narrowed the problem down to being Serial related. When I leave the serial monitor open and reset the arduino, it runs fine, but if I hook it to USB wall adapter not pc with serial monitor, it doesn't work. I still don't know how that does anything though, because I am quite new to programming, especially communication protocols like serial.
If anyone has any feedback/ideas/criticisms, please let me know. Thank you in advance!