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.
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...
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
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;}
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