Timer for Arduino

I am trying to make a timer that goes from tenths milliseconds up to 60 seconds. Here is the code i have so far.

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

#include <SPI.h>
byte hundredths;
byte tenths;
byte secondsOnes = 0;
byte secondsTens = 0;
byte minutesOnes = 0;
byte minutesTens = 0;
byte hoursOnes= 0;
byte hoursTens = 1;
byte days;
byte years;
byte oldhundredths;





byte fontArray[] = {
0b00111111, // 0  1 = segment on, DP-g-f-e-d-c-b-a
0b00000110, // 1             
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111100, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9      
0b00000000, // blank  
0b10000000, // DP
};
//    a
// f     b
//    g
// e     c
//    d     DP

void setup(){

Serial.begin(115200); // make serial monitor match
Serial.println ("Setup Done");                                    // " was missing
}

void loop(){

currentMicros = micros();


elapsedTime = currentMicros - previousMicros;
if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
previousMicros  = previousMicros + 10000UL;
hundredths = hundredths+1;
if (hundredths == 10){
    hundredths = 0;
    tenths = tenths +1;
    if (tenths == 10){
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes == 10){
            secondsOnes = 0;
            secondsTens = secondsTens +1;
            if (secondsTens == 6){ 
                secondsTens = 0;
                minutesOnes =  minutesOnes + 1;
                if (minutesOnes == 10){
                    minutesOnes = 0;
                    minutesTens = minutesTens +1;
                    if (minutesTens == 6){
                        minutesTens = 0;
                        hoursOnes = hoursOnes + 1;
                          if (hoursOnes == 10){
                              hoursOnes = 0;
                              hoursTens = hoursTens +1;
                                if ( (hoursTens == 2) && (hoursOnes == 4)){
                                      hoursOnes = 0;
                                      hoursTens = 0;
                                      if (days>1){
                                          days = days-1;
                                           }
                                      else {
                                         days = 365;
                                         years = years - 1;
                                         }
                                    
                                      } // 24 hr rollover check
                                   } //hoursTens rollover check
                              } // hoursOnes rollover check
                        } // minutesTens rollover check
                     } // minutesOnes rollover check
                 } // secondsTens rollover check
              } // secondsOnes rollover check
          } // tenths rollover check
       } // hundredths rollover check
 // hundredths passing check

if (oldhundredths != hundredths){  // show the elapsed time once a second 
oldhundredths = hundredths;
}
PORTF = fontArray[hundredths];
PORTG = fontArray[tenths];
PORTH = fontArray[secondsOnes];
PORTK = fontArray[secondsTens];

digitalWrite (latchPin, LOW);
SPI.transfer(fontArray[digits[0]]); // or shiftOut(dataPin, clockPin, MSBFIRST, fontArray[digits[0]]); 
SPI.transfer(fontArray[digits[1]]);
SPI.transfer(fontArray[digits[2]]);
SPI.transfer(fontArray[digits[3]]);
digitalWrite (latchPin, HIGH);

} // end one second check

I cant figure out SPI.transfers and need help to complete the code, thank you

previousMicros  = previousMicros + 10000UL;

Adding time is a bad idea.

I cant figure out SPI.transfers and need help to complete the code, thank you

What are you trying to transfer data to?

Move this to the top of the sketch
#include <SPI.h>

Otherwise, what’s to figure out? The code (that I wrote for another poster) is already there for you, just change the names:

digitalWrite (latchPin, LOW);
SPI.transfer(fontArray[digits[hundreths]]); 
SPI.transfer(fontArray[digits[tenths]]);
SPI.transfer(fontArray[digits[secondsOnes]]);
SPI.transfer(fontArray[digits[secondsTens]]);
// etc, add minutes, hours, days years, as needed
digitalWrite (latchPin, HIGH);

PaulS: previousMicros  = previousMicros + 10000UL;

Adding time is a bad idea.

In this case it is perfectly fine to add. Using addition to predict a future time is bad, but so long as the intervals are calculated via subtraction, updating the previous time with addition serves a useful purpose that can't be accomplished without it.

There is no problem with this in a rollover situation because you're still guaranteed that previous cant get ahead of current time.

CrossRoads:
Move this to the top of the sketch
#include <SPI.h>

Otherwise, what’s to figure out? The code (that I wrote for another poster) is already there for you, just change the names:

