Hello,
I am currently working on a school project and I have decided to create a locking mechanism using an 1602 LCD with a I2C module, a basic matrix keypad, and 2 different colored LED's (green and red).
I am having an issue where when i enter the correct pin/password and my greenLED will not turn on. My redLED turns on when I input the wrong answer. I have attached my code and if you guys need a picture of the hardware then I'll post it.
I have attached my code which I am using and should not be too hard to understand.
*Forgot to mention that I am using the Arduino Mega 2560
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define Password_Length 8
#define greenLED 11
#define redLED 10
//
//RedLED = 10;
//GreenLED = 11;
char Data[Password_Length];
char Master[Password_Length] = "1234567";
int lockOutput = 13;
byte dataCount = 0;
char key;
const byte ROWS = 4; // use 4X4 keypad for both instances
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2', '3', 'A'},
{'4','5', '6', 'B'},
{'7','8', '9', 'C'},
{'*','0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad kpd( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup(){
// Wire.begin( );
kpd.begin( makeKeymap(keys) );
Serial.begin(9600);
lcd.backlight();
lcd.init();
pinMode(lockOutput, OUTPUT);
pinMode(redLED, OUTPUT); //RED LED
pinMode(greenLED, OUTPUT); //GREEN LED
}
//byte alternate = false;
void loop(){
// alternate = !alternate;
lcd.setCursor(0,0);
lcd.print("Enter Pin: ");
key = kpd.getKey();
if(key){
Data[dataCount] = key;
lcd.setCursor(dataCount, 1);
lcd.print(Data[dataCount]);
dataCount++;
}
if(dataCount == Password_Length-1 ){
lcd.clear();
if(!strcmp(Data, Master)){
lcd.print("Correct!");
digitalWrite(11, HIGH);
digitalWrite(10, LOW);
lcd.clear();
lcd.print("Locking in 5 Sec!");
delay(5000);
lcd.print("Locked!");
}else {
lcd.print("INCORRECT!");
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
delay(1000);
digitalWrite(10, LOW);
}
lcd.clear();
clearData();
}
}
void clearData(){
while(dataCount != 0) {
Data[dataCount--] = 0;
}
return;
}