[Solved] Strobing to Light Four 7-Segment Displays; Not Working Properly

So I got this little 7-segment bubble display with four digits from Sparkfun. I also got one of their shift registers (74HC595N). I’ve had success in the past getting these to work together. But now I have an issue.

I’m trying to get a timer together using these. I coded a number, 300, into the scetch as four different values: a=0,b=3,c=0,d=0. After squashing some bugs, it works… except that when it reaches 0267, all digits except the last go blank (it remains on 8) and it stops counting. Thinkng it was the Arduino (Uno) going bad after all the abuse I sent it through, I uploaded the code to a new(er) Uno. It still acts the same. It seems as though it stops executing the code after so much time.

The only two conditions in the loop are

now=millis();
  
  while(millis() - now < 1000){

and

seconds = (a*1000)+(b*100)+(c*10)+d;
if(seconds>0){

Neither of those two were designed to stop execution of the code.

// define numbers
const int numbers[] = {0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b00111110,0b11100000,0b11111110,0b11100110}; // 0-9

// set the enable pin
const int en = 10; // set low when sending data

// these are the digits on the display
const int a1 = 2;
const int a2 = 3;
const int a3 = 4;
const int a4 = 5;

//data,clock
const int sda = 7;
const int sck = 8;

//set the digits
int a;
int b;
int c;
int d;

//We'll need these for timing
int now;
int seconds;

void setup(){
  pinMode(en,OUTPUT);
  pinMode(a1,OUTPUT);
  pinMode(a2,OUTPUT);
  pinMode(a3,OUTPUT);
  pinMode(a4,OUTPUT);
  pinMode(sda,OUTPUT);
  pinMode(sck,OUTPUT);
  digitalWrite(en,1);
  delay(10);
  digitalWrite(en,0);
  shiftOut(sda,sck,LSBFIRST,0);
  digitalWrite(en,1);
  
  a=0;
  b=3;
  c=0;
  d=0;
}

void loop(){
  now=millis();
  
  while(millis() - now < 1000){
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[a]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,0);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    delay(1);
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[b]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,1);digitalWrite(a2,0);digitalWrite(a3,1);digitalWrite(a4,1);
    delay(1);
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[c]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,0);digitalWrite(a4,1);
    delay(1);
    digitalWrite(en,0);
    shiftOut(sda,sck,LSBFIRST,numbers[d]);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,1);
    digitalWrite(en,1);
    digitalWrite(a1,1);digitalWrite(a2,1);digitalWrite(a3,1);digitalWrite(a4,0);
    delay(1);
  }
  
  // subtract a second
 seconds = (a*1000)+(b*100)+(c*10)+d;
if(seconds>0){
  seconds--;
  a=seconds/1000;
  b=seconds/100 - (a*10);
  c=seconds/10 - (a*100) - (b*10);
  d=seconds - (a*1000) - (b*100) - (c*10);
}
}

Hi free-bee

int now;

Try changing this variable to be unsigned long, to match the millis() function. Otherwise, it will overflow and go negative at values greater than 32767. This will happen roughly 33s after the program starts running, which is about 267s on the countdown.

unsigned long now;

Regards

Ray

I was going to say that I have no idea why that worked. And then I remembered, I'm dumb. I was thinking that with each loop, now was resetting to 0. Now I get to integrate a multiplexer, a keypad, and resistor array to make it adjustable.