When Any Keys Are Pressed, The Countdown Time is Ideally Paused

I got a program (a countdown time) that displays “PLEASE PAY 5 PESOS” after one minute countdown.

While the countdown is on, the user has the option to press any key. When the user does that, the countdown stops and displays “CONTINUE CHARGING? YES(1) OR NO(2)”. When the user pressed “1”, it would display “CHARGING RESUMED. PAY LATER!!”. When the user pressed “2”, it would display “CHARGING STOPPED. ARE YOU LOSING MONEY? GIVE ME YOUR PASSWORD” and the user will enter his password. While all those processes happen, it is ideal that the countdown should be paused.

When the user accidentally pressed any keys (and has no intention of pressing at that moment), the countdown should be resumed after a pre-determined time.

This is the whole code:

#include <Keypad.h>
#include<LiquidCrystal.h> 
#include <Wire.h> 

LiquidCrystal lcd(0,1,5,4,3,2); 

int hours = 0; // start hours
int minutes = 0; //start min
int seconds = 59; //start seconds
int row[]={9,8,7,6};// Defining row pins of keypad connected to Arduino pins
int col[]={13,12,11,10};//Defining column pins of keypad connected to Arduino
int i,j,lcd_count,count=1,key_id=0,flag,entry=0,entry6=0,key=0;// See About the Program 
int col_scan;// Variable to identify a key press
char temp_press; // Variable to hold value of key press

void setup()
{
lcd.begin(16,2);

              for(i=0;i<=3;i++)
               {
                      pinMode(row[i],OUTPUT);
                      pinMode(col[i],INPUT);
                      digitalWrite(col[i],HIGH);
               }

            
}


/* Main Program Begins */
void loop()
{


CountdownTime();

while(entry6<0)
 keyscan();
 if(temp_press!='E')
 {
  WhileTimeIsRunning();
 }

}    
/* Main Program Ends */


void CountdownTime()
{
 
 lcd.begin(16, 2);
 lcd.print("TIME LEFT(H:M:S)");
 delay(150);
 
 while (hours>0||minutes>0||seconds>=0) 
 {
 
 lcd.setCursor(4, 2);
 (hours < 10) ? lcd.print("0") : NULL;
 lcd.print(hours);
 lcd.print(":");
 (minutes < 10) ? lcd.print("0") : NULL;
 lcd.print(minutes);
 lcd.print(":");
 (seconds < 10) ? lcd.print("0") : NULL;
 lcd.print(seconds);
 lcd.display();
 stepDown();
 delay(500);

            
 }


}


void stepDown() 
{



    if (seconds > 0) 
      {
         seconds -= 1;
      }
    
    else 
           {
              if (minutes > 0) 
                   {
                     seconds = 59;
                     minutes -= 1;
                   }
              else 
                   {
                      if (hours > 0)
                          {
                              seconds = 59;
                              minutes = 59;
                              hours -= 1;
                          } 
                        
                      else 
                          {
                                trigger();
                          }
                    }
           }

}

void trigger() 

{
 lcd.clear(); // clears the screen and buffer
 lcd.setCursor(3, 0); // set timer position on lcd for end.
 lcd.println("PLEASE PAY");
  lcd.setCursor(3, 1);
 lcd.println("5 PESOS");
 delay(1000);
 lcd.display();
}



void WhileTimeIsRunning()
{
       if (key_id=1)
             {
                  if(temp_press!='E')    
                                           {
                                               lcd.clear();
                                               lcd.setCursor(0,0);
                                               lcd.print("CONTINUE CHARGING?");
                                               lcd.setCursor(0,1);
                                               lcd.print("YES(1) OR  NO(2)");
                                               delay(1000);       
                                               ChargingOrNot();
                                                  if (entry6==2)
                                                        {
                                                                entry6=0;
                                                              
                                                        }
                                           }  
                                           
              }
              
        entry6=0;
        key_id=0;      
                                  
                           
}

(SEE MY NEXT POST “When Any Keys Are Pressed, The Countdown Time is Ideally Paused 2”, BECAUSE MY CODE IS LONG)

(AND PLEASE SEE MY SCREENSHOT. THIS IS WHERE I TEST MY CODE)

