Hi there forum!
I am trying to build a diasarmable fake bomb, and I have pretty much figured it out, but I have 2 problems I can’t figure out.
Iḿ using a 3x4 matrix keypad to take input from the user, and a 2x16 IC2 LCD display for output.
When the user is asked to enter detonation time I write out "H: MIN: SEC: " on the screen, place the cursor at the correct place and wait for the user to enter a 2 digit string, saves the string to mins, sets the cursor at the correct place to enter MIN, and so on.
If I try to enter a number that start with 0, say 01 hours, that gets printed to the screen as 11, if I enter 02 it’s 22, and so on. But when the time is set and written to screen for confirmation, the right values show.
When the “bomb” is armed a countdown starts and is printed to screen.
I used the timer library that calls a function every seond that subtracts a second, if there are no seconds subtract a minutes and set secs to 59, and so on, and then prints to the LCD. I think I got the math part working!
The problem is that when a value goes below 10, a zero is added to the printout, so 9 becomes 90, 8 becomes 80 and so on.
The countdown is “correct”, it’s just displayes wrong.
If I it’s supposed to print out "H:00MIN:00SEC:09 " it shows as “H:00MIN:00SEC:90”.
If I set the timer to start at 10 or less it works, but if it starts over at 11 or above a get the added zero.
I did a serialprint of the values, and they show up ok without added zerors in the serial monitor.
Help please!
This is the code to set the timer:
lcd.clear();
lcd.setCursor(3,0);
lcd.print("SET TIMER:");
lcd.setCursor (0,1);
while (temp.length() < 2)
{
lcd.setCursor (2,1);
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '*':
break;
case '#':
break;
default:
temp=temp+key;
lcd.print(temp);
}
}
}
hours = temp.toInt();
if (hours > 24)
{
hours=24;
}
tempTime = hours*hoursToMilli;
detonationMilli = detonationMilli+tempTime;
temp=("");
lcd.setCursor(0,1);
lcd.print("H:");
lcd.setCursor(2,1);
lcd.print(hours);
lcd.setCursor(4,1);
lcd.print("MIN: SEC: ");
while (temp.length() < 2)
{
lcd.setCursor (8,1);
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '*':
break;
case '#':
break;
default:
temp=temp+key;
lcd.print(temp);
}
}
}
mins = temp.toInt();
if (mins > 59)
{
mins=59;
}
tempTime = mins*minsToMilli;
detonationMilli = detonationMilli+tempTime;
temp=("");
lcd.setCursor(0,1);
lcd.print("H:");
lcd.setCursor(2,1);
lcd.print(hours);
lcd.setCursor(4,1);
lcd.print("MIN:");
lcd.setCursor(8,1);
lcd.print(mins);
lcd.setCursor(10,1);
lcd.print("SEC: ");
while (temp.length() < 2)
{
lcd.setCursor (14,1);
char key = kpd.getKey();
if(key) // Check for a valid key.
{
switch (key)
{
case '*':
break;
case '#':
break;
default:
temp=temp+key;
lcd.print(temp);
}
}
}
secs = temp.toInt();
if (secs > 59)
{
secs=59;
}
tempTime = secs*1000;
detonationMilli = detonationMilli+tempTime;
temp=("");
lcd.clear();
lcd.setCursor(3,0);
lcd.print("TIMER SET:");
lcd.setCursor(0,1);
lcd.print("H:");
lcd.setCursor(2,1);
lcd.print(hours);
lcd.setCursor(4,1);
lcd.print("MIN:");
lcd.setCursor(8,1);
lcd.print(mins);
lcd.setCursor(10,1);
lcd.print("SEC:");
lcd.setCursor(14,1);
lcd.print(secs);
and this is the function for the countdown that gets called every 1000 ms by the timer
void SecondsTimer(void *context)
{
if (secs > 0)
{
secs=secs-1;
}
else if (secs <= 0 && mins > 0)
{
secs=59;
mins=mins-1;
}
else if (secs <= 0 && mins <= 0 && hours > 0)
{
secs=59;
mins=59;
hours=hours-1;
}
Serial.print("Hours: ");
Serial.println(hours);
Serial.print("Minutes: ");
Serial.println(mins);
Serial.print("Seconds: ");
Serial.println(secs);
lcd.setCursor(0,0);
lcd.print("H:");
lcd.setCursor(2,0);
lcd.print(hours);
lcd.setCursor(4,0);
lcd.print("MIN:");
lcd.setCursor(8,0);
lcd.print(mins);
lcd.setCursor(10,0);
lcd.print("SEC:");
lcd.setCursor(14,0);
lcd.print(secs);
lcd.setCursor(0,1);
}
And I’ll attach the file with my whole 479 lines of bloated ill formated code! I’m a noob, be gentle…
KeypadAndLCDTest.ino (8.29 KB)