# 4digit 7segment stopwatch HELP

Hi im making 4 bit 7 segment stopwatch i need help whit stopwatch doest work properly this is whats wrong:

-after starting my stopwatch and stopwatch on phone at the same time theres little difference at 59sec on phone on arduino stopwatch shows 56-57

the second problem

-how coud i change that after 59 secods it woud add 1minute and not after 99sec if i change
for( int i = 0; i<=9999; i++) to for( int i = 0; i<=59; i++) after 59 seconds it does not add 1minute and just starts counting seconds from 0 again

the third problem

• after stopwatch reaches 99seconds this happens i post video in the link Untitled on Vimeo
``````//code write by Moz for YouTube changel LogMaker360, 5-11-2016
//code belongs to this video, https://www.youtube.com/watch?v=256VQ6eVNng

int digit1 = 9; //PWM Display most left display
int digit2 = 10; //PWM Display second left
int digit3 = 11; //PWM Display second right display
int digit4 = 12; //PWM Display most right display

#define DIGIT_ON  LOW
#define DIGIT_OFF  HIGH
#define DISPLAY_BRIGHTNESS  500

boolean duiz = false;
boolean hon = false;

int segA = 2;
int segB = 3;
int segC = 4;
int segD = 5;
int segE = 6; //pin 6 is used bij display 1 for its pwm function
int segF = 7;
int segG = 8;
int segPD = 13;
void setup() {
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(segPD, OUTPUT);

pinMode(digit1, OUTPUT);
pinMode(digit2, OUTPUT);
pinMode(digit3, OUTPUT);
pinMode(digit4, OUTPUT);
Serial.begin(9600);

}
void loop(){
int number;
Serial.println(number);

for( int i = 0; i<=9999; i++){// for loop to pick a number from.
duiz = false;
hon = false;
for(int k = 0; k<450; k++){ // for loop to slow it down.

int figur = i;
for(int digit = 1 ; digit < 5 ; digit++) { //for loop to place the number in the right digit

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
if(figur > 0){
digitalWrite(digit1, DIGIT_ON);
lightNumber(figur / 1000);            // for example 2511 / 1000 = 2
figur %= 1000;                        // new value of figur = 511         figur = figur %1000

delayMicroseconds(DISPLAY_BRIGHTNESS);
if (figur < 100){
duiz = true;
if (figur <10){
hon = true;

}

}else duiz = false;
}

break;
case 2:
if(duiz == true){
digitalWrite(digit2, DIGIT_ON);
lightNumber(0);
delayMicroseconds(DISPLAY_BRIGHTNESS);

}if(hon == true){
break;
}

if(figur > 99 && figur < 1000){
digitalWrite(digit2, DIGIT_ON);
lightNumber(figur / 100);
figur %= 100;
delayMicroseconds(DISPLAY_BRIGHTNESS);
if (figur < 10){
hon = true;

}else hon = false;
}

break;
case 3:
if(hon == true){
digitalWrite(digit3, DIGIT_ON);
lightNumber(0);
delayMicroseconds(DISPLAY_BRIGHTNESS);
break;
}

if(figur > 9 && figur < 100){
digitalWrite(digit3, DIGIT_ON);
lightNumber(figur / 10);
figur %= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS);
}

break;
case 4:

if(figur < 10){
digitalWrite(digit4, DIGIT_ON);
lightNumber(figur);
delayMicroseconds(DISPLAY_BRIGHTNESS);

break;
}

}
//Turn off all segments
lightNumber(10);

//Turn off all digits
digitalWrite(digit1, DIGIT_OFF);
digitalWrite(digit2, DIGIT_OFF);
digitalWrite(digit3, DIGIT_OFF);
digitalWrite(digit4, DIGIT_OFF);
digitalWrite(segPD, HIGH);

}
}}}

void lightNumber(int numberToDisplay) {

#define SEGMENT_ON  HIGH
#define SEGMENT_OFF LOW

switch (numberToDisplay){

case 0:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_OFF);
break;

case 1:
digitalWrite(segA, SEGMENT_OFF);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;

case 2:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_OFF);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_ON);
break;

case 3:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_ON);
break;

case 4:
digitalWrite(segA, SEGMENT_OFF);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;

case 5:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;

case 6:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;

case 7:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;

case 8:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_ON);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;

case 9:
digitalWrite(segA, SEGMENT_ON);
digitalWrite(segB, SEGMENT_ON);
digitalWrite(segC, SEGMENT_ON);
digitalWrite(segD, SEGMENT_ON);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_ON);
digitalWrite(segG, SEGMENT_ON);
break;

