1

I used sevseg.h library to write numbers to my 4 digit seven segment. The display should update every second with the current time since program started running. This is my code so far:

#include <SevSeg.h>

SevSeg sevseg;

void setup() {
    byte numDigits = 4;
    byte digitPins[] = {5, 4, 3, 2};
    byte segmentPins[] = {13, 12, 11, 10, 9, 8, 7, 6};
    sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
    sevseg.setBrightness(50);
    sevseg.setNumber(0000, -1);
}

void loop() {
    unsigned long timer = millis();
    for (int i = 0; i < 500; i++) {
        sevseg.setNumber(timer/1000, -1);
        sevseg.refreshDisplay();
        delay(2);
    }
}

Since the delay is 2, the for loop loops 500 times, resulting in 1000ms or 1 second. The current second is written to the display. For some reason, the display is updated every other 5 or 6 seconds. Why is this occurring? How can this be fixed so that my display turns into a stopwatch in a way?

2

This is the code that fixed the issue:

  #include <SevSeg.h>
  SevSeg sevseg;

  void setup() {
    byte numDigits = 4;
    byte digitPins[] = {5, 4, 3, 2};
    byte segmentPins[] = {13, 12, 11, 10, 9, 8, 7, 6};
    sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
    sevseg.setBrightness(50);
    sevseg.setNumber(0000, -1);
  }

  void loop() {
    unsigned long timerGlobal = millis();
    sevseg.setNumber(timerGlobal/1000, -1);
    sevseg.refreshDisplay();
  }

In this, no delays are needed. millis is divided by 1000 and the integer second is drawn on the display. Proved to be much easier than I previously thought.

-1

On SevSeg.h Arduino Page, It says:

Your program must run the refreshDisplay() function repeatedly to display the number.

I think that's the Problem. You are calling it just once and then your the number is changed.

Plus, you are saying it must get updated after One Second, but I think in your code, its being updated ever other 2 milisecond (WOW).

What I suggest is that you add a delay() for one second and then call sevseg.refreshDisplay(); several times in a for loop. You also need to change the way you measure no. of seconds elapsed.

i.e. try Something like this:

#include <SevSeg.h>
SevSeg sevseg;
void setup() {
  byte numDigits = 4;
  byte digitPins[] = {5, 4, 3, 2};
  byte segmentPins[] = {13, 12, 11, 10, 9, 8, 7, 6};
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins);
  sevseg.setBrightness(50);
  sevseg.setNumber(0000, -1);
}
unsigned long timerGlobal = millis();
void loop() {
  delay(1000);
  sevseg.setNumber( (timerGlobal - millis())/1000, -1)
  for (int i = 0; i < 50; i++) {
    sevseg.refreshDisplay();
  }
}
  • 1
    If my thinking is correct, the value of timer changes only when the void loop starts again, so even though the number gets set multiple times, it stays the same number. The refreshDisplay is also called 500 times a second since timer stays the same for a second. Adding large delays like 1000 doesn't work since it interrupts the constant refreshing of display. After running your code, I got - - - - showing on the display. After changing some code, the screen stays off for 1000ms and only stays on for a few ms. – Nick Solonko Apr 2 '16 at 18:23
  • well, I might be wrong since I haven't tested my code on Hardware. But, I think i would work. Can you try changing that 50 to some large number in for loop. That might help. Anyways. Sorry. – ARK Apr 2 '16 at 18:28
  • 1
    Using a delay is a terrible suggestion. From the SevSeg documentation: “any delays introduced by other functions will produce undesirable effects on the display”, which is quite obvious if you think about it. – Edgar Bonet Apr 28 '17 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.