Help fixing code for seven segment display

I started learning how to code a week ago I've been working on this project since, i initially thought displaying the time would be easy especially on a digital display like this. But its only kind of easy, you got arrays and multiplexing etc. etc. anyways the problem with my code is that it doesn't allow me to set an initial value for time in minutes. in my code i should be able to go int h = 12; int m = 55; and have it display 1255 but instead it displays 1200 i tried to get ChatGPT to fix it but it took 6 tries and the code it gave displays the digits in reverse(5521 and it fails to fix it, when informed of the problem. (the code im displaying is my base code does not chat gpts altered or rewritten version)

 // Defines seg pins 
int segmentPins[] = {2, 3, 4, 5, 6, 7, 8}; // a, b, c, d, e, f, g
int digitPins[] = {9, 10, 11, 12}; // Digit controll for multiplexing
int m = 55;
int h = 12;
int s = 0 ;
int time = h * 100 + m;


const unsigned long second = 1000;
const unsigned long minut = 60000;
unsigned long currentMillis;

// maps numbers to the binary to display them 
const byte digitPatterns[] = {
  B00111111, // 0
  B00000110, // 1
  B01011011, // 2
  B01001111, // 3
  B01100110, // 4
  B01101101, // 5
  B01111101, // 6
  B00000111, // 7
  B01111111, // 8
  B01101111 // 9
};

void setup() {
  // Set  pins as outputs
  for (int i = 0; i < 7; i++) {
    pinMode(segmentPins[i], OUTPUT);
  }
  // Set digit pins as outputs
  for (int i = 0; i < 4; i++) {
    pinMode(digitPins[i], OUTPUT);
    digitalWrite(digitPins[i], HIGH); // Turn off digits initially
  }
}

void loop() {
  //time code formats values maintains format and counts 
  currentMillis = millis();  

  m = currentMillis / minut ;
  if( m > 59){h++; m = 0;}
  if( h > 12){h = 1;}
              
int time = h * 100 + m;
  
 
  displayNumber(time);
}

// Function to display a number 
void displayNumber(int number) {
  for (int i = 0; i < 4; i++) {
    int digit = number % 10; // Extract the last digit
    number /= 10; // Remove the last digit

    digitalWrite(digitPins[i], LOW); // Enable the digit
    setSegments(digitPatterns[digit]);
    delay(2); // Short delay for multiplexing
    digitalWrite(digitPins[i], HIGH); // Disable the digit
  }
}

// Function to light up segments 
void setSegments(byte pattern) {
  for (int i = 0; i < 7; i++) {
    digitalWrite(segmentPins[i], pattern & (1 << i));
  }   
}

It does, but once the program go to the main loop(), your initial time will be cleared by line :

Your mistake, that the time is not a just millis, it must be initial time added to it.

To going further - do not use a whole value of millis() - it make difficult to manage a hour change. Instead - use a difference between a previous and current value. Once the difference became a minute - increment your m value to one and calculate a minute and hour.

Worst idea, evar.

Here is what your code produces... the first 1255 is from your configuration. The rest are from digit and number

time:1255

digit:5 number:125
digit:5 number:12
digit:2 number:1
digit:1 number:0

This is the core of your code. Fix your core code and you fix your code. Code is fine...

int m = 55;
int h = 12;
int s = 0 ;
int time = h * 100 + m;

void setup() {
  Serial.begin(115200);
  Serial.print("time:");
  Serial.println(time);
}

void loop() {
  displayNumber(time);
  while(1);
}

void displayNumber(int number) {
  for (int i = 0; i < 4; i++) {
    int digit = number % 10; // Extract the last digit
    number /= 10; // Remove the last digit
    Serial.print("digit:");
    Serial.print(digit);
    Serial.print(" number:");
    Serial.println(number);
  }
}

Just call displayNumber() and keep track of the 7seg digit where it goes.

Maybe store the backwards time result in an array, like...

int timeArray[4];

... and when you get a digit, use its index ("i"), so...

timeArray[0] = 5
timeArray[1] = 5
timeArray[2] = 2
timeArray[3] = 1

Then call displayNumber() one at a time, remembering to change the 7seg digit.

@xfpd
The problem is not in display:)

I'm not sure what you mean, but I saw myself over complicating things, then went back (before the strikeout)

I mean that OP saw a 1200 on display not because he works with display wrong, but because the code clears his initial time at the start of program.

1 Like

i fixed my code i don't know exactly why it was broken but it has something to do with minuets being the variable with math done to it. if somebody would tell me the technical reason as to why it's a problem. i like whys with my is

Sorry, your message is not clear understandable for me.
If your native language is not English, you could use a translator.

is that what i did when i changed the counted time from minuets to seconds. avoided a wipe of minuets

If you have a question, show your revised code.

English is my main language, im sorry i was using improper English so it probably doesn't translate to your main very well. MY code isn't broken now i changed m = currentMillis / minut ;
if( m > 59){h++; m = 0;}
if( h > 12){h = 1;}

int time = h * 100 + m;

to

s = currentMillis / second ;
m = s / 60 + inm;
if( m > 59){h++; m = 0; inm = 0;} //inm is initial minuet
if( h > 12){h = 1;}

int time = h * 100 + m;

This code is not correct. Once s exceeds 59 minutes, it starts to increment hour at every run.
You shouldn't use millis directly, use the difference.

ok i will

do you mean when s/60 ++m 60 times it goes s/60 ++h

Try it. Leave your code run for 60 minutes and you will see.

all things considered that wouldn't have been a bad idea. unnecessarily processing heavy but a fix nun the less

weird. when i looked at it 2 mins ago at 11:01 my clock said 11:22 but when i set inm to 59 it goes from 10:59 to 11:00

i didn't really think i would like coding but its fun, at first i thought of it as a means to a end to support my passion for mechanical and electrical designee. But i think i just felt that way because I've been doing mechanical and electrical since 13 for scale im 17, its mostly just math and using that math in creative ways also theirs (at least currently) a mystery to it. Because in mechanical designee you see what's going wrong and electrical designee has 200+ years of metaphors built up to help visualize what's happening. but coding is detached from are reality. aging at least it feels that way at my skill level Maby the other 2 felt that way once two, Damn am having a 1/5th life crisis

"coding is a science" versus "coding is an art" starts wars all the time. It is an unwavering, precise language, but with no single path to an end.

i just noticed it why do you have a blue led specifically as your profile picture. is it because that was the hardest led to discover?