Hi guys im new here, aswell as to arduino. Im trying to make a lil home project and im completely stuck. the thing is i got this keypad, lcd screen and a relay card assambled with an arduino.
The project was to use the keypad to activate the relay ect, and after a clock would be shown on the lcd display. I can get both of the programs to work separately but i cant get it to work together to show the clock after my password is input. The clock shows and updates only when i press # on the keypad.
Il post my code and maby someone can point me in the right direction to understand whats wrong.
Best regards
#include <Password.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
LiquidCrystal lcd(2,3,4,9,10,11,12);
const int buttonHr = 7;
const int buttonMin = 8;
int secs =0;
int secs2 = 0;
int mins = -1;
int hrs = 0;
boolean isAM = true;
int milliDivSecs = 1000;
int milliDivMins = 60000;
int milliDivHrs = 360000;
unsigned long prevmillis=0;
int interval = 1000;
Password password = Password( "4321" );
const byte ROWS = 4;
const byte COLS = 3;
char keys[ROWS][COLS] = {
{
'1','2','3', }
,
{
'4','5','6', }
,
{
'7','8','9', }
,
{
'*','0',' ', }
};
byte rowPins[ROWS] = {
25, 24, 23, 22};
byte colPins[COLS] = {
28, 27, 26};
const int buttonPin = 7;
int buttonState = 0;
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
#define ledPin 13
void setup(){
lcd.begin(16, 1);
lcd.print(" Ect.. ");
delay(5000);
lcd.clear();
lcd.print("Password: ");
Serial.begin(9600);
keypad.addEventListener(keypadEvent);
keypad.setDebounceTime(250);
pinMode(buttonHr, INPUT);
pinMode(buttonMin, INPUT);
}
void loop(){
int interval = 1000;
keypad.getKey();
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
lcd.clear();
}
}
void keypadEvent(KeypadEvent eKey){
switch (keypad.getState()){
case PRESSED:
lcd.print('*');
switch (eKey){
case ' ':
guessPassword();
break;
default:
password.append(eKey);
}
}
}
void guessPassword(){
if (password.evaluate()){
digitalWrite(ledPin,HIGH);
digitalWrite(8, HIGH);
lcd.clear();
lcd.print(" On ");
password.reset();
delay(5000);
lcd.clear();
}
else{
digitalWrite(ledPin,LOW);
lcd.clear();
lcd.print("Try again");
password.reset();
delay(1000);
lcd.clear();
lcd.print("Password:");
lcd.clear();
}
lcd.setCursor(5,0);
lcd.print("12:00:00 AM");
unsigned long currmillis = millis();
//detect button pressing
if(digitalRead(buttonHr) == HIGH){
delay(25);
hrs = hrs+1;
updateHrs();
//update AMPM on button press
if(hrs==12){
updateAMPM();
}
delay(400);
}
if(digitalRead(buttonMin) == HIGH){
delay(25);
mins = mins + 1;
//Serial.println(mins);
updateMin();
delay(400);
}
if(currmillis-prevmillis > 999){
//lcd.clear();
prevmillis =currmillis;
if(secs<10){
lcd.setCursor(12,0);
lcd.print(secs);
lcd.setCursor(11,0);
lcd.print(0);
}
else{
lcd.setCursor(11,0);
lcd.print(secs);
}
//display minutes
if(secs == 0){
mins = mins+1;
updateMin();
}
//get new seconds from system time
secs = (millis()/milliDivSecs)%60; // divide by 1000 and mod by 60 gives seconds from milliseconds
}
}
//update min function
//calls the update am apm funciton and the update hours functions.
void updateMin(){
if(mins > 59){
hrs = hrs+1;
updateHrs(); //update hours then
if(hrs==11 && mins >59){
updateAMPM();
}
mins = 0; //reset mins
lcd.setCursor(8,0);
lcd.print("00");
}
if(mins < 10){
lcd.setCursor(9,0);
}
else{
lcd.setCursor(8,0);
}
lcd.print(mins);
}
//update hour function
void updateHrs(){
//display hours - needs fixing for am pm
if(hrs> 12){
//reset to 1
hrs = 1;
}
if(hrs< 10){
lcd.setCursor(5,0);
lcd.print(" ");
lcd.setCursor(6,0);
}
else{
lcd.setCursor(5,0);
}
lcd.print(hrs);
}
void updateAMPM(){
if(isAM){
isAM = false;
lcd.setCursor(14,0);
lcd.print("PM");
}
else{
isAM = true;
lcd.setCursor(14,0);
lcd.print("AM");
}
}