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:
-
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.
-
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)