This are the errors I observed:

  1. In my simulation, The countdown time does run, but when I pressed any key, the countdown time still runs. Ideally, when I pressed any key, the countdown time should pause and an LCD display should be “CONTINUE CHARGING? YES(1) OR NO(2)”. It didn’t happen on my simulation and no idea how to fixed it…I don’t know where is the flaw in my code.

  2. In my simulation, before posting my code above, I was frustrated because when I pressed any key, it needs to be long-pressed before accepting an input. Is that a result of too many if-else statements or something else?

Please help me out…

CountdownBePaused.ino (11.5 KB)

void ChargingOrNot()
{
           keyscan();
                       if(key_id==1)
                              {
  
                                      if (temp_press=='1')
                                               {    
                                                   //pass2[entry6]=temp_press;
                                                   lcd.setCursor(0,0);
                                                   lcd.print("CHARGING RESUMED");
                                                   lcd.setCursor(0,1);
                                                   lcd.print(" PAY LATER!! ");
                                                   delay(1000);
                                                   lcd.clear();
                                                                                                                     
                                               }

                                     else if (temp_press=='2')
                                               {
                                                     
                                                    //pass2[entry6]=temp_press;
                                                    lcd.setCursor(0,0);
                                                    lcd.print("CHARGING STOPPED");
                                                    lcd.setCursor(0,1);
                                                    lcd.print("ARE YOU LOSING MONEY?");
                                                    delay(1000);
                                                    lcd.clear();
                                                    lcd.setCursor(0,0);
                                                    lcd.print(" GIVE ME YOUR ");
                                                    lcd.setCursor(0,1);
                                                    lcd.print(" PASSWORD ");
                                                    delay(1000);                                                    
                                                    lcd.clear();
                                                }
              
                                      else  
                                                {    
                                                     
                                                     lcd.setCursor(0,0);
                                                     lcd.print("PRESS ONLY 1 AND 2");        
                                                     lcd.setCursor(0,1);
                                                     lcd.print(" AGAIN!! ");
                                                     delay(500);
                                                     
                                                     
                                                  
              
                                               }
                              } 

key_id=0;   
                                            
}

(SEE MY NEXT POST "When Any Keys Are Pressed, The Countdown Time is Ideally Paused 3" BECAUSE MY PROGRAM IS LONG)

char keypress(int i, int j) // Subroutine to Identify the value of Key pressed
{
          if(i==0&&j==0)
                {
                   return('1');
                }
                
          if(i==0&&j==1)
                {
                   return('2');
                }
                
          if(i==0&&j==2)
                {
                   return('3');
                }
                
          if(i==0&&j==3)
                {
                   return('A');
                }
                
          if(i==1&&j==0)
                {
                   return('4');
                }
                
          if(i==1&&j==1)
                {
                   return('5');
                }
                
          if(i==1&&j==2)
                {
                   return('6');
                }
                
          if(i==1&&j==3)
                {
                   return('B');
                }
                
          if(i==2&&j==0)
                {
                   return('7');
                }
                
          if(i==2&&j==1)
                {
                   return('8');
                }
                
          if(i==2&&j==2)
                {
                   return('9');
                }
                
          if(i==2&&j==3)
                {
                   return('C');
                }
                
          if(i==3&&j==0)
                {
                   return('*');
                }
                
          if(i==3&&j==1)
                {
                   return('0');
                }
                
          if(i==3&&j==2)
                {
                   return('#');
                }
                
          if(i==3&&j==3)
                {
                   return('D');
                }
} // Subroutine to identify Keypress Ends

(YOU SHOULD HAVE READ MY FIRST TWO POSTS...Many thanks for helping me out)

I have asked the Moderator to merge your 3 Threads.

When code is too long just add your .ino file as an attachment. And please do that now. Joining three pieces of code together can introduce errors.

...R

Okay, I've done it....I put the whole code in the attachment....You may download it and modify it to correct the errors that I just posted. Many thanks!!!

Merged.

I attached and put my whole code in the .ino file for everyone can see…

Here it is…

CountdownBePaused.ino (11.5 KB)

While all those processes happen, it is ideal that the countdown should be paused.

When the user accidentally pressed any keys (and has no intention of pressing at that moment), the countdown should be resumed after a pre-determined time.

I'm not clear what is the role, or range of the countdown.

How long does the countdown last?
What happens when it expires?
How is it restarted?

From a brief look at your code it seems as if you see the countdown as a separate thing whereas it needs to be integrated so that every iteration of loop() moves it on a bit (or at least checks if it should move on).

