4 Digit 7 Segment Stopwatch help

Don't use "default:". Use "case 0:".

@steve_111 The auto-formatting may be ok in the ide, but the formatting was all lost when you pasted it into your post without using code tags.

@UKHeliBob does the "copy for forum" feature work with the new forum? I haven't tested it.

No need for Auto formatting ? I find that hard to believe

In the IDE do you really have 3 }, each on their own line at the end of the sketch ?

Note that Auto format will not fix problems, rather it makes the code easier to read and can make some problems easier to spot

Where did you get the code from ?

I changed it, but nothing happened to the 0's.

I got the code from a website. It helped me a little along the way.

@ PaulRB
Copy for forum works fine here. After all, all it does is to add the code tags

Hello
all in all the sketch is doing what it is coded for.
This is very importent comment in line 15
long n = 0;// n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999.
For test puposes you can insert

n=millis();

to check the wiring.

But code tags are different now. It's a bunch of `s rather than [ code ] and [ / code ]

Let's try it:

Code tags using new forum style tags
Code tags using old forum style tags

Yes, both work.

Please use code-tags (or copy for forum) modify your post, this really doesn't provide much overview.

Your code works, actually it does, except that 'n' is never modified. It is declared '0' and that's it. all calculations on what to display are derived from it.

int count = 0;//Set count=0. Here count is a count value that increases by 1 every 0.1 second, which means 1 second is counted when the value is 10

that doesn't happen either.
In other words, add() is never called why would that happen automatically.
I suppose you could set up a timer to call add() every 100ms, or you could poll using millis(), but then probably more appropriate would be to just use millis() and an elapsed time function.

But i think most of us got stuck with looking at the structure of your sketch. If you've gotten this from the internet, you picked a sketch with a terrible structure.

Just some ideas for that :

Your pin assignments for the 7-segment should probably be put into 2 arrays

int a = 2;
int b = 3;
int c = 4;
int d = 5;
int e = 6;
int f = 7;
int g = 8;
int p = 13;
int anodePin[8] = {2, 3, 4, 5, 6, 7, 8, 13};
int cathodePin[4] = {12, 11, 10, 9};

void setup() {
 for (uint8_t i = 0; i < 8; i++) pinMode(anodePin[i], OUTPUT);
 for (uint8_t i = 0; i < 4; i++) pinMode(cathodePin[i], OUTPUT);
}

Now you can also address the pins in the functions from that

void pickDigit(int digit) //light up a 7-segment display
{
  for (uint8_t i = 0; i < 4; i++) {
    if (i == digit) digitalWrite(cathodePin[i], LOW);
    else digitalWrite(cathodePin[i], HIGH);
  }


void clearLEDs() { //clear the 7-segment display screen
  for (uint8_t i = 0; i < 8; i++) digitalWrite(anodePin[i], LOW);
}

void digit(uint8_t number) {
  const uint8_t setpin[] = { 0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 
                       0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111 };
  for (uint8_t i = 0; i < 8; i++) {
    if ((setpin[number] >> i) & 1) digitalWrite(anodePin[i], HIGH);
    else digitalWrite(anodePin[i], LOW);
  }
}

and even call these from loop() from a for loop.

Now the code can look like this :

//the pins of 4-digit 7-segment display attach to pin2-13 respectively
int anodePin[8] = {2, 3, 4, 5, 6, 7, 8, 13};
int cathodePin[4] = {12, 11, 10, 9};

long n = 0;           // n represents the value displayed on the LED display. For example, when n=0, 0000 is displayed. The maximum value is 9999.
int x = 100;
int del = 5;           // Set del as 5; the value is the degree of fine tuning for the clock
int count = 0;         // Set count=0. Here count is a count value that increases by 1 every 0.1 
                       // second, which means 1 second is counted when the value is 10
void setup() {
 for (uint8_t i = 0; i < 8; i++) pinMode(anodePin[i], OUTPUT);
 for (uint8_t i = 0; i < 4; i++) pinMode(cathodePin[i], OUTPUT);
}



void loop(){

  long temp = n; // transfer your counter into a temporary value that we can modify
  for (uint8_t i = 0; i < 4; i++) {
    clearLEDs();
    pickDigit(i);
    pickNumber(temp % 10); // extract the remainder of division by 10.
    temp = temp / 10; // do the division
    delay(del);//delay 5ms
  }
}


void pickDigit(int digit) { //light up a 7-segment display
  for (uint8_t i = 0; i < 4; i++) {
    if (i == digit) digitalWrite(cathodePin[i], LOW);
    else digitalWrite(cathodePin[i], HIGH);
  }
}
void clearLEDs() { //clear the 7-segment display screen
  for (uint8_t i = 0; i < 8; i++) digitalWrite(anodePin[i], LOW);
}

void pickNumber(uint8_t number) {
  const uint8_t setpin[] = { 0b00111111, 0b00000110, 0b01011011, 0b01001111, 0b01100110, 
                             0b01101101, 0b01111101, 0b00000111, 0b01111111, 0b01101111 };
  for (uint8_t i = 0; i < 8; i++) {
    if ((setpin[number] >> i) & 1) digitalWrite(anodePin[i], HIGH);
    else digitalWrite(anodePin[i], LOW);
  }
}

void add() {
  count ++;
  if (count == 10)
  {
    count = 0;
    n++;
    if (n == 59)
    {
      n = 0;
    }
  }
}

Which leaves us with getting the add() function to be called every 100ms (or so)

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