Seven segment display is wrong

Hello, I'm just learning.
I have a little project, and having trouble
This is what I need to do

  • Display: Counts down from 15 to 0 on two 7-segment displays
  • LED-1: On by default, turns off when Button 1 is pressed
  • LED-2: Blinks during countdown, stays on after reaching 0
  • Button 1: Starts countdown
  • Button 2: Resets everything
  • No multiplexing: Each segment wired directly

I'm working with 2 seven segment displays common anode.
2 leds
These will be eventually replaced by relays to power a big display because of power requirements.

I cheated and used ChatGPT to work up a sketch.
The sketch works but displays the numbers like a predator gauntlet.
I cannot decipher in the code how the 7 segment displays are being illuminated.
Any help would be appreciated.

May have to hire someone for this.
code below
Thanks for looking

// Segment mapping for numbers 0-9 on common anode 7-segment display
const byte digits[10] = {
  B11000000, // 0
  B11111001, // 5
  B10100100, // 2
  B10110000, // 8
  B10011001, // 4
  B10010010, // 1
  B10000010, // 6
  B11111000, // 7
  B10000000, // 3
  B10010000  // 9
};

// Pins for segments a-g of both displays (no multiplexing)
const int segPinsTens[7] = {2, 3, 4, 5, 6, 7, 8};      // Tens display
const int segPinsOnes[7] = {9, 10, 11, 12, 13, A0, A1}; // Ones display

// Button and LED pins
const int buttonStart = A2;
const int buttonReset = A3;
const int led1 = A4;
const int led2 = A5;

bool counting = false;
int count = 15;
unsigned long lastBlink = 0;
bool led2State = false;
unsigned long countStartTime = 0;
bool displayOff = false;

void setup() {
  // Setup segment pins
  for (int i = 0; i < 7; i++) {
    pinMode(segPinsTens[i], OUTPUT);
    pinMode(segPinsOnes[i], OUTPUT);
  }

  pinMode(buttonStart, INPUT_PULLUP);
  pinMode(buttonReset, INPUT_PULLUP);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

  digitalWrite(led1, HIGH); // LED-1 initially ON
  clearDisplays();
}

void loop() {
  // Reset logic
  if (digitalRead(buttonReset) == LOW) {
    resetSystem();
    delay(300); // Debounce
  }

  // Start countdown on Button 1 press
  if (!counting && digitalRead(buttonStart) == LOW) {
    digitalWrite(led1, LOW); // Turn off LED-1
    counting = true;
    countStartTime = millis();
    displayOff = false;
    delay(300); // Debounce
  }

  if (counting) {
    unsigned long currentMillis = millis();

    // Countdown logic (1 second per decrement)
    if (count > 0 && currentMillis - countStartTime >= 1000) {
      count--;
      countStartTime = currentMillis;
    }

    // Flash LED-2 every 1 second during countdown
    if (count > 0 && currentMillis - lastBlink >= 500) {
      led2State = !led2State;
      digitalWrite(led2, led2State);
      lastBlink = currentMillis;
    }

    // At 0: keep LED-2 on, turn off displays after 1s
    if (count == 0) {
      digitalWrite(led2, HIGH);
      if (!displayOff && currentMillis - countStartTime >= 1000) {
        clearDisplays();
        displayOff = true;
      }
    }

    // Update display
    if (!displayOff) {
      showNumber(count);
    }
  }
}

void showNumber(int num) {
  int tens = num / 10;
  int ones = num % 10;

  displayDigit(tens, segPinsTens);
  displayDigit(ones, segPinsOnes);
}

void displayDigit(int digit, const int segPins[7]) {
  byte segs = digits[digit];
  for (int i = 0; i < 7; i++) {
    digitalWrite(segPins[i], bitRead(segs, 7 - i)); // Common anode: HIGH = off
  }
}

