Displaying bigger numbers on a 4 digit 7-segment?

I'm making a simple counter using a 4-digit 7-segment display, but I don't know how a could display a number bigger than 9, or bigger than 99, with this display.

Basically, each time the pushbutton is pressed, 1 will be added to to the "buttonvalue" variable (I have not yet made this part of the code, since that's not what I need help for.

The way my "printn" function works is that you tell it the digit on which you want to display a number, the the said number. but obviously, a number like 10, 36 (or higher) can't be displayed.

I guess I could split the variable into units, tens and hundreds then display them separatly, but I don't know if it's possible, or if it's even a good way of doing it.

Oh and here's the unfinished code:

int pb = 2; //general variables

int d1 = 10;
int d2 = 11;
int d3 = 12;
int d4 = 13;

int seg1 = 3;
int seg2 = 4;
int seg3 = 5;
int seg4 = 6;
int seg5 = 7;
int seg6 = 8;
int seg7 = 9;

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

  pinMode(pb, INPUT_PULLUP);

  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);

  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);

  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);
}

void loop() {
  int pbstate = digitalRead(pb); //this is just for another part of my project, dont worry about it
  printn(d1, 19);
}


void printn(int digit, int number) { //here is the "print" function
  int x;
  int i;
  int j;

  int n0[] = {1, 1, 1, 1, 1, 1, 0}; //i used arrays to "save" each number
  int n1[] = {0, 1, 1, 0, 0, 0, 0};
  int n2[] = {1, 1, 0, 1, 1, 0, 1};
  int n3[] = {1, 1, 1, 1, 0, 0, 1};
  int n4[] = {0, 1, 1, 0, 0, 1, 1};
  int n5[] = {1, 0, 1, 1, 0, 1, 1};
  int n6[] = {1, 0, 1, 1, 1, 1, 1};
  int n7[] = {1, 1, 1, 0, 0, 0, 0};
  int n8[] = {1, 1, 1, 1, 1, 1, 1};
  int n9[] = {1, 1, 1, 1, 0, 1, 1};

  if (number > 9) {
    number = number - 10;
    printn(d2, 1);
  }


  Serial.println(number);

  digitalWrite(digit, HIGH);

  for (i = 3; i < 10; i++) { //i used for loops to make the code more compact instead of having like 47 "digitalWrite"s
    digitalWrite(i, 0);
  }

  for (i = 3, j = 0; i < 10, j < 7; i++, j++) {
    if (number == 0) {
      x = n0[j];
    } else if (number == 1) {
      x = n1[j];
    } else if (number == 2) {
      x = n2[j];
    } else if (number == 3) {
      x = n3[j];
    } else if (number == 4) {
      x = n4[j];
    } else if (number == 5) {
      x = n5[j];
    } else if (number == 6) {
      x = n6[j];
    } else if (number == 7) {
      x = n7[j];
    } else if (number == 8) {
      x = n8[j];
    } else if (number == 9) {
      x = n9[j];
    }

    digitalWrite(i, x);
    digitalWrite(digit, LOW);
    delayMicroseconds(800UL);
    digitalWrite(digit, HIGH);
  }
}

That is the usual method.

Alright, i'll try that.

That’s the way to do it

Here is an example I posted yesterday that shows the basics:

////////////////////////////////////
// Example of displaying a number on a
//
// Four Digit Seven Segment Display
// (Common Anode)
//
// Written by John Wasser, July 16th, 2022
////////////////////////////////////

int NumberToDisplay = 8901;

// List of digit select lines, least significant digit first
// Digit Common Anodes (HIGH for on, LOW for off)
const byte DigitCount = 4;
const byte DigitPins[DigitCount] = {A0, 2, 3, 4};

// Segment Cathodes (LOW for on, HIGH for off)
// If current dropping resistors are used, they go on these pins.
const byte SegmentCount = 7;
const unsigned char SegmentPins[SegmentCount] = {5, 6, 7, 8, 9, 10, 11};

// Segment Cathodes (0/LOW for on, 1/HIGH for off)
const byte Segments[] =
{
  0b11000000, // 0
  0b11001111, // 1
  0b10100100, // 2
  0b10000110, // 3
  0b10001011, // 4
  0b10010010, // 5
  0b10010000, // 6
  0b11000111, // 7
  0b10000000, // 8
  0b10000011, // 9
};


void setup()
{
  for (int i = 0; i < DigitCount; i++)
  {
    pinMode(DigitPins[i], OUTPUT);
    digitalWrite(DigitPins[i], LOW);
  }

  for (int i = 0; i < SegmentCount; i++)
  {
    pinMode(SegmentPins[i], OUTPUT);
    digitalWrite(SegmentPins[i], HIGH);
  }
}

void loop()
{
  int number = NumberToDisplay;

  // Display each digit, right to left
  for (int i = 0; i < DigitCount; i++)
  {
    // Peel a digit off the low end of the number
    int digit = number % 10;
    number /= 10;

    // Display the digit on the seven segments
    unsigned char segments = Segments[digit];
    for (int s = 0; s < SegmentCount; s++)
    {
      digitalWrite(SegmentPins[s], segments & 1);
      segments >>= 1;
    }

    // Turn on the digit briefly
    digitalWrite(DigitPins[i], HIGH);  // Select one digit
    delayMicroseconds(3000UL);  // Higher numbers give higher brightness but more flicker
    digitalWrite(DigitPins[i], LOW);
  }  // End of for(i
}
1 Like

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