Hello everyone,
I'm currently making a timed smokebomb for Airsoft games.
Now the problem is that I can't find out how to set the countdown time with my keypad, and display it on the 16x2 lcd.
I've already setup a case for the countdown(see my code), but it can only display the seconds.
I want to let it display the minutes and/or seconds.
For inputting the countdown time, one of the following input methods should be good:
Option 1) Input seconds and let the Arduino convert it to minutes & seconds.
for example: I input 90 seconds(I type 90 on the keypad) >>> it becomes 1 minute and 30 seconds(1:30 on the lcd).
Option 2) Make it possible to input the minutes and the seconds >>> so no convert from seconds to minutes & seconds.
So it doesn't matter if I only can set the countdown time in seconds, and let the countdown clock only display seconds.
If one of this options work, then it's good.
So basically I'm stuck with two cases:
case SETTIME:
// set countdown time with keypad
break;
case ARMED:
// count to zero from the set countdown time
break;
case ARMED:
{
lcd.clear();
while(time > 0) {
lcd.setCursor(0,0);
lcd.print("Time remaining:");
time -= 1;
delay(1000);
lcd.clear();
lcd.setCursor(7,1);
lcd.print(time);
}
if(time == 0) {
lcd.print("Bomb exploded!");
}
break;
Here is the full code that I currently have:
#include <LiquidCrystal.h>
#include <ShiftLCD.h> // Include Library's
#include <Keypad.h>
#define SETTIME 0 // case for setting time
#define ON 1 // case for arming bomb
#define ARMED 2 // case for countdown
#define DISARMED 3 // case for stopping countdown
#define DETONATED 4 // case for finished countdown
int bombState=0; // 0 = Set time, 1 = On, 2 = Armed ,3 = Disarmed, 4 = Detonated
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 ArmCode[4] = {'7','8','9','1'}; // password to arm the bomb
char inputArray[4]; // array to gather user keypad presses
int i = 0; // store keypresses here
void setup()
{
lcd.begin(16,2); // open the LCD
// bootup screen
lcd.print("AIRSOFT BOMB V1"); // print some text
delay(2500); // display text for 2500ms
lcd.clear(); // clear display
}
void loop()
{
// cases
switch(bombState) {
////////////// SET COUNTDOWN TIME//////////////////////////
case SETTIME:
HERE I WANT THE CODE TO SET THE TIME WITH THE KEYPAD
break;
///////////////////////////////////////////////////////////
//////////////////////ENTER ARM CODE/////////////////////////
case ON: // enter the right code to arm the bomb
{
lcd.setCursor(0,0);
lcd.print("Enter Code:");
char key = keypad.getKey();
//if a key is pressed
if(key)
{
inputArray[i] = key; //store entry into array
i++;
lcd.setCursor(0,1); // set cursor at column 0 and row 1
lcd.print(key); // print keypad character entry to lcd
if (i == 4) // if 4 presses on the keypad have been made
{
if (inputArray[0] == ArmCode[0] && // if first character pressed on keypad = first character in password for arming bomb
inputArray[1] == ArmCode[1] && // and if second character pressed on keypad = second character in password for arming bomb
inputArray[2] == ArmCode[2] && // and if third character pressed on keypad = third character in password for arming bomb
inputArray[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
bombState = ARMED; // switch to ARMED case
}
}
}
}
break;
////////////////////////////////////////////////////////////////////////
case ARMED:
{
lcd.clear();
while(time > 0) {
lcd.setCursor(0,0);
lcd.print("Time remaining:");
time -= 1;
delay(1000);
lcd.clear();
lcd.setCursor(7,1);
lcd.print(time);
}
if(time == 0) {
lcd.print("Bomb exploded!");
}
break;
}
}
}
How should I do this?
I would really appreciate help on this
Regards,
Tibo