void clearDisplays() {
  for (int i = 0; i < 7; i++) {
    digitalWrite(segPinsTens[i], HIGH); // Turn off all segments
    digitalWrite(segPinsOnes[i], HIGH);
  }
}

void resetSystem() {
  counting = false;
  count = 15;
  digitalWrite(led1, HIGH);
  digitalWrite(led2, LOW);
  led2State = false;
  displayOff = false;
  clearDisplays();
}

To which pins on the 7 segment displays have you wired the above Arduino pins ?
If you have not already checked the data sheet of the seven segment display for the correct mapping, then post a link to it here.

When the program starts, all the segments will be lit when the pins are set to OUTPUT in setup().

EDIT

This table is also odd (a) from the sorting order and (b) the difference between a '0' and an '8' should be only in one bit so this should also be reviewed to see if is simply the comments which are incorrect.

So start with something simple first

Not really cheating, because now you have to find and correct all the mistakes made by the bot, and there are many.

May have to hire someone for this.

The flag button below the post can be used to ask a moderator to move the thread to the Jobs and Paid Consultancy forum section.

( :robot: translated)
Cheating or not depends on how you use it. You have a wonderful and patient artificial assistant at your disposal who can explain everything to you — don’t let it do things for you, let it explain them to you :wink:

The segments of the displays are typically called as follows:

    -A-
  F|   |B
    -G-
  E|   |C
    -D-

Create a byte array where bit 0 corresponds to the lit segments, for example:

const byte digits[10] = {
  //ABCDEFG.
   B00000011,  // 0
   B10011111,  // 1
   ...etc...2 3 4 5 6...

Then it's a matter of connecting the segments to specific output pins of the Arduino, and writing the bit value associated with the desired digit (array index) to these pins.

The reason for the 'predator gauntlet' is basically the wrong array created by ChatGPT.

Once you understand the principle, the byte table and how to connect the pins are up to you.

Thanks ,
yes! that was it. Plus I was making changes and not recompiling.
started understanding this morning when I figured changes i was making were not being updated.
Plus ChatGPT left of the A segment...
Thanks
D

When did your teacher ask for this homework?

I think the first comment below is correct. But the comments on the other lines are nonsense. Where did those comments come from?

// Segment mapping for numbers 0-9 on common anode 7-segment display
const byte digits[10] = {
  B11000000, // 0
  B11111001, // 5
  B10100100, // 2
  B10110000, // 8
  B10011001, // 4
  B10010010, // 1
  B10000010, // 6
  B11111000, // 7
  B10000000, // 3
  B10010000  // 9
};

If I correct them:

// Segment mapping for numbers 0-9 on common anode 7-segment display
const byte digits[10] = {
  B11000000, // 0
  B11111001, // 1
  B10100100, // 2
  B10110000, // 3
  B10011001, // 4
  B10010010, // 5
  B10000010, // 6
  B11111000, // 7
  B10000000, // 8
  B10010000  // 9
};

Now, there is only one bit difference between '0' and '8', as expected. The count of zero bits for each digit seems to correspond to the number of lit segments, so I think this code is ok.

You have to wire the segments to the correct pins in order to get normal, non-predator numbers.

Take this example:

B10011001, // 4

Where the bits are zero, we want the corresponding segments to light to make the symbol "4" appear, like the upper-case letters here:

.aaa.
F...B
F...B
F...B
.GGG.
e...C
e...C
e...C
.ddd.

This tells us that the segment patterns should be understood as follows:

//gfedcba
B10011001, // 4

The zero bits would light segments b, c, f, g

The pins for the ones digit are as follows:

const int segPinsOnes[7] = {9, 10, 11, 12, 13, A0, A1}; // Ones display

So I think pin 9 should be connected to segment a, pin 10 to segment b and so on until pin A1 connected to segment g.

Is this how you have them connected?

If not, you can either re-wire them to match, or you can change the sequence of pins in the segPinsOnes array to match your wiring.

Ha, It's my 61st BDay