I have a TM1637 display, but all timers here always show mm:ss. What I want is the display show ss.t while leaving the last digit blank when under a minute (like 46.2). So it should go from 1:00 to 59.9, not 0:59. How do I do so?
How are you keeping time? Please post your sketch in code tags. Otherwise there is no context for your question, so you can only get a very generic answer.
#include <Arduino.h>
#include <TM1637Display.h>
// Module connection pins (Digital Pins)
#define CLK 4
#define DIO 5
// Constants
const uint8_t OFF[] = {0, 0, 0, 0};
const unsigned int buzz_freq = 500;
short int buzz_pin = 9;
// Global Variables
long int count_time; // 1000ms in 1sec, 60secs in 1min, 60mins in 1hr. So, 1000x60x60 = 3600000ms = 1hr
long int reset_value; // copy this value into count_time when the button is pressed next time
unsigned long NewTime = 0;
unsigned long current_time = 0;
unsigned long previous_time = 0;
short int position_button = 6; // Position button to Pin 6
short int increase_button = 3; // Increase button to Pin 3
short int decrease_button = 7; // Decrease button to Pin 7
short int start_button = 2; // Start the timer button to Pin 8 (Start = pause button)
volatile short int start_pause = 0;
int seconds, minutes;
unsigned int left_value = 0;
unsigned int right_value = 0;
int Num = 0; // For switching between left & right part of the display(Even = left, Odd = right)
TM1637Display display(CLK, DIO);
void setup(){
//Serial.begin(9600);
display.setBrightness(2); // Display brightness (0= min, 7= max)
display.clear();
pinMode(start_button, INPUT);
pinMode(increase_button, INPUT);
pinMode(decrease_button, INPUT);
pinMode(position_button, INPUT);
pinMode(buzz_pin, OUTPUT);
/*seconds = (count_time % 60); // To display the countdown in mm:ss format
minutes = ((count_time / 60) % 60); //separate CountTime in two parts
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0b11100000, true, 2, 0);
delay(100);*/
// If start button changes from HIGH to LOW then START/PAUSE the timer
attachInterrupt(digitalPinToInterrupt(start_button), check_button, FALLING);
}
void loop(){
if(start_pause == 1){
delay(250);
start_pause = 0;
//Serial.println(left_value*60 + right_value);
// Pass this value to countDown function to start the timer
if(count_time == 0)
{
count_time = reset_value;
}
countDown(); // Function call
}
if(digitalRead(position_button) == 0){
Num++;
Serial.println(Num);
delay(200);
// Sets the time for left part of the display
while(Num % 2 == 0 && digitalRead(position_button) == 1 && start_pause == 0)
{
if(digitalRead(increase_button) == 0){
left_value++;
delay(150);
}
if(digitalRead(decrease_button) == 0){
left_value--;
delay(150);
}
display.showNumberDec(left_value, false, 2, 0);
} // End of 1st while loop
display.clear();
// Sets the time for right part of the display
while(Num % 2 != 0 && digitalRead(position_button) == 1 && start_pause == 0)
{
if(digitalRead(increase_button) == 0){
right_value++;
delay(150);
}
if(digitalRead(decrease_button) == 0){
right_value--;
delay(150);
}
display.showNumberDec(right_value, false, 2, 2);
} // End of 2nd while loop
display.clear();
count_time = left_value*60 + right_value; // This is for if you want to start the timer next time
reset_value = count_time; // with the previous set-time
}
}
void countDown(){
delay(250);
while(count_time > 0 && start_pause == 0)
{
current_time = millis();
if(current_time - previous_time >= 1000) // Update the display after every one second
{
NewTime = count_time - 1; // Calculate the time remaining
seconds = (NewTime % 60); // To display the countdown in mm:ss format
minutes = ((NewTime / 60) % 60 ); //separate CountTime in two parts
display.showNumberDecEx(seconds, 0, true, 2, 2); // Display the seconds in the last two places
display.showNumberDecEx(minutes, 0b11100000, true, 2, 0); // Display the minutes in the first two places, with colon
count_time = NewTime; // Update the time remaining
previous_time = current_time; // For one second time interval
left_value = minutes;
right_value = seconds;
}
} //End of while loop
if(count_time == 0) {
// Count down is completed, now make the display OFF
delay(200);
start_pause = 0;
Num = 0;
for(int i = 0; i < 10; i++)
{
// Turn ON the buzzer for 10 times and also Blink the display
//tone(buzz_pin, buzz_freq);
display.showNumberDec(0, true);
delay(300);
//noTone(buzz_pin);
display.clear();
delay(300);
}
}
else {
// Display the time stored when the Pause(Start) button is pressed
display.showNumberDecEx(seconds, 0, true, 2, 2);
display.showNumberDecEx(minutes, 0b11100000, true, 2, 0);
start_pause = 0;
delay(100);
}
} //End of void countDown function
void check_button()
{
start_pause = 1;
}
The first change is to count in multiples of 1/10 second instead of 1 second...
if(current_time - previous_time >= 100) // Update the display after every one second
Do you see the changes you would make, once you're counting in 0.1 second increments? You just need to adjust a few lines to extract the time units accordingly.
It should still stay in mm:ss if the time is 1 minute or higher. Only when the time is less than a minute the dispay should show ss.t.
Makes no difference. That concerns what you display not what you calculate...
So you can just add an 'if-else' to display time less than one second differently.
It will be easier to use the same time units throughout the execution, than to change them in mid stream.
I can see the code... forum software has delays...
Yes, now....
one more thing... if in a branch that executes 10 times every second, you can have a count to ten timer using a few simple lines of code that will instantly give you whole seconds. That would minimize any changes you would need to make to the existing code. With that, you could also make a function that would return sub seconds, to make the display code easy.
Hello
You may take a view to get some ideas.
Many displays which have a colon do not also have decimal point. What type are you using?
AFAIK, none of the readily available TM1637 modules have both colon and decimals, or individual control of the two colon points.
You will have to put up with the colon for the seconds and decimal.
I could turn off the top colon LED while leaving the bottom colon LED on. This could be a decimal point.
This is what I did when displaying elapsed time on a 4 digit 7-segment display.
Seconds and hundredths from 0.00 to 99.99 (steady decimal point)
Minutes and seconds from 1:40 to 99:59 (steady colon)
Hours and minutes from 1:40 to 99:59 (blinking colon)
// Bit maps for the seven-segment display
// Segment Cathodes (0/LOW for on, 1/HIGH for off)
const byte Segments[] =
{
0b11000000, // 0
0b11001111, // 1
0b10100100, // 2
0b10000110, // 3
0b10001011, // 4
0b10010010, // 5
0b10010000, // 6
0b11000111, // 7
0b10000000, // 8
0b10000011, // 9
};
const byte ColonLowerDot = 12;
const byte ColonUpperDot = 13;
// List of digit select lines, least significant digit first
// Digit Common Anodes (HIGH for on, LOW for off)
const byte DigitCount = 4;
const byte DigitPins[DigitCount] = {A0, 2, 3, 4};
// Segment Cathodes (LOW for on, HIGH for off)
const byte SegmentCount = 7;
const unsigned char SegmentPins[SegmentCount] = {5, 6, 7, 8, 9, 10, 11};
void setup()
{
for (int i = 0; i < DigitCount; i++)
{
pinMode(DigitPins[i], OUTPUT);
digitalWrite(DigitPins[i], LOW);
}
for (int i = 0; i < SegmentCount; i++)
{
pinMode(SegmentPins[i], OUTPUT);
digitalWrite(SegmentPins[i], HIGH);
}
digitalWrite(ColonLowerDot, HIGH);
digitalWrite(ColonUpperDot, HIGH);
pinMode(ColonLowerDot, OUTPUT);
pinMode(ColonUpperDot, OUTPUT);
}
void loop()
{
// Get time since last reset
unsigned long hundredths = millis() / 10;
unsigned long seconds = hundredths / 100;
unsigned long minutes = seconds / 60;
int hours = minutes / 60;
int clock;
if (seconds < 100)
// Display seconds.hundredths up to 100 seconds, then minutes:seconds
clock = (seconds % 100) * 100 + (hundredths % 100);
else if (minutes < 100)
// Display minutes:seconds up to 100 minutes, then hours/minutes
clock = (minutes % 100) * 100 + (seconds % 60);
else
clock = (hours % 100) * 100 + (minutes % 60);
// Clear all segments before enabling a digit
for (int s = 0; s < SegmentCount; s++)
{
digitalWrite(SegmentPins[s], HIGH);
}
// Display each digit, right to left
for (int i = 0; i < DigitCount; i++)
{
// Peel a digit off the low end of the number
int digit = clock % 10;
clock /= 10;
// Blank the MSD if it is zero
if (i == 3 && digit == 0)
{
for (int s = 0; s < SegmentCount; s++)
digitalWrite(SegmentPins[s], HIGH);
}
else
{
// Display the digit on the seven segments
unsigned char segments = Segments[digit];
for (int s = 0; s < SegmentCount; s++)
{
digitalWrite(SegmentPins[s], segments & 1);
segments >>= 1;
}
}
if (seconds < 100)
{
// Steady decimal point when showing seconds and hundredths
digitalWrite(ColonLowerDot, HIGH);
digitalWrite(ColonUpperDot, LOW);
}
else if (minutes < 100)
{
// Steady colon when showing minutes and seconds
digitalWrite(ColonLowerDot, LOW);
digitalWrite(ColonUpperDot, LOW);
}
else
{
// Make the colon blink each second
digitalWrite(ColonLowerDot, seconds & 1);
digitalWrite(ColonUpperDot, seconds & 1);
}
// Turn on the digit briefly
digitalWrite(DigitPins[i], HIGH); // Select one digit
delayMicroseconds(3000UL); // Higher numbers give higher brightness but more flicker
digitalWrite(DigitPins[i], LOW);
}
}
So you are actually able to control the two colon points separately on your display?
I am not in a position to check this on mine at present - it will require some excavation ... ![]()
Not on my (very common) "DiyMore" displays... one bit controls both colon dots.
My highest time my timer should countdown from is 99:59 (mm:ss). When under 1 min, it should go from 59.9 to 00.0.
How would you implement that? You have John Wasser's code to study and modify. It shouldn't be a mystery at this point...