Coding a Timer, with 4 digit 7 segement display AND 74HC595

All that over-complex code you copied but don't understand seems to be back again. Oh well, I'm sure the others will help you get to a solution. Good luck.

Thank you. I hadn’t noticed the resistor value was so large. With 2k2 resistors the current is comfortably less than 20mA.

@1notsure_2505

Please, read and follow the given steps to understand how to program a 4-digit cc7-Segment multiplexed display unit using 74HC595 shift register.

1. Study the schematic (tested, Fig-1) to see how signal flows from Arduino to 7-segment display Unit.


Figure-1:

2. Let us see how we can show 1 at DP0 position of the display unit of Fig-1.
(1) Get the cc-code of 1 as 0x06 (0 0 0 0 0 1 1 0 = DP G F E D C B A)
(2) Send the bit pattern of Step-(1) onto the Shift Register of 74HC595. It can be done easily by executing the following codes: (The alternate method is bitbang.)

SPI.transfer(0x06);  //MSBit is transmitted first.

(3) Transfer the above content of Shift Regsiter into the Latch Register of 74HC595 by executing the following code that generates a rising edge of a clock pulse.

digitalWrite(ST_CP, HIGH);
digitalWrite(ST_CP, LOW);

(4) Assert LOW on A0-pin and HIGH on A1 to A3 pins.
(5) Check that 1 has appeared on DP0 position of the display ubit.
(6) The complete tested sketch is given belwo which can be uoloaded into UNO.

#include<SPI.h>
#define START 2
#define STOP 3
#define ST_CP 4

byte lupTable[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};//0,1,2,3,4,5,6,7,8,9

void setup()
{
  SPI.begin();
  pinMode(START, INPUT_PULLUP);
  pinMode(ST_CP, OUTPUT);
  digitalWrite(ST_CP, LOW);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  //--------------------------
  SPI.transfer(lupTable[1]); //06 = 0 0 0 0 0 1 1 0
  digitalWrite(ST_CP, HIGH);
  digitalWrite(ST_CP, LOW);
  //----------------------------
  digitalWrite(A0, LOW);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
 
}

void loop()
{

}

3. hGiven below a tested sketch to show 15.00 on the diplay unit.

#include<SPI.h>
#define START 2
#define STOP 3
#define ST_CP 4

byte lupTable[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};//0,1,2,3,4,5,6,7,8,9

void setup()
{
  SPI.begin();
  pinMode(START, INPUT_PULLUP);
  pinMode(ST_CP, OUTPUT);
  digitalWrite(ST_CP, LOW);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  //--------------------------
  SPI.transfer(lupTable[1]); //06 = 0000 0110
  digitalWrite(ST_CP, HIGH);
  digitalWrite(ST_CP, LOW);
  //----------------------------
  digitalWrite(A0, LOW);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  //------------------------
}

void loop()
{
  SPI.transfer(lupTable[1]); //1; 06 = 0000 0110
  digitalWrite(ST_CP, HIGH);
  digitalWrite(ST_CP, LOW);
  //-------
  digitalWrite(A0, LOW);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  delay(1);
  //------------------------
  SPI.transfer(lupTable[5] | 0x80); //5.
  digitalWrite(ST_CP, HIGH);
  digitalWrite(ST_CP, LOW);
  //------
  digitalWrite(A0, HIGH);
  digitalWrite(A1, LOW);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, HIGH);
  delay(1);
  //------------------------
  SPI.transfer(lupTable[0]); //0
  digitalWrite(ST_CP, HIGH);
  digitalWrite(ST_CP, LOW);
  //--
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, LOW);
  digitalWrite(A3, HIGH);
  delay(1);
  //------------------------
  SPI.transfer(lupTable[0]); //0
  digitalWrite(ST_CP, HIGH);
  digitalWrite(ST_CP, LOW);
  //------
  digitalWrite(A0, HIGH);
  digitalWrite(A1, HIGH);
  digitalWrite(A2, HIGH);
  digitalWrite(A3, LOW);
  delay(1);
  //------------------------
}

4. Re-write the sketch of Step-3 using for() loop to reduce the number of code lines in the loop() function.
Hints:

