Hi,
Bad english and first time writing in this forum. Hop you can understand:-)
I am using an Arduino Mega to control a briefcase bomb for an home made Escape Room.
When powering, the 4 digit on the TM1637 should show 60:00 and when triggered by a remote the countdown from 60 minutes should begin. When reaching 00:00 the numbers blink 10 times and stop.
I have a keypad and a joystick that can stop the timer. These are working fine, so I havent put them in my code here.
My problems are:
-
When reaching about 35:25 the timer starts over from 60:00. This is my main concern. I think it is because I can not figure out how to get a constant value from Millis() when starting with my remote, but I am not sure
-
I can not get a colon from the beginning. The display shows 6000 and not 60:00. I have tried using “display.showNumberDecEx(0,64);” I get my colon, but the 3 first digits and the colon is flickering.
-
A blinking colon through out the 60 minutes would be nice, but how?
Here is my code:
// remote---------------------Remote setup--------------------------------------
#include <IRremote.h>
const int RECV_PIN = 16;
IRrecv irrecv(RECV_PIN);
decode_results results;
// millis setup----------------------------Millis setup----------------------------------------
unsigned long newMillis = 0;
unsigned long starttime = 0;
int start = 0;
int ending = 0;
//countdown------------------------------Countdown setup-------------------------------------
// DEFINES
// Macros to retrieve the fractional seconds and minute parts of a time
// supplied in ms
#define numberOfSeconds(_time_) ((_time_ / 1000) % 60)
#define numberOfMinutes(_time_) (((_time_ / 1000) / 60) % 60)
// INCLUDES
// https://github.com/avishorp/TM1637
#include <TM1637Display.h>
// CONSTANTS
const uint8_t OFF[] = {0, 0, 0, 0};
// In this library, the byte order is .GFEDCBA
const uint8_t SLUT[] = {B0111111, B0111111, B0111111, B0111111};
const uint8_t START[] = {B1111101, B0111111, B0111111, B0111111};
// GLOBALS
// Create a display object, specifying parameters (Clock pin, Data pin)
TM1637Display display(14, 15);
//set timer-----------------------set timer-----------------------------------------------
// 1000ms in 1sec, 60secs in 1min, 60mins in 1hr. So, 1000x60x60 = 3600000ms = 1hr
unsigned long timeLimit = 3600000;
// Calculate the time remaining
unsigned long timeRemaining = 3600000;
//--------------------------------------------SETUP--------------------------------------------
void setup(){
// remote
irrecv.enableIRIn();
//countdown
Serial.begin(9600);
// Set brightness
display.setBrightness(0x0c);
// Clear the display
display.setSegments(OFF);
}
void displayText() {
display.setSegments(SLUT);
}
void displayText2() {
display.setSegments(START);
}
void displayText3() {
display.setSegments(OFF);
}
//------------------------------------------------LOOP------------------------------------------
void loop(){
if(start == 0)
{displayText2();
}
if(start == 1)
{starttime = millis();}
newMillis = millis() - starttime;
//remote
if(irrecv.decode(&results))
irrecv.resume();
switch(results.value)
{
case 0xFFC23D: //Keypad button "play/pause"
start = start + 1;
if(timeRemaining > 100) {
// To display the countdown in mm:ss format, separate the parts
int seconds = numberOfSeconds(timeRemaining);
int minutes = numberOfMinutes(timeRemaining);
// This displays the seconds in the last two places
display.showNumberDecEx(seconds, 0, true, 2, 2);
// Display the minutes in the first two places, with colon
display.showNumberDecEx(minutes, 0x80>>3, true, 2, 0);
// Update the time remaining
timeRemaining = timeLimit - newMillis;
}
else{
displayText();
delay(300);
displayText3();
delay(300);
ending = ending + 1;
}
if(ending == 10)
{
exit(0);
}
} }