I think it would be easier to achieve what you want with code that was organized something like this

void loop() {
   readKeypad();
   respondToKeyPress();
   updateLCD();
   updateCountdown();
}

Have a look at Planning and Implementing a Program

If you use a variable to keep track of the state of the system (whether it is waiting for another keypress, or is updating a password etc) it would be simple to use that to determine whether the countdown should be suspended.

...R

Well, I'm actually working on a project called "Charging Station"....but with security features, primarily passwords.

The code I posted is just only the part of the actual whole code for that charging station....

This is actually the whole idea of what I'm working now :o

AT START-UP:
When the owner first switched on the charging station, the solenoid door lock shall be in a “lock” position (The door can’t be opened). Then a message appears on LCD display “Welcome! Press any key to continue.” When the owner pressed any keys on the keypad, the LCD will then display “Please enter the password, my master.” The owner will enter his own password (it should be a long password or at least 6 numbers). Now, after that, the LCD will display “The Charging Station is ready to be used.” The owner will then leave the charging station. The solenoid door lock is still on “lock” position during the whole start-up process.

IN SECURE MODE:
When the LCD displayed “The Charging Station is ready to be used” and a user wants to use it to charge his phone, the user will put a 5 peso coin on the coin slot to “de-lock” the solenoid door lock (the door can be opened). The user then put his phone in the compartment and a charger wire is inserted in the phone (no current is churned out this time). When the door closes, the LCD then displays ““The Charging Station is ready to be used. Press 1 to continue.” When the user pressed the “1” on the keypad, the solenoid lock is now activated (“lock” position). The LCD will display “This is your password: XXXXXXX. Please enter the displayed password to start charging process”. After the user done that, the LCD will display the remaining time (countdown timer). Now, the charging station is doing its work.

When the time has expired (after 30 minutes), the LCD will display “Please insert 5 peso coin (that is the currency in the Phillippines) to continue charging”. The charging station will do this indefinitely.

If the user wants to get his phone early, he will press any key and the LCD will display “Continue charging? Yes (Press 1) or No (Press 0)”. If the user presses “1”, the charging continues and if the user presses “0”, the LCD will then display “Please enter your password to get your phone.” If the user forgets his password, he should call the owner of the charging station. If the owner of the phone accidentally pressed any keys and had no intention to get his phone, the display "Continue charging? Yes (Press 1) or No (Press 0)" should last for 10 seconds and resumes the countdown after that time elapsed.

Hope you take time in reading....I've been working on it for almost two months, but to no avail....

Only the countdown part is the problem now..At least you might have an idea why am I using countdowns

Basically my charging station is very similar to in playing arcade games in malls, but with a catch:

I put passwords to discourage theft.

:confused:

I would really try to use that code, but I wonder if it would be simulated on my Proteus 7. I got this bad experience:

When I used this part of code in my Charging Station before:

#include <Keypad.h>
#include <Password.h>
#include <LiquidCrystal.h>

Password password = Password( "1234" );
byte maxPasswordLength = 6;
byte currentPasswordLength = 0;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
 
//Define the keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

 
byte rowPins[ROWS] = {11,10,9,8}; //connect to row pinouts
byte colPins[COLS] = {7,6,5,4};

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(3,4,5,6,7,8);

when I tried to simulate it in my Proteus ISIS 7, the LCD in the simulation doesn’t show any display!

That’s why I resorted to “brute force” programming. Notice that I even tell the Arduino to give the “1” in the keypad by making this code:

char keypress(int i, int j) // Subroutine to Identify the value of Key pressed
{
          if(i==0&&j==0)
                {
                   return('1');
                }

I wonder if the part of the code you gave me would be simulated.

void loop() {
   readKeypad();
   respondToKeyPress();
   updateLCD();
   updateCountdown();
}

Worth a try though. :o
I’ll try again…ahahaa!

your problems seem to be related to the use of delays and the lack of timers.

If a key is pressed start a timer that sets a flag and blocks the countdown. If the timer times out then remove the flag and allows the countdown to restart

the use of delays in the program is causing your key presses to be delayed. A delay stops the code until the delay is finished. Any inputs during this time are not updated. Learn to code using millis timers instead of delays. Also try to avoid using "while" unless your plan calls for it. A "if" works better in most cases unless you really have to stop the program or hold it in a small loop until something happens.

I see no attempt to trouble shoot the code using the serial port. You may have removed all the serial print lines but I would have expected to see serial.begin in setup. Serial print is a pain in the arse but its the best tool available to see whats going on in the background.

I don't have a keypad or lcd so I can offer no real assistance other than to read the code and compare the problem you are facing verses what the code says.

gpop1:
your problems seem to be related to the use of delays and the lack of timers.

If a key is pressed start a timer that sets a flag and blocks the countdown. If the timer times out then remove the flag and allows the countdown to restart

I see no attempt to trouble shoot the code using the serial port. You may have removed all the serial print lines but I would have expected to see serial.begin in setup. Serial print is a pain in the arse but its the best tool available to see whats going on in the background.

I don’t have a keypad or lcd so I can offer no real assistance other than to read the code and compare the problem you are facing verses what the code says.

Yeah…I should have begin in the part “Serial print” somewhere in the code, because when I made the circuit in the Proteus ISIS 7 and simulated it, the LCD display is blank !(according to simulations)

Well, fortunately the Proteus ISIS 7 comes handy for me…I attached a picture of my circuit where I test my code. You can try my circuit as well…

Guys....I'm considering rewriting the code all over again. It seems my code is flawed.

I think too linearly, coupled with ignorance about pause, resume and doing this "

Robin2:
it seems as if you see the countdown as a separate thing whereas it needs to be integrated so that every iteration of loop() moves it on a bit (or at least checks if it should move on).

"

as someone suggested me to do....

Guys, if you were in my situation now, the same problem. How do you code this if you were given this problem?

The problem goes like this:

The LCD first displays “The Charging Station is ready to be used. Press any to continue.” When the user pressed the “1” on the keypad, the LCD will display “Please enter the password to start the charging process”. After the user done that, the LCD will display the countdown time (30 minute countdown time). Now, the charging station is doing its work.

When the time has expired, the LCD will display “Please insert 5 peso coin to continue charging.” The charging station will do this indefinitely.

If the user wants to get his phone early, he will press any key and the LCD will display “Continue charging? Yes (Press 1) or No (Press 0)”. If the user presses “1”, the charging continues and if the user presses “0”, the LCD will then display “Please enter your password to get your phone.” If the user forgets his password, he should call the owner of the charging station.

If the owner of the phone (the user) accidentally pressed any keys and had no intention to get his phone, the display "Continue charging? Yes (Press 1) or No (Press 0)" should last for 10 seconds and resumes the countdown after that time elapsed.

I attached the picture for you to see how I made the wiring. And don't worry. The concern here is the proper display of LCD when those processes happens.

When I do the coding, it is flawed. Can you show me how you make the code if you were confronting that problem posed above? My way is flawed. Please show it to me how you coded it....I owe you a lot...

I used the Proteus 7 to simulate the circuit. Please try it when you are making the code and simulating it.

Thanks!!!

First thing you need to do is slow down and give people time to reply to each of your posts before you add another one. And it may be wise to wait and think about your problem until you are clear about what question you want to ask. So far the Thread reads as if you are thinking out loud. That is a good thing to do for your own purposes but it is just confusing on the Forum.

I suggested earlier how I think you should approach the program. And I think what I suggested is consistent with what you wrote in Reply #8

I suspect you are still at the stage where thinking will be more profitable than writing code.

Think of the project as a collection of separate little activities that have some variables that they use to share information between them.

Also, you refer to a countdown process but millis() is the most convenient timekeeper for what you want and it is a count-up timer.

...R

don't get confused between what the lcd is displaying and what the code is doing.

lcd printing is nothing more than a output. Its along the same lines as a digital write. You need to plan the logical order of the code first.

write a simple logic order on paper with out concentrating on the lcd display. who says what and when is the easy part.

I would start with something like this. Once it looks good on paper I would write the code in the same order.

1/ waiting for keyboard input key 1
2/ if keyboard input is 1 turn on lcd backlight
3/ start a exit timer
4/ monitor for password
4a/ check password if password fails back to step 1
4b/ if password is excepted move to step 5
4c/ if password is not entered with in time exit back to step 1 reset timer
5/ open door, start charger, start door timer
5a/ if door is not closed after timer is done stop charger
6/ monitor door if door closed start charge timer
6a/ if door timer was done restart charger
7/ monitor keypad and charge count down
8/ if charge count down is zero stop charger