case 10:
digitalWrite(segA, SEGMENT_OFF);
digitalWrite(segB, SEGMENT_OFF);
digitalWrite(segC, SEGMENT_OFF);
digitalWrite(segD, SEGMENT_OFF);
digitalWrite(segE, SEGMENT_OFF);
digitalWrite(segF, SEGMENT_OFF);
digitalWrite(segG, SEGMENT_OFF);
break;
}

}
``````

From my point of view a stopwatch needs a start button and a stop button. I don't see that in your sketch.
Secondly - you are dealing with delayMicrosecond(...) - that is not precise at all! You should use variables with microseconds() function.

And - sorry - I cannot recognise the program logic.

A good structure of a program

• should deal with the logic part - in your case with updated variables for milliseconds, seconds, minutes,...
• and a second part to show the variables at the 7-segemnt-pieces.

ok now i fixed the first problem seconds now counts precise just need to figure out second and third problem

Here is a 4-digit 7-segment timer I wrote which you can use as an example. It uses the millis() function to provide milliseconds since the sketch started. For the first 99.99 seconds it shows seconds and hundredths. From 100 seconds to 99 minutes and 59 seconds it shows minutes and seconds. After that it shows hours and minutes. It should be as accurate as the Arduino system clock. On an Arduino Uno that's about 10 seconds a day.

``````// Bit maps for the seven segment display
// Segment Cathodes (0/LOW for on, 1/HIGH for off)
const byte Segments[] =
{
0b11000000, // 0
0b11001111, // 1
0b10100100, // 2
0b10000110, // 3
0b10001011, // 4
0b10010010, // 5
0b10010000, // 6
0b11000111, // 7
0b10000000, // 8
0b10000011, // 9
};

const byte ColonLowerDot = 12;
const byte ColonUpperDot = 13;

// List of digit select lines, least significant digit first
// Digit Common Anodes (HIGH for on, LOW for off)
const byte DigitCount = 4;
const unsigned char DigitPins[DigitCount] = {A0, 2, 3, 4};

// Segment Cathodes (LOW for on, HIGH for off)
const byte SegmentCount = 7;
const unsigned char SegmentPins[SegmentCount] = {5, 6, 7, 8, 9, 10, 11};

void setup()
{
for (int i = 0; i < DigitCount; i++)
{
pinMode(DigitPins[i], OUTPUT);
digitalWrite(DigitPins[i], LOW); // Off
}

for (int i = 0; i < SegmentCount; i++)
{
pinMode(SegmentPins[i], OUTPUT);
digitalWrite(SegmentPins[i], HIGH);  // Off
}

digitalWrite(ColonLowerDot, HIGH);
digitalWrite(ColonUpperDot, HIGH);

pinMode(ColonLowerDot, OUTPUT);
pinMode(ColonUpperDot, OUTPUT);
}

void loop()
{
// Get time since last reset
unsigned long hundredths = millis() / 10;
unsigned long seconds = hundredths / 100;
unsigned long minutes = seconds / 60;
int hours = minutes / 60;
int clock;

if (seconds < 100)
// Display seconds.hundredths up to 99.99 seconds, then minutes:seconds
clock = (seconds % 100) * 100 + (hundredths % 100);
else if (minutes < 100)
// Display minutes:seconds up to 99 minutes and 59 seconds, then hours/minutes
clock = (minutes % 100) * 100 + (seconds % 60);
else
clock = (hours % 100) * 100 + (minutes % 60);

// Clear all segments before enabling a digit
for (int s = 0; s < SegmentCount; s++)
{
digitalWrite(SegmentPins[s], HIGH);
}

// Display each digit, right to left
for (int i = 0; i < DigitCount; i++)
{
// Peel a digit off the low end of the number
int digit = clock % 10;
clock /= 10;

// Blank the MSD if it is zero
if (i == 3 && digit == 0)
{
for (int s = 0; s < SegmentCount; s++)
digitalWrite(SegmentPins[s], HIGH);
}
else
{
// Display the digit on the seven segments
unsigned char segments = Segments[digit];
for (int s = 0; s < SegmentCount; s++)
{
digitalWrite(SegmentPins[s], segments & 1);
segments >>= 1;
}
}

if (seconds < 100)
{
// Steady decimal point when showing seconds and hundredths
digitalWrite(ColonLowerDot, HIGH);
digitalWrite(ColonUpperDot, LOW);
}
else if (minutes < 100)
{
// Steady colon when showing minutes and seconds
digitalWrite(ColonLowerDot, LOW);
digitalWrite(ColonUpperDot, LOW);
}
else
{
// Make the colon blink each second
digitalWrite(ColonLowerDot, seconds & 1);
digitalWrite(ColonUpperDot, seconds & 1);
}

// Turn on the digit briefly
digitalWrite(DigitPins[i], HIGH);  // Select one digit
delayMicroseconds(3000UL);  // Higher numbers give higher brightness but more flicker
digitalWrite(DigitPins[i], LOW);
}
}
``````