hi,
i wrote a script for the arduino to respond to a 4 digit code -but it is not working. Any idea or advice on how to fix it would be appreciated. i am a newbie 9 yoa and really keen to understand about the programming language - the guys at jaycar store suggested i make contact on this forum for guidance. If this is not the right forum, please let me know and if you could redirect me please. I appreciate your response. thanks
the code looks like this:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Keypad.h>
//constants for LEDs
int greenLED = 12;
int redLED = 11;
//set our code
char* ourCode = "1234";
int currentPosition = 0;
//define the keypad
const byte rows = 4;
const byte cols = 4;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[cols] = {8, 7, 6}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
//lcd.init();
// Print a message to the LCD.
lcd.backlight();
displayCodeEntryScreen();
//setup and turn off both LEDs
pinMode(redLED, OUTPUT);
pinMode(greenLED, OUTPUT);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, LOW);
}
void loop()
{
int l;
char key = keypad.getKey();
if (int(key) != 0) {
lcd.setCursor(14,3);
lcd.print(" ");
lcd.setCursor(14,3);
for (l=0; l<=currentPosition; ++l)
{
lcd.print("*");
}
if (key == ourCode[currentPosition])
{
++currentPosition;
if (currentPosition == 4)
{
unlockDoor();
currentPosition = 0;
}
} else {
invalidCode();
currentPosition = 0;
}
}
}
void invalidCode()
{
digitalWrite(redLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("*ACCESS DENIED!*");
lcd.setCursor(0,1);
lcd.print("**INVALID CODE**");
delay(5000);
digitalWrite(redLED, LOW);
displayCodeEntryScreen();
}
void unlockDoor()
{
digitalWrite(greenLED, HIGH);
clearScreen();
lcd.setCursor(0,0);
lcd.print("*ACCESS GRANTED*");
lcd.setCursor(0,1);
lcd.print("** WELCOME! **");
//add any code to unlock the door here
delay(5000);
digitalWrite(greenLED, LOW);
displayCodeEntryScreen();
}
void displayCodeEntryScreen()
{
clearScreen();
lcd.setCursor(0,0);
lcd.print("PLEASE ENTER");
lcd.setCursor(1,0);
lcd.print("CODE:" "");
}
void clearScreen()
{
lcd.setCursor(0,0);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(" ");
}