Fixing timer bug

Hello, I've been trying for a while and can't figure this out

I've created a countdown timer that goes from ---- Ten minutes -> Minutes -> Ten Seconds -> Seconds
How can I change the Ten second counter to start from 6 instead of 9?

I've written out the code as following:

const int a = 12;
const int b = 10;
const int c = 8;
const int d = 7;
const int e = 6;
const int f = 11;
const int g = 9;
int p = 0;

int startStopReset = 13; 

const int d1 =1;
const int d2 =2;
const int d3 =3;
const int d4 =4;

long n = 3600000; //start time -> CAN CHANGE TO WHATEVER TIME YOU WANT
int x = 100; 
int del = 2588; //delay value
 
void setup()
{
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);
  pinMode(startStopReset, INPUT); 
  digitalWrite(startStopReset, HIGH); 
}
 
void loop()
{
  digitalWrite (p, HIGH);

  clearLEDs();
  pickDigit(1);
  pickNumber((n/x/1000)%10);
  delayMicroseconds(del);
 
  clearLEDs();
  pickDigit(2);
  pickNumber((n/x/100)%10);
  delayMicroseconds(del);
 
  clearLEDs();
  pickDigit(3);
  dispDec(3);
  pickNumber((n/x/10)%10);
  delayMicroseconds(del);
 
  clearLEDs();
  pickDigit(4);
  pickNumber(n/x%10);
  delayMicroseconds(del);
 
  n--; //'n++' for stopwatch
 
  if (digitalRead(13) == LOW)
  {
    n = 3600000; //re-start time -> CHANGE TO WHATEVER TIME YOU ORIGONALLY SET (start time)
  }
}
 
void pickDigit(int x) //changes digit
{
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);
 
  switch(x)
  {
  case 1: 
    digitalWrite(d1, LOW); 
    break;
  case 2: 
    digitalWrite(d2, LOW); 
    break;
  case 3: 
    digitalWrite(d3, LOW);
    digitalWrite(p, HIGH); //new 
    break;
  default: 
    digitalWrite(d4, LOW); 
    break;
  }
}
 
void pickNumber(int x) //changes value of number
{
  switch(x)
  {
  default: 
    zero(); 
    break;
  case 1: 
    one(); 
    break;
  case 2: 
    two(); 
    break;
  case 3: 
    three(); 
    break;
  case 4: 
    four(); 
    break;
  case 5: 
    five(); 
    break;
  case 6: 
    six(); 
    break;
  case 7: 
    seven(); 
    break;
  case 8: 
    eight(); 
    break;
  case 9: 
    nine(); 
    break;
  }
}
 
void dispDec(int x)
{
  digitalWrite(p, HIGH);
}
 
void clearLEDs()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}
 
void zero()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}
 
void one()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
 
void two()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
 
void three()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}
 
void four()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
 
void five()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
 
void six()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
 
void seven()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}
 
void eight()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}
 
void nine()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

Thanks for posting the code the best way!

Having a glance of the ton of code I think You're "new in the game". A usual mistake is that the code contains almost no comments telling what the lines are supposed to do.

What is the over all purpose of the project? Suppose You're heading into a dead end or are using a difficult strategy? Helping You now and You get bang stuck later is a waste of time.

To get an overview I would like to see the flow chart, the "map", of the flow of the execution. It takes too much time to discover what the code is doing, how it's running.

What is the result of the code running and what is the result You want?

And perhaps if you start adding comments - definitely not a comment to every line, but a description of each small section of code, you will figure out what you need to do. :thinking:

Hello Mr. Railroader,
I am using a 4 digit 7 segment display to make a standard timer. That code was from another source from the Arduino Project Hub, I can give you the link if you want, just know that I modified the code to best fit the situation.
https://create.arduino.cc/projecthub/tylerpeppy/arduino-countdown-timer-fc2fd9?ref=search&ref_id=timer&offset=15

Yes. I was not in the mood of writing a novell telling "how to"...

That's an okey way to start a project. I would still want to get an overview of the code and a description of the main idea. Plowing through projects calls for more time then I'm prepared to spend.

You extract the essential information and present it here.

Earlier on you could tell that I was new. And I am, I honestly don't know too much of how 7 segments work, or how the code works myself, but what I can tell you from the code is that we start with addressing the segments of the display, then tell it wether or not to count up or down, starting with an initial start time. The clock then counts down, in seconds to reach 0, but within that, the ten second digit counter starts at 9, but I need it to count down and reset it at 6, then over again, to make sure the timer is accurate. (I at least took some lessons in Javascript however, so I understand a little bit)

Your message is about a 7 segment display and I would like to know the idea, the strategy for any information sent to the display.
Plowing into that code not knowing how it's supposed to work, what the pieces are supposed to do.... I say no.

I agree with you, and knowing my situation now, I understand now that I'm going to need to learn myself what all these pieces do, if I'm going to get anywhere. So I'm going to take the time and learn all the things about how this works, and how to use it.
Thanks for your help, even though we didn't get to a direct solution, your questions are something I can use for all my projects later in the future.

One strategy I used during my time working with programming and new, unknown code was: Use serial monitor and Serial.println in the code to show what's going on there. Many times one setup of prints need a second set.
You'll crack down any code that way.
One approach for a down counting timer could be like converting the time to seconds, and count down seconds. It's fairly easy to convert a bucket of seconds down to hours, minutes and seconds instead of bothering with 00 to 59 to .....

That code is written by an amateur (no comments) and code can contain "anything".

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