Set countdown time with Keypad?

Thank you!!!! This helped me very much!

It counts down now, but there is a little bug:

If I want to countdown from 20 seconds, then I enter '2000' and press enter.
That all works fine, until it eaches 9 seconds, then it shows 90>80>70>60>50>40>30>20>10>00 instead of 9>8>7>6>5>4>3>2>1>0.

If I want to countdown from 20 seconds, and I enter '20', then it counts down from 20 to 00, and the minutes are -130.

The bug happens not only with number 20, but also with other numbers.

Here is the code:

#include <LiquidCrystal.h>
#include <ShiftLCD.h>       // Include Library's
#include <Keypad.h> 

#define SETUP 0
#define SETTIME 1  //  case for setting time
#define ARMCODE 2       // case for arming bomb
#define ARMED 3    // case for countdown
#define DISARMED 4  // case for stopping countdown
#define DETONATED 5  // case for finished countdown

int bombState=0; // 0 = Setup, 1 = Set time, 2 = Arm Code, 3 = Armed ,4 = Disarmed, 5 = Detonated


int time;
int setTime;

const byte ROWS = 4; // Keypad has 4 rows
const byte COLS = 4; // Keypad has 4 columns
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},         // define which character is on a button
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {5,6,7,8}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9,10,11,12}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );    
	 
ShiftLCD lcd(2, 4, 3); // pins connected to LCD

char clearButton[1] = {'*'};
char EnterButton[1] = {'#'};
char SetupPW[] = {'1','2','3','4'}; 
char ArmCode[4] = {'7','8','9','1'};   // password to arm the bomb
char inputTimeArray[4];   // array to gather user keypad presses
char inputSetupArray[4];
char inputArmArray[4];
int i = 0; // store keypresses here
int x = 0; // store the keypresses here
int a = 0;



void setup()
{
  lcd.begin(16,2);			 // open the LCD
  // bootup screen
  lcd.print("AIRSOFT BOMB V1"); // print some text
  lcd.setCursor(4,1);
  lcd.print("BY TIBO");  
  delay(2500); // display text for 2500ms
  lcd.clear(); // clear display
}
void loop()
{
  
  // cases
  switch(bombState) {
    


    case SETUP:
    {
      SetupCode();
    }
break;    
      
      
      
    case SETTIME: // set the countdown time
    {
      SetTimeCode();
    }
        break;
    
  
    case ARMCODE: // enter the right code to arm the bomb
  {
    TypeArmCode();
  }
break;


case ARMED:
{
Countdown();
}
break;


  }
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////SetupCode//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////

boolean SetupCode() {

  lcd.setCursor(0,0);
  lcd.print("Enter Password:");
char key = keypad.getKey();
//if a key is pressed
if(key)
{
  inputSetupArray[x] = key; //store entry into array
  x++;
  lcd.setCursor(0,1); // set cursor at column 0 and row 1
lcd.print(key); // print keypad character entry to lcd
if (x == 4) // if 4 presses on the keypad have been made
 {
   if (inputSetupArray[0] == SetupPW[0] && // if first character pressed on keypad = first character in password for arming bomb
inputSetupArray[1] == SetupPW[1] &&  // and if second character pressed on keypad = second character in password for arming bomb
inputSetupArray[2] == SetupPW[2] && // and if third character pressed on keypad = third character in password for arming bomb
inputSetupArray[3] == SetupPW[3] ) // and if fourth charactet pressed on keypad = fourth character in password for arming bomb

{
  lcd.clear(); // clear display
  lcd.setCursor(0,0);
  lcd.print("Pass Correct"); // display some text
  delay(1000); // display text for 1000ms
  lcd.clear();
  bombState = SETTIME; // switch to ARMED case
  
}
else {
    lcd.clear(); // clear display
  lcd.setCursor(0,0);
  lcd.print("Pass Incorrect"); // display some text
  delay(1000); // display text for 1000ms
  x = 0;                      //Reset your entryindex so that it effectively clears your entry.
  inputSetupArray[x] = '/0';         //Always add the null
  lcd.clear();
  
}

  }
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////SetTimeCode//////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

boolean SetTimeCode() {
  

lcd.setCursor(0,0);
lcd.print("Enter Time:");
char key = keypad.getKey();
//if a key is pressed
if(key)
{

  inputTimeArray[i] = key; //store entry into array
 i++;
 inputTimeArray[i] = '/0';
  lcd.setCursor(0,1); // set cursor at column 0 and row 1
lcd.print(key); // print keypad character entry to lcd
  
 if (key == '#') {
 inputTimeArray[i] = TimeValue[i];
  lcd.clear(); // clear display
  lcd.setCursor(0,0);
  lcd.print("Time Set"); // display some text
  delay(1000); // display text for 1000ms
  bombState = ARMCODE; // switch to ARMED case
  lcd.clear();
 }
}
}


  
  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
/////////////////////////////////////////////////////TypeArmCode////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

boolean TypeArmCode() {
  
    lcd.setCursor(0,0);
  lcd.print("Enter ArmCode:");
char key = keypad.getKey();
//if a key is pressed
if(key)
{
  inputArmArray[a] = key; //store entry into array
  a++;
  lcd.setCursor(0,1); // set cursor at column 0 and row 1
lcd.print(key); // print keypad character entry to lcd
if (a == 4) // if 4 presses on the keypad have been made
 {
   if (inputArmArray[0] == ArmCode[0] && // if first character pressed on keypad = first character in password for arming bomb
inputArmArray[1] == ArmCode[1] &&  // and if second character pressed on keypad = second character in password for arming bomb
inputArmArray[2] == ArmCode[2] && // and if third character pressed on keypad = third character in password for arming bomb
inputArmArray[3] == ArmCode[3] ) // and if fourth charactet pressed on keypad = fourth character in password for arming bomb

{
  lcd.clear(); // clear display
  lcd.setCursor(0,0);
  lcd.print("Bomb Armed!"); // display some text
  delay(1000); // display text for 1000ms
  lcd.clear();
  bombState = ARMED; // switch to ARMED case
}

else {
    lcd.clear(); // clear display
  lcd.setCursor(0,0);
  lcd.print("Code Incorrect"); // display some text
  delay(1000); // display text for 1000ms
  a = 0;                      //Reset your entryindex so that it effectively clears your entry.
  inputSetupArray[a] = '/0';         //Always add the null
  lcd.clear();
  
}

  }
}
}
  
  
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////Countdown/////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

boolean Countdown() {
  
  while(i > 0) {
lcd.setCursor(0,0);
   lcd.print("Time remaining:");
   i -= 1;
   delay(1000);
   lcd.clear();
   lcd.setCursor(7,1);
   lcd.print(i);
  if (i == 0) {
    lcd.clear();
   lcd.setCursor(0,0); 
   lcd.print("Bomb Exploded");
  }
}
}

Do you know why it shows the numbers wrong?
I'll also take a look at it now.

This is the to-do-list:

  • Fix the bug
    -Change boolean to void where possible
    -Make disarm function
    -Make it idiot proof

This is interesting to watch. Everything you have done is completely different from the way I have done it yet it still seems to function like it should.

Yes it is, that's something of the cool things of Arduino.

btw: it can print the integers directly.