4 digit 7segment coding problems

Im just starting out learning to code and using the arduino Uno.
Im at the stage of learning how to code the 4 digit 7 segment display. Although im running into 2 issues. The first: is with a tutorial piece of code, which dislpays 0,1,2,3 on the dislpay, but I have no idea why or how to change the displayed numbers.
Through this I went down the youtube and internet rabbit hole. eventually finding a video using the sevseg lib. however im getting a error thrown up when verify the code.
My goal: be able to display any 4 digit number

I have attached both codes below, any tips and advice would be greatly appreciated:

tutorial code:

/*
  Sketch 17.2.2
  4-digit 7-segment Display

  modified 2016/8/17
  by http://www.freenove.com
*/

#include <FlexiTimer2.h>  // Contains FlexiTimer2 Library

int latchPin = 12;          // Pin connected to ST_CP of 74HC595(Pin12)
int clockPin = 13;          // Pin connected to SH_CP of 74HC595(Pin11)
int dataPin = 11;           // Pin connected to DS of 74HC595(Pin14)
int comPin[] = {7, 6, 5, 4};// Common pin (anode) of 4 digit 7-segment display

int second = 0;             // Define variables stored record time

// Define the encoding of characters 0-F for the common-anode 7-Segment Display
byte num[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};

void setup() {
  // set pins to output
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  for (int i = 0; i < 4; i++) {
    pinMode(comPin[i], OUTPUT);
  }
  FlexiTimer2::set(1000, timerInt);  // configure the timer and interrupt function
  FlexiTimer2::start();              // start timer
}

void loop() {
  // Calculate the seconds of each digit number
  byte bit[4];
  bit[0] = second % 10;
  bit[1] = second / 10 % 10;
  bit[2] = second / 100 % 10;
  bit[3] = second / 1000 % 10;
  // Show seconds
  for (int i = 0; i < 4; i++) {
    // Select a single 7-segment display
    chooseCommon(i);
    // Send data to 74HC595
    writeData(num[bit[3 - i]]);
    delay(5);
    // Clear the display content
    writeData(0xff);
  }
}

// the timer interrupt function of FlexiTimer2 is executed every 1s
void timerInt() {
  second++;       // second plus 1
}

void chooseCommon(byte com) {
  // Close all single 7-segment display
  for (int i = 0; i < 4; i++) {
    digitalWrite(comPin[i], LOW);
  }
  // Open the selected single 7-segment display
  digitalWrite(comPin[com], HIGH);
}

void writeData(int value) {
  // Make latchPin output low level
  digitalWrite(latchPin, LOW);
  // Send serial data to 74HC595
  shiftOut(dataPin, clockPin, LSBFIRST, value);
  // Make latchPin output high level, then 74HC595 will update the data to parallel output
  digitalWrite(latchPin, HIGH);
}

sevseg code:



#include <SevSeg.h>
SevSeg sevseg;

void setup(){
  byte numbDigits = 4;
  byte digitalPins[] = {2, 3, 4, 5};
  byte segmentPins [] = {6, 7, 8, 9, 10, 11, 12, 13};
  bool resistorsOnSegments = 0;
  sevseg.begin(COMMON_CATHODE, numDigits, digitPins, segmentPins, resistorsOnSegment);
  sevSeg.setBrightness{90};
}

void loop(){
  sevseg.setNumber(8751, 3);
  sevseg.refreshDisplay();
}

From what I saw in the first code, it uses the IC 74LS595.

As it seems that the 7seg connections are different in the codes,

to make it easier for us to help, I am posting a diagram used for the 2 codes.

1 Like

presumably the 4digit value of seconds is displayed.

but a multiplex display needs to display one digit at a time, giving the human eye a chance to see the digit before displaying the next digit.

writeData() shifts the segment values out and chooseCommon()enables the digit, butloop()` is repeatedly calling them for all 4 digits without and pause.

you could try adding a delay of try 20 msec in the for loop in loop().

of course this is an awkward way of doing things. The code really needs to have a function to just display the next digit that can be call once at the apropriate inverval in loop() while it does other things

1 Like

This code does not compile correctly because it has several typing errors.

#include <SevSeg.h>
SevSeg sevseg;                                    // Instance  (sevseg) use lower case

void setup(){
  byte numbDigits = 4;                           // <<<<< letter b   may be numDigits
  byte digitalPins[] = {2, 3, 4, 5};
  byte segmentPins [] = {6, 7, 8, 9, 10, 11, 12, 13};
  bool resistorsOnSegments = 0;
  sevseg.begin(COMMON_CATHODE, numDigits, 
digitPins,                                      // Not digitPins  correct digitalPins
segmentPins, 
                        resistorsOnSegment);    // < Missing "s" at name end
  sevSeg.setBrightness{90};                    //   <<  ---- not {90}  correct (90)
//                                             // not S uppercas   correct  sevseg.
}

void loop(){
  sevseg.setNumber(8751, 3);
  sevseg.refreshDisplay();
}


1 Like

See this simulation:

I used different pins than yours but that's not important and I no have use resistors.
After all, it's just a simulator and the LEDs don't burn out.

Thank you, this has compiled now.

Ill rebuild the circuit and fingers crossed, we will have won. :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.