Why Timer Arduino not functioning after I add in the function

Hi anyone who can see it. Currently I'm doing a 5 sec countdown timer based project. The coutdown timer will work perfectly alone. However, when I add it in my project, I won't print out the time as 5 sec and it is not moving. Really hope to get your help

Here is the countdown code alone:
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
long hour = 00, minute = 00, second = 05;//second
long countdown_time = (hour*3600) + (minute * 60) + second;
void setup() {

}

void loop() {

}
void timer(){
lcd.init();
lcd.backlight();
lcd.setCursor(4, 0);
lcd.print("HH:MM:SS");
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;
lcd.setCursor(4, 1);
if (countdown_hour < 10) {
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");
if (countdown_minute < 10) {
lcd.print("0");
}
lcd.print(countdown_minute);
lcd.print(":");
if (countdown_sec < 10) {
lcd.print("0");
}
lcd.print(countdown_sec);
}
delay(500);

}

Below is I wanna insert it in the whole project ( I want it countdowns when the light bright up)
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11);

#include <Keypad.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);

char input;

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = {9, 8, 7, 6};
byte colPins[COLS] = {5, 4, 3, 2};

Keypad inputKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

#include <Servo.h>
int pos = 120;
Servo servo_12;
Servo servo_13;
int RelayPin = 40;
int RelayPin2 = 41;
bool RUN= true;
String v_passcode= "1100";
String v_passcode2= "2200";

void setup(){
Wire.begin();
Serial.begin(9600);
lcd.begin(20,4);
lcd.init();
lcd.backlight();

pinMode(RelayPin, OUTPUT);
digitalWrite(RelayPin, HIGH);
pinMode(RelayPin2, OUTPUT);
digitalWrite(RelayPin2, HIGH);

mySerial.begin (9600);

servo_12.attach(12, 500, 2500);
servo_13.attach(13, 500, 2500);
intro();
}

void loop(){
RUN= true;
while (RUN){
input = inputKeypad.getKey();
if (input){
switch (input){
case '1':
rider();
break;

  case '2':
  client();
  break;

  default:
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Thank You!");
  lcd.setCursor(0,1);
  lcd.print("Please Try Again!");
  delay(1000);
  lcd.clear();
  RUN= false;
  intro();  
  break;      

}
}
}
}

void intro(){
lcd.setCursor(5,1);
lcd.print("Welcome to");
lcd.setCursor(8,2);
lcd.print("SaniC");
delay(6000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(" 1. Rider");
lcd.setCursor(0,1);
lcd.print(" 2. Client");
return;
}

void sendOtpMessage(String tel) {
String msg = "";
msg = "Your OTP is : 1100 From SaniC, Do not share your OTP";
String cmd = "";
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(1000); // Delay of 1000 milli seconds or 1 second
cmd = "AT+CMGS="" + tel + """;
mySerial.println(cmd);
delay(1000);
mySerial.println(msg); // The SMS text you want to send
delay(100);
mySerial.println((char)26); // ASCII code of CTRL + Z
delay(4000);
}

void sendOtpMessage2(String tel) {
String msg = "";
msg = "Your OTP is : 2200 From SaniC, Do not share your OTP";
String cmd = "";
mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
delay(2000); // Delay of 1000 milli seconds or 1 second
cmd = "AT+CMGS="" + tel + """;
mySerial.println(cmd);
delay(2000);
mySerial.println(msg); // The SMS text you want to send
delay(100);
mySerial.println((char)26); // ASCII code of CTRL + Z
delay(3000);
}

void rider(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter Client TEL:");
lcd.setCursor(0,1);
String tel="+60";
lcd.print(tel);

  int length=0;
  while (length<=11){
    length= tel.length();
    input= inputKeypad.getKey();
    if (input){
      tel= tel+input;
      lcd.setCursor(0,1);
      lcd.print(tel);
      }
  }

  if (length==12){
    delay(6000);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Select Box Size");
    lcd.setCursor(0,1);
    lcd.print("1. Big Box");
    lcd.setCursor(0,2);
    lcd.print("2. Small Box");

   int len=0;
   while (len<1){
    input= inputKeypad.getKey();
    if (input=='1'){
        lcd.clear();
        lcd.setCursor(2,1);
        lcd.print("Door is Opening!");
     for (pos = 45; pos <= 150; pos += 1) {
          servo_12.write(pos);
          delay(15); 
         }
        delay(6000);
        for (pos = 150; pos >= 45; pos -= 1) {
          servo_12.write(pos);
          delay(15);
       }                     
  digitalWrite(RelayPin, LOW);   //light on
  timer();     
  delay(5000);      
  digitalWrite(RelayPin, HIGH);   //light off while 
  sendOtpMessage(tel);
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Thank You, See You");
    delay(1000);
    lcd.clear();
    RUN= false;
    intro();
    break;
      }

    if (input=='2'){
        lcd.clear();
        lcd.setCursor(2,1);
        lcd.print("Door is Opening!");
     for (pos = 45; pos <= 150; pos += 1) {
          servo_13.write(pos);
          delay(15); 
         }

        delay(6000);
        for (pos = 150; pos >= 45; pos -= 1) {
          servo_13.write(pos);
          delay(15);
       }
  digitalWrite(RelayPin2, LOW);   //light on
  timer();
  
  delay(5000);

  digitalWrite(RelayPin2, HIGH);//light off while 
    sendOtpMessage2( tel);       
    lcd.clear();
    lcd.setCursor(0,0);
    lcd.print("Thank You, See You");
    delay(1000);
    lcd.clear();
    RUN= false;
    intro();
    break;
      }          
  }     
}

}

void client(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Enter the OTP :");
int len2=0;
String passcode= "";
while (len2<=3){
len2= passcode.length();
input= inputKeypad.getKey();
if (input){
passcode= passcode+input;
lcd.setCursor(0,1);
lcd.print(passcode);
}
}

  if (passcode == v_passcode){
     lcd.clear();
     lcd.setCursor(2,1);
     lcd.print("Door is Opening!");
     for (pos = 45; pos <= 150; pos += 1) {
          servo_12.write(pos);
          delay(15); 
         }

        delay(6000);
        for (pos = 150; pos >= 45; pos -= 1) {
          servo_12.write(pos);
          delay(15);
       }
    digitalWrite(RelayPin, LOW);   //light on
    timer();
    
    delay(5000);

  digitalWrite(RelayPin, HIGH);//light off while 

  }

  if (passcode == v_passcode2){
     lcd.clear();
     lcd.setCursor(2,1);
     lcd.print("Door is Opening!");
     for (pos = 45; pos <= 150; pos += 1) {
          servo_13.write(pos);
          delay(15); 
         }

        delay(6000);
        for (pos = 150; pos >= 45; pos -= 1) {
          servo_13.write(pos);
          delay(15);
       }
 
    digitalWrite(RelayPin2, LOW);   //light on
    timer();
    delay(5000);
    digitalWrite(RelayPin2, HIGH);//light off while 
  }
  else if ((passcode!= v_passcode) || (passcode!= v_passcode2)){
    delay(600);
    lcd.clear();
    lcd.setCursor(4,0);
    lcd.print("Password Wrong!");
    lcd.setCursor(4,1);
    lcd.print("Access Failure!");
    lcd.setCursor(2,2);
    lcd.print("Please try again!");
  }
    
    delay(1000);
    lcd.clear();
    RUN= false;
    intro();  
  }

void timer()
{
long hour = 00, minute = 00, second = 100;//second
long countdown_time = (hour*3600) + (minute * 60) + second;
lcd.clear();
lcd.init();
lcd.backlight();
lcd.setCursor(2,0);
lcd.print("Sanitizing");
lcd.setCursor(6,1);
lcd.print("HH:MM:SS");
long countdowntime_seconds= countdown_time - (millis()/1000);
while (countdowntime_seconds >0){
long countdown_hour= countdowntime_seconds/ 3600;
long countdown_minute= ((countdowntime_seconds/60) %60);
long countdown_sec = countdowntime_seconds%60;
lcd.setCursor(6,2);
if (countdown_hour<10){
lcd.print("0");
}
lcd.print(countdown_hour);
lcd.print(":");

if (countdown_minute <10){
lcd.print("0");
}

lcd.print(countdown_minute);
lcd.print(":");

if (countdown_sec <10){
lcd.print("0");
}

lcd.print(countdown_sec);
}
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

You should RE-edit your first post by clicking the pencil-icon
mark your whole code and delete it.

then
post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

loop() is repeatedly called on the Arduino, there's no need for the while loop which only invokes the code to service the keypad.

timer() is invoked within rider() and client() which are only invoked within loop() when specific keys are detected

seems that timer() should be invoked within loop() outside the conditional for input

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