Need Help with Passcode sketch

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);
}


in the meantime it is suggested that you place some Serial.print statements at strategic points within the errant function to see if it is doing what you think and that the variables informing your logic are all plausible values.

a7

Thanks! Yes I have the counter function in a separate sketch with serial print and know that it works as intended and counts down. In the full sketch posted here I have just added it, but still the same. It seems to go back and run the loop all over again.

You have added your counter function nested inside of everything else.
So all your functions are nested into nested-nested functions.
I haven't analysed your code in detail but I guess
your if-condition is only true once

  if (data_count == Password_Length - 1) {

because inside this if-condition you do

      clearData();

and I guess this changes data_count
and then the if-condition is no longer true

and this means your function counter() is not called again

  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();
    }

Programming is not just stuffing things together. You have to analyse the highlevel logic.
After successfully entering a valid password your counter shall count-down.
Usually this is done by setting a flag-variable and then you place the function-call to counter() in loop()
as pseudo-code

void loop()

  getpasswd(); // inside function getpasswd() if password is valid set a flag-variable to TRUE
  
  if (passwordValid = TRUE) {  // <== attention single-equal-sign is WRONG!
    counter();
  }
}

for comparing the double-equal-sign

==

must be used
compare with
if (passwordValid == TRUE)

best regards Stefan

Thank you Stefan! This got me in the right direction for sure.

Note: This should be:
if (passwordValid == true) {
or, if 'passwordValid' is of type 'bool' or 'boolean':
if (passwordValid) {

Hi John,

thank you very much for correcting my typo.
Yes of course double-equal-sign == compares
single-equal-sign a-ssigns.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.