byte lupTable[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};//0,1,2,3,4,5,6,7,8,9
byte ccTable[] = {0x06, 0x6D, 0x3F, 0x3F}; //cc-codes for: 1 5 0 0

void loop()
{
  for (int i = 0; i < 4; i++)
  {
    byte y = 0xFF;
    if (i == 1)
    {
      SPI.transfer(ccTable[i] | 0x80); //5.
    }
    else
    {
      SPI.transfer(ccTable[i]);
    }
    digitalWrite(ST_CP, HIGH); //CLK signal generation
    digitalWrite(ST_CP, LOW);
    //-------
    bitClear(y, i);
    PORTC = y;
    delay(1);
    //------------------------
  }
}

5. Create sketch so that the display shows 15.00 at powerup. After that when the START button is pressed, the display keeps counting down by 1-sec.

#include<SPI.h>
#define START 2
#define STOP  3
#define ST_CP 4

byte lupTable[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};//0,1,2,3,4,5,6,7,8,9
byte ccTable[4];  //holds cc-code of SecRD, SecLD; MinLD, MinRD
byte decSec = 00;
byte decMin = 00;


void setup()
{
  SPI.begin();
  pinMode(START, INPUT_PULLUP);
  pinMode(ST_CP, OUTPUT);
  digitalWrite(ST_CP, LOW);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  //--------------------------
  decSec = 00;
  decMin = 15;   //15.
  //-------------------------
  ccTable[3] = lupTable[decSec % 10];
  ccTable[2] = lupTable[decSec / 10];
  //---------------------------------
  ccTable[1] = lupTable[decMin % 10];
  ccTable[0] = lupTable[decMin / 10];
  //---------------------------------
  refreshDisplay();

  while (digitalRead(START) != LOW)
  {
    refreshDisplay();
  }
}

void loop()
{
  unsigned long prMillis = millis();
  while (millis() - prMillis < 1000)
  {
    ccTable[3] = lupTable[decSec % 10]; //get ihtside digit of Sec
    ccTable[2] = lupTable[decSec / 10]; //get leftside digit of Sec
    //---------------------------------
    ccTable[1] = lupTable[decMin % 10]; //get rightside digit of Min
    ccTable[0] = lupTable[decMin / 10]; //get leftside digi of Min
    //---------------------------------
    refreshDisplay();                     //refresh display until 1sec has elapsed
  }
  if (decSec == 00)
  {
    if (decMin != 0)
    {
      decSec = 59;
      decMin = decMin - 1;
      if (decSec == 00)
      {
        if (decMin == 00)
        {
          while (true);
        }
      }
    }
  }
  else
  {
    decSec--;
  }
}

void refreshDisplay()
{
  for (int i = 0; i < 4; i++)
  {
    byte y = 0xFF;
    if (i == 1)  //check if showing digit at DP1 position in order to place point
    {
      SPI.transfer(ccTable[i] | 0x80); //place point with rightside digit of Min
    }
    else
    {
      SPI.transfer(ccTable[i]);
    }
    digitalWrite(ST_CP, HIGH); //CLK signal generation
    digitalWrite(ST_CP, LOW);
    //-------
    bitClear(y, i);
    PORTC = y;
    delay(1);
    //------------------------
  }
}

6. Add STOP Button with Fig-1 of Step-1. Now, add codes with sketch of Step-5 so that when the STOP Button is pressed, the countig down process is frozen.

yep, its back because its the only way i able to get the digits to light up...
i am grateful to receive help and feed back, including "change your schematic completely to something else and use this code @GolamMostafa " or " it appears you have not reached the level of knowledge required for me to assist you @PaulRB"
I don't have extensive experience using Arduino, or coding. I am simply very curious to learn and tinker with hobby projects.
all this overly complex code = elegoo starter kit; tutorial online identifies his code as 'simple' yet i am completely confounded when trying to apply this to my current project.
@PaulRB you were very helpful guiding me in the right direction in your first reply, about getting a code for a timer to display in the serial monitor. which i was able to accomplish, but when actually assisting in transferring that data to a display... your response makes me question your

include "Karma; 2000+" // Did i get that right :=

here's code that implements a countdown timer using hardware on the multi-function shield which used a shift register for both segment and digit I/O. It wouldn't be difficult to modify the code to drive the digit pins using Arduino pins

// -----------------------------------------------------------------------------
// drive 7-seg displays on MultiFuction board

//  constants for multiplexed 7-segment display
#define Latch   4
#define Clock   7
#define Data    8
#define LED0   10
#define LED1   11

byte pins [] = { Latch, Clock, Data, LED0, LED1 };

//    a      d
//   f b    c e
//    g      g
//   e c    b f
//    d h    a
//
//   h g f e d c b a

const byte SEGMENT_MAP_DIGIT[] = {
    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0X80, 0X90
};

const byte SEGMENT_OFF = 0xFF;

const byte DISP_MAP [] = { 1, 2, 4, 8 };
#define N_DISP  sizeof(DISP_MAP)

byte disp [N_DISP] = {SEGMENT_OFF, SEGMENT_OFF, SEGMENT_OFF, SEGMENT_OFF};

// -------------------------------------
void
seg7update ()
{
    static byte idx = 0;
    uint16_t val = (disp [idx] << 8) + DISP_MAP [idx];

    digitalWrite (Latch, LOW);
    shiftOut (Data, Clock, MSBFIRST, val >> 8);
    shiftOut (Data, Clock, MSBFIRST, val);
    digitalWrite (Latch, HIGH);

    if (N_DISP <= ++idx)
        idx = 0;
}

// -------------------------------------
// update the value of each digit
void seg7disp (
    int  val )
{
    Serial.println (__func__);

    int i;
    for (i = N_DISP-1; i >= 0; i--, val /= 10)
        disp [i] = SEGMENT_MAP_DIGIT [val % 10];

    // blank leading zeros
    i = 0;
    while (SEGMENT_MAP_DIGIT [0] == disp [i])
        disp [i++] = SEGMENT_OFF;
}

// -------------------------------------
void seg7init (void)
{
    Serial.println (__func__);
    for (unsigned i = 0; i < sizeof(pins); i++)  {
        digitalWrite (pins [i], HIGH);
        pinMode      (pins [i], OUTPUT);
    }
}

// -----------------------------------------------------------------------------
int mins;
int secs;
int val;
int cntTic;

const unsigned long MsecPeriod = 5;;
      unsigned long msecLst;

// -------------------------------------
void loop (void)
{
    unsigned long msec = millis ();
    if (msec - msecLst >= MsecPeriod)  {
        msecLst += MsecPeriod;

        if (20 == ++cntTic)  {      // should be 200 = 1000 / MsecPeriod
            cntTic = 0;

            if (0 < mins || 0 < secs)  {
                if (0 == secs--)  {
                    secs = 59;
                    -- mins;
                }
                val = 100 * mins + secs;
            }
            seg7disp (val);
        }

        seg7update ();
    }

    if (Serial.available ())  {
        Serial.read ();     // ignore
        mins = 5;
    }
}

// -------------------------------------
void
setup (void)
{
    Serial.begin (9600);

    seg7init ();
}

You are a beginner, so I was trying to help you towards a solution one step at a time, so I could check at each stage that you understand why each step is important, and so that you understand the code because you are writing it yourself. If I presented you with an explanation of the entire solution, it would be overwhelming and therefore useless to you.

I suggested a next step in post #11. In post #18, you said you thought you understood this, but you did not attempt it, so I assume you didn't understand. Instead, all the code that I described as over-complex and a hindrance to you was suddenly back.

You defended bringing back the code I didn't want to you use because you say "its the only way". You think this because you are a beginner. As someone with more experience of coding, I can tell you there are always a hundred ways of writing the code to achieve any given function. Some ways are best for performance, some for efficiently, some for simplicity and some for understandability. Some ways are a careful balance between some or all of those goals. And most of the rest of the hundred ways are simply less good, even though they may achieve the desired function.

The code you copied was written by someone else for a different purpose. For your project, even if you could make it work, it would be in the "less good" category.

It has taken many years on the forum for that karma score to build up. In that time, I have learned a lot about the right and wrong ways to go about offering help to beginners, from my own success and failures and from observing the successes and failures of other senior forum members. I still don't get it right all the time. I'm sorry about that.

And I'm sorry that this reply has turned into a lecture.