# 4 digit 7 segment display stopwatch

Hello i am new to programming so i woud need your help i am using this 7 segment display http://www.farnell.com/datasheets/2095874.pdf?_ga=2.127782861.1566654749.1572523254-912966484.1572389117

what i cant figure it out are 2 problem the main one is how to add 1 minute after every 60sec, i have wired everything correctly to protoboard so far the secods count correctly to 59 after that secods returns to 0 but it does not add 1 minute and also how can i make 2 dots in the middle betwen minutes and secods blink every 1 second the program code you can see bellow in my case 2 blinking dots are int p on pin 13 i did try to do it in the loop like this:
digitalWrite(p, HIGH);
delay(1000);
digitalWrite(p, LOW);
delay(1000);
but what happens is that all the digits starts blinking insted of just dots.

I hope you can help me whit i woud be verry grateful i am creating stopwatch for my personal project thanks here is my code below:

#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively
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 d1 = 12;
int d2 = 11;
int d3 = 10;
int d4 = 9;
//int dp=14;
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()
{
//set all the pins of the LED display as output
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);

Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
Timer1.attachInterrupt( add ); // attach the service routine here
}
/***************************************/

void loop()
{

clearLEDs();//clear the 7-segment display screen
pickDigit(0);//Light up 7-segment display d1
pickNumber((n/1000));// get the value of thousand
delay(del);//delay 5ms

clearLEDs();//clear the 7-segment display screen
pickDigit(1);//Light up 7-segment display d2
pickNumber((n%1000)/100);// get the value of hundred
delay(del);//delay 5ms

clearLEDs();//clear the 7-segment display screen
pickDigit(2);//Light up 7-segment display d3
pickNumber(n%100/10);//get the value of ten
delay(del);//delay 5ms

clearLEDs();//clear the 7-segment display screen
pickDigit(3);//Light up 7-segment display d4
pickNumber(n%10);//Get the value of single digit
delay(del);//delay 5ms

}
/**************************************/
void pickDigit(int x) //light up a 7-segment display
{
//The 7-segment LED display is a common-cathode one. So also use digitalWrite to set d1 as high and the LED will go out
digitalWrite(d4, HIGH);
digitalWrite(d3, HIGH);
digitalWrite(d2, HIGH);
digitalWrite(d1, HIGH);
digitalWrite(p, HIGH);

switch(x)
{
case 0:
digitalWrite(d4, LOW);//Light d1 up
break;
case 1:
digitalWrite(d3, LOW); //Light d2 up
break;
case 2:
digitalWrite(d2, LOW); //Light d3 up
break;
default:
digitalWrite(d1, LOW); //Light d4 up
break;
}
}
//The function is to control the 7-segment LED display to display numbers. Here x is the number to be displayed. It is an integer from 0 to 9
void pickNumber(int x)
{
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 clearLEDs() //clear the 7-segment display screen
{
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() //the 7-segment led display 0
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}

void one() //the 7-segment led display 1
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void two() //the 7-segment led display 2
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void three() //the 7-segment led display 3
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}

void four() //the 7-segment led display 4
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void five() //the 7-segment led display 5
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void six() //the 7-segment led display 6
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void seven() //the 7-segment led display 7
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void eight() //the 7-segment led display 8
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void nine() //the 7-segment led display 9
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
/*******************************************/
{
// Toggle LED
count ++;
if(count == 10)
{
count = 0;
n++;
if(n == 59)
{
n = 0;
}
}
}

It looks like you are displaying the value of variable “n”, which never gets higher than 59.

Try removing these lines:

``````    if (n == 59)
{
n = 0;
}
``````

from add() and see what happens.

Since the variables “n” and “count” are used in an ISR they need to be declared volatile like this:

``````volatile 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.
volatile 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
``````

Original code formatted:

``````#include <TimerOne.h>
//the pins of 4-digit 7-segment display attach to pin2-13 respectively
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 d1 = 12;
int d2 = 11;
int d3 = 10;
int d4 = 9;
//int dp=14;
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()
{
//set all the pins of the LED display as output
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);

Timer1.initialize(100000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
Timer1.attachInterrupt( add ); // attach the service routine here
}
/***************************************/

void loop()
{

clearLEDs();//clear the 7-segment display screen
pickDigit(0);//Light up 7-segment display d1
pickNumber((n / 1000)); // get the value of thousand
delay(del);//delay 5ms

clearLEDs();//clear the 7-segment display screen
pickDigit(1);//Light up 7-segment display d2
pickNumber((n % 1000) / 100); // get the value of hundred
delay(del);//delay 5ms

clearLEDs();//clear the 7-segment display screen
pickDigit(2);//Light up 7-segment display d3
pickNumber(n % 100 / 10); //get the value of ten
delay(del);//delay 5ms

clearLEDs();//clear the 7-segment display screen
pickDigit(3);//Light up 7-segment display d4
pickNumber(n % 10); //Get the value of single digit
delay(del);//delay 5ms

}
/**************************************/
void pickDigit(int x) //light up a 7-segment display
{
//The 7-segment LED display is a common-cathode one. So also use digitalWrite to  set d1 as high and the LED will go out
digitalWrite(d4, HIGH);
digitalWrite(d3, HIGH);
digitalWrite(d2, HIGH);
digitalWrite(d1, HIGH);
digitalWrite(p, HIGH);

switch (x)
{
case 0:
digitalWrite(d4, LOW);//Light d1 up
break;
case 1:
digitalWrite(d3, LOW); //Light d2 up
break;
case 2:
digitalWrite(d2, LOW); //Light d3 up
break;
default:
digitalWrite(d1, LOW); //Light d4 up
break;
}
}
//The function is to control the 7-segment LED display to display numbers. Here x is the number to be displayed. It is an integer from 0 to 9
void pickNumber(int x)
{
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 clearLEDs() //clear the 7-segment display screen
{
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() //the 7-segment led display 0
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
}

void one() //the 7-segment led display 1
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void two() //the 7-segment led display 2
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void three() //the 7-segment led display 3
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}

void four() //the 7-segment led display 4
{
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void five() //the 7-segment led display 5
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void six() //the 7-segment led display 6
{
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void seven() //the 7-segment led display 7
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
}

void eight() //the 7-segment led display 8
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}

void nine() //the 7-segment led display 9
{
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
}
/*******************************************/
{
// Toggle LED
count ++;
if (count == 10)
{
count = 0;
n++;
if (n == 59)
{
n = 0;
}
}
}
``````

hi if i remove
if (n == 59)
{
n = 0;
}
from the code secods count up to 99 then adds 1 minute how how to count to 59 and then add minute

You need to change the math you're using. The below might give you an idea, try running it and looking at the serial monitor.

``````long n = 0;

void setup()
{
Serial.begin(9600);
}

void loop()
{
Serial.print((n%1000)/100);// get the value of hundred
Serial.print(" : ");
Serial.print((n % 100 / 10)); //get the value of ten
Serial.print((n % 10)); //Get the value of single digit
Serial.print("  ");
Serial.print(n/60);// get the value of hundred
Serial.print(" : ");
Serial.print((n % 60 / 10)); //get the value of ten
Serial.println((n % 10)); //Get the value of single digit
delay(500);
n++;
}
``````

Im sorry i really cannot figure this out

This is the Stop Watch I have built as an exercise, which may help you to get answers to some of your queries particularly the way of updating the SEC-MIN field and placing dot (.) with the MIN field. In this Stop Watch, 1 sec Time Tick has been generated using millis() function. There are two buttons marked as START and STOP which initiate the Start and Stop of the counting of Stop Watch.

Figure-1:

Sketch: (tested in UNO)

``````byte lupTable[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};//cc-codes:0-9
byte ccTable[] = {0x3F, 0x3F, 0x3F, 0x3F};//DP0-DP3 holds cc-code for DP0-DP3
byte decSec = 0;
byte decMin = 0;

void setup()
{
Serial.begin(9600);
for (byte i = 8; i < 18; i++)//DPin-8 to 13; A0 to A3
{
pinMode(i, OUTPUT);
}
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
ccTable[1] = ccTable[1]|0x80;   //[lace dot (.) with MIN field
while (digitalRead(2) != LOW)   //checking if START button has been pressed
{
refreshDisplay();
}
}

void loop()
{
if(digitalRead(3) == LOW)  //STOP the counting of Stop Watch
{
while(1)   //wait for ever
{
refreshDisplay();   //keep showing current count of Stop Watch
}
}
unsigned long prMillis = millis();
while (millis() - prMillis < 1000)
{
ccTable[3] = lupTable[decSec % 10];
ccTable[2] = lupTable[decSec / 10];
//---------------------------------
ccTable[1] = lupTable[decMin % 10];
ccTable[1] = ccTable[1] | 0x80;       //place dot with MIN
ccTable[0] = lupTable[decMin / 10];
//---------------------------------
refreshDisplay();                     //refresh display until 1sec has elapsed
}
decSec = decSec + 1;  //increase 1 sec  //increase decSec variable by 1 seconed
if (decSec == 60)     //check if 60 sec has elapsed
{
decSec = 00;
decMin = decMin + 1; //increase decMin variable by 1 min
if (decMin == 60)     //check if 60 Min has elapsed
{
decMin = 00;
}
}
}

void refreshDisplay()
{

for (byte i = 0; i < 4; i++)
{
byte y = 0b111111;
byte x = ccTable[i];
PORTB = x;