digitalWrite (latchPin, LOW);

SPI.transfer(fontArray[digits[hundreths]]);
SPI.transfer(fontArray[digits[tenths]]);
SPI.transfer(fontArray[digits[secondsOnes]]);
SPI.transfer(fontArray[digits[secondsTens]]);
// etc, add minutes, hours, days years, as needed
digitalWrite (latchPin, HIGH);

The thing that i was confused about is what do I use to define latchPin, so that the 4 leds correspond to the rest of the code

also digits is also not declared

You have very little Arduino experience I take it? Try this. I can’t compile from here, but I think it’s correct.

#include <SPI.h>
// shift register connections:
byte latchPin = 10; // to RCLK
// MOSI from 11 to Serial Data in. Daisy chain Serial Data out (Q7/) to next part
// SCK from13 to SRCLK, to all parts
// OE on shift register to GND on all parts
// SRCLR to +5 on all parts
// 0.1uF cap from Vcc pin to Gnd on all parts.

unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedTime;

byte hundredths;
byte oldhundredths;
byte tenths;
byte oldtenths;
byte secondsOnes = 0;
byte oldsecondsOnes;
byte secondsTens = 0;
byte minutesOnes = 0;
byte minutesTens = 0;
byte hoursOnes= 0;
byte hoursTens = 0;
/ /byte days;
// byte years;

byte fontArray[] = {
0b00111111, // 0  1 = segment on, DP-g-f-e-d-c-b-a
0b00000110, // 1             
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111100, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9      
0b00000000, // blank  
0b10000000, // DP
};
//    a
// f      b
//    g
// e     c
//    d     DP

void setup(){
pinMode (latchPin, OUTPUT);
Serial.begin(115200); // make serial monitor match
Serial.println ("Setup Done");  
}

void loop(){

currentMicros = micros();

elapsedTime = currentMicros - previousMicros;
if ( elapsedTime >=10000UL){  // 0.01 second passed? Update the timers
previousMicros  = previousMicros + 10000UL;
hundredths = hundredths+1;
if (hundredths == 10){
    hundredths = 0;
    tenths = tenths +1;
    if (tenths == 10){
        tenths = 0;
        secondsOnes = secondsOnes + 1;
        if (secondsOnes == 10){
            secondsOnes = 0;
            secondsTens = secondsTens +1;
            if (secondsTens == 6){ 
                secondsTens = 0;
                minutesOnes =  minutesOnes + 1;
                if (minutesOnes == 10){
                    minutesOnes = 0;
                    minutesTens = minutesTens +1;
                    if (minutesTens == 6){
                        minutesTens = 0;
                        hoursOnes = hoursOnes + 1;
                          if (hoursOnes == 10){
                              hoursOnes = 0;
                              hoursTens = hoursTens +1;
                                if ( (hoursTens == 2) && (hoursOnes == 4)){
                                      hoursOnes = 0;
                                      hoursTens = 0;

                                      // days, years commented out
                                      //if (days>1){
                                      //     days = days-1;
                                      //     }
                                      //else {
                                      //   days = 365;
                                      //   years = years - 1;
                                      //   }
                                    
                                      } // 24 hr rollover check
                                   } //hoursTens rollover check
                              } // hoursOnes rollover check
                        } // minutesTens rollover check
                     } // minutesOnes rollover check
                 } // secondsTens rollover check
              } // secondsOnes rollover check
          } // tenths rollover check
       } // hundredths rollover check
 // hundredths passing check

// I would personally only update using tenths and oldtenths, or secondsOnes and oldsecondsOnes. I think this will just show a blur for the hundreths digit.
if (oldhundredths != hundredths){  // show the elapsed time 
oldhundredths = hundredths;
}

// comment out unwanted digits
digitalWrite (latchPin, LOW);
SPI.transfer(fontArray[hundresths]); 
SPI.transfer(fontArray[tenths]);
SPI.transfer(fontArray[secondsOnes]);
SPI.transfer(fontArray[secondsTens]);
SPI.transfer(fontArray[minutesOnes]);
SPI.transfer(fontArray[minutesTens]);
SPI.transfer(fontArray[hoursOnes]);
SPI.transfer(fontArray[hoursTens]);
digitalWrite (latchPin, HIGH);

} // end one second check
} // end loop