Adding thousandths of s second to countdown on 7 segment display with arduino.

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
Hi, im making a countdown timer with a max 7219 4 digit 7 segment display and arduino Uno. I want to add thousandths of a second to the 8 second timer. I am unsure on how to add more digits, i know I need to change the countdown from intervals of 1000ms to 1ms but thats all I know, if someone could help that would be great! Thanks!

Code:
#include "LedControl.h"
const int buttonPin = 2;
int buttonState = 0;


LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=1000;

void setup() {
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(2, INPUT);


  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
}


void CountDown() {
  digitalWrite(5, HIGH);
  for(int i=8;i>0;i--) {
    lc.setDigit(0,0,i,false);
    delay(delaytime);
      
 
  }
  lc.clearDisplay(0);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(delaytime);
 
}

void loop() {
  buttonState = digitalRead(2);
  if (buttonState == HIGH){
    digitalWrite(6, LOW);
    CountDown();
  }
  }
 

jpanhalt

Joined Jan 18, 2008
11,087
That display driver has a nominal scan rate of 800 Hz (500 to 1300 Hz range) and a 2.25 ms delay from receipt of data to display. For a 1 ms resolution you will be pushing those limits. You might consider displaying every 10 ms or 100 ms and then milliseconds only when stopped.
 

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
That display driver has a nominal scan rate of 800 Hz (500 to 1300 Hz range) and a 2.25 ms delay from receipt of data to display. For a 1 ms resolution you will be pushing those limits. You might consider displaying every 10 ms or 100 ms and then milliseconds only when stopped.
I’d be fine with that, the main problem is I can’t get it to count down from 8000 at all, when I change my 8 to 8000 it stops displaying anything...
 

jpanhalt

Joined Jan 18, 2008
11,087
Sorry, but I cannot do C.

From what little I can understand of C, it appears your code counts seconds, changing from the '8' to 8000 will count down from 8000 seconds. In Assembly, you would need to change the time between counts from 1 second to 1 ms. Is that "delay"?
 

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
Sorry, but I cannot do C.

From what little I can understand of C, it appears your code counts seconds, changing from the '8' to 8000 will count down from 8000 seconds. In Assembly, you would need to change the time between counts from 1 second to 1 ms. Is that "delay"?
Yes, that’s about correct. The 8 I’m using, is just that. 8. It way be part of the problem but I’m not sure how to make a count down that tells the arduino it’s displaying seconds.
 

Ian Rogers

Joined Dec 12, 2012
1,136
Your count should be done in an interrupt, the display can then be done in the main loop.

The function to display 4 digits will be longer than you require... The Ic.setdigit() does just that.... Sets one digit.. I do not have a copy of the led driver to hand, but there will be a function to set all digits..

You need to pop over to.. http://playground.arduino.cc/Main/LedControl This is where your code came from... Towards the bottom it shows how to place multiple digits..
 

Norfindel

Joined Mar 6, 2008
326
Take a look at the millis() function. You will only need to be aware that:

Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.
 

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
Take a look at the millis() function. You will only need to be aware that:
I’m not sure if that would work for me, I am counting down. If you could help me with the code to just start the millis and rested it at the right time, then I could use it, but it doesn’t have to be the most accurate timer.
 

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
Your count should be done in an interrupt, the display can then be done in the main loop.

The function to display 4 digits will be longer than you require... The Ic.setdigit() does just that.... Sets one digit.. I do not have a copy of the led driver to hand, but there will be a function to set all digits..

You need to pop over to.. http://playground.arduino.cc/Main/LedControl This is where your code came from... Towards the bottom it shows how to place multiple digits..
I’ve been looking at that example, and I can’t quite figure it out... it seems like there should be a function to just print multiple digits and not each one individually. I guess I could take a countdown in software then extract each digit and print it to the display... but I can’t figure out how to take a specific digit, any help on that would be appreciated! Thanks!
 

Norfindel

Joined Mar 6, 2008
326
I’m not sure if that would work for me, I am counting down. If you could help me with the code to just start the millis and rested it at the right time, then I could use it, but it doesn’t have to be the most accurate timer.
Well, you have the amount of milliseconds since power-on, so:

At the moment you need to start the countdown, you do something like: startMillis = millis();
Then, when you need to display the countdown, you calculate: elapsedMillis = millis() - startMillis
And your countdown is: 8000 - elapsedMillis

But the catch is that millis() will overflow and return to 0 when it reaches the maximum count after roughly 50 days turned on. So, you need to detect that in your code, and calculate the correct elapsedmillis when that happens.
 

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
Well, you have the amount of milliseconds since power-on, so:

At the moment you need to start the countdown, you do something like: startmillis = millis();
Then, when you need to display the countdown, you calculate: elapsedmillis = millis() - startmillis
And your countdown is: 8000 - elapsedmillis

But the catch is that millis() will overflow and return to 0 when it reaches the maximum count after roughly 50 days turned on. So, you need to detect that in your code, and calculate the correct elapsedmillis when that happens.
Thanks! I won’t add the 50 day protection because I don’t intend to have to run it for more than an hour.
 

Ian Rogers

Joined Dec 12, 2012
1,136
I think it works, but I still cant figure out how to get all 4 digits.
Just copy the code..
C:
void printNumber(int v) {
int ones;
int tens;
int hundreds;
int thousands;
if(v<0) return;

ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v%10;
v=v/10;
thousands=v; 

//Now print the number digit by digit
lc.setDigit(0,3,(byte)thousands,false);
lc.setDigit(0,2,(byte)hundreds,false);
lc.setDigit(0,1,(byte)tens,false);
lc.setDigit(0,0,(byte)ones,false);
}
Then call printNumb from your loop.
 

Thread Starter

Julian Barbera

Joined Dec 31, 2018
8
I changed some of it around because it was a bit off, at least on my display. so I ended up with:
Code:
hundreds=v%10;
thousands=v%10;
v=v/10;
tens=v%10;
v=v/10;
ones=v%10;
thousands=v;
now the hundredths is just a blinking 8 and thousandths is just a zero. I guess that the 8 is just because it cant update the display fast enough and the zero is just there for the same reason. When I take a slo mo video of it I can see all the numbers flashing! So it works now! Unless anyone has a trick to make my eyes see faster (;
 

Norfindel

Joined Mar 6, 2008
326
That's why digital meters have limited update speed, otherwise it's impossible to read the numbers. Too fast to read the measurement.
 
Top