Hi! I am writing a sketch where a passcode is entered via a keypad and the correct passcode starts a countdown timer inside the function called counter. But whenever I enter the correct passcode on the keypad it triggers the relay I have set and the LCD. And in the monitor it says Counter Starts and then it goes back to the loop and says enter passcode again. I cant find the reason it doesnt run the entire counter function.
#include <SPI.h>
#include "LCD_Driver.h"
#include "GUI_Paint.h"
#include "image.h"
#include <Keypad.h>
#define Password_Length 8
#define relay 2
#define red1 3
#define green1 4
long hour = 23, minute = 59, second = 59;
long countdown_time = (hour*3600) + (minute * 60) + second;
int s=0,m=0,h=0;
char Data[Password_Length];
char Master[Password_Length] = "4444444";
byte data_count = 0, master_count = 0;
bool Pass_is_good;
char customKey;
const byte ROWS = 4;
const byte COLS = 3;
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {A1, A2, A3, A4};
byte colPins[COLS] = {A0, A5, 5};
Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
// timer is used to keep track of current time until Unlock
PAINT_TIME timer;
void setup()
{
Config_Init();
LCD_Init();
LCD_SetBacklight(1000);
Paint_NewImage(LCD_WIDTH, LCD_HEIGHT, 0, BLACK);
Paint_Clear(BLACK);
Paint_DrawString_EN(73, 123, "WELCOME",&Font24, BLACK, GREEN);
Serial.begin(9600);
pinMode(relay, OUTPUT);
pinMode(red1, OUTPUT);
pinMode(green1, OUTPUT);
digitalWrite(red1, 0);
digitalWrite(green1, 0);
delay(1000); // 1 seconds
}
void loop()
{
Paint_Clear(BLACK);
Serial.print("\nEnter PassCODE:");
Paint_DrawString_EN(73, 100, "ENTER",&Font24, BLACK, GREEN);
Paint_DrawString_EN(73, 120, "PASSCODE",&Font24, BLACK, GREEN);
getpwd();
}
void getpwd()
{
Paint_Clear(BLACK);
while(data_count<7)
{
customKey = customKeypad.getKey();
if (customKey){
Data[data_count] = customKey;
Serial.print(Data[data_count]);
//Paint_DrawString_EN(123, 123+data_count, "*",&Font16, BLACK, GREEN);
data_count++;
}
}
if(data_count == Password_Length-1){
if(!strcmp(Data, Master)){
Serial.println("\nCorrect");
clearData();
digitalWrite(relay, 1);
digitalWrite(green1,1);
digitalWrite(red1, 0);
delay(1000);
counter();
}
else{
Serial.println("\nIncorrect");
Paint_Clear(BLACK);
Paint_DrawString_EN(123, 123, "Incorrect",&Font16, BLACK, GREEN);
//digitalWrite(relay, 0);
digitalWrite(green1,0);
digitalWrite(red1, 1);
clearData();
}
}
}
void clearData()
{
while(data_count !=0){
Data[data_count--] = 0;
digitalWrite(green1, 0);
digitalWrite(red1, 0);
digitalWrite(relay, 0);
}
return;
}
void counter() {
digitalWrite(green1, 0);
digitalWrite(red1, 0);
digitalWrite(relay, 0);
Serial.print("\nCounter Starts");
Paint_Clear(BLACK);
long countdowntime_seconds = countdown_time - (millis() / 1000);
if (countdowntime_seconds >= 0) {
long countdown_hour = countdowntime_seconds / 3600;
long countdown_minute = ((countdowntime_seconds / 60)%60);
long countdown_sec = countdowntime_seconds % 60;
if (countdown_hour < 10) {
Paint_DrawString_EN(123, 123, "0",&Font16, BLACK, GREEN);
}
Paint_DrawString_EN(123, 123, countdown_hour,&Font16, BLACK, GREEN);
Paint_DrawString_EN(123, 123, ":",&Font16, BLACK, GREEN);
if (countdown_minute < 10) {
Paint_DrawString_EN(123, 123, ":",&Font16, BLACK, GREEN);
}
Paint_DrawString_EN(123, 123, countdown_minute,&Font16, BLACK, GREEN);
Paint_DrawString_EN(123, 123, ":",&Font16, BLACK, GREEN);
if (countdown_sec < 10) {
Paint_DrawString_EN(123, 123, "0",&Font16, BLACK, GREEN);
}
Paint_DrawString_EN(123, 123, countdown_sec,&Font16, BLACK, GREEN);
}
delay(500);
}