Hi,
I am trying to build a project that will act as a lock. I want to have the string "pin" (which is the code to unlock the lock) be changed to the value of whatever numbers come through the serial, but i need it to be a string. is there any way to accomplish this?
any help would be great.
Here is my code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
#include <Keypad.h>
const byte ROWS = 4; // rows
const byte COLS = 3; // columns
char hexaKeys[ROWS][COLS] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'},
};
byte rowPins[ROWS] = {A0, A1, A2, A3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 9, 10}; //connect to the column pinouts of the keypad
Keypad myKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
char keypressed;
String pinentered;
String pin = "1234";
char newpin;
void setup(){
lcd.begin(16, 2);
Serial.begin(9600);
}
void loop(){
char keypressed = myKeypad.getKey();
if (keypressed == '#'){
if (pinentered == pin) {
lcd.setCursor(0,1);
lcd.print("Access Granted! ");
}
if (pinentered != pin) {
lcd.setCursor(0,1);
lcd.print("Access Denied! ");
}
}
else if (keypressed == '*'){
pinentered = "";
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("CLEARED! ");
}
else if (keypressed){
pinentered = pinentered + keypressed;
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print(pinentered);
}
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// read all the available characters
while (Serial.available() > 0) {
pin = String(Serial.read());
}
}
}
Thanks,
Bay
![]()