How to display digital clock seconds?

i want to make 6 digit seven segment clock but in there i got only display hours and minutes so how to make it display seconds?

here my code:

and how to remove ghosting? all segments become dim when it off
thanks for your help :slight_smile:

Read the how to use this forum-please read stickies to see how to post code. No one here likes having to download code. Make it easier for us to help you and you will get more and faster help.

i know how to put code, but this forum have limited characters and my code is too long to post :slight_smile:

TendoH:
i know how to put code, but this forum have limited characters and my code is too long to post :slight_smile:

If you use Reply, NOT the Quick Reply field, there is a link below the text entry field that allows you to attach code here.

If you don't want to do that, you should post your question in the rubbish bin, too.

Ok, Here My Code:

#include "Wire.h"
#include "RTClib.h"
#include "EEPROM.h"

RTC_DS1307 RTC;

int hours; 
int hh; 
int mm;
int ss;
int timecc;
int buzer = 30;

int segmentA = 6;
int segmentB = 7;
int segmentC = 8;
int segmentD = 9;
int segmentE = 10;
int segmentF = 11;
int segmentG = 12;
int digitOne = A3; //A0
int digitTwo = A2; //A1
int digitThree = A1; //A3
int digitFour = A0; //A2
//int digitFive = 2; //A2
//int digitSix = 13; //A2
int hoursButton = 5;
int minsButton = 3;
int enter = 4;
int pin=17;
int relay=1;
int ap=16;

byte value;
byte value2;
byte value3;

void setup() {
  pinMode(buzer,OUTPUT);
  pinMode(pin,OUTPUT);
  pinMode(relay,OUTPUT);
 
  pinMode(segmentA, OUTPUT);
  pinMode(segmentB, OUTPUT);
  pinMode(segmentC, OUTPUT);
  pinMode(segmentD, OUTPUT);
  pinMode(segmentE, OUTPUT);
  pinMode(segmentF, OUTPUT);
  pinMode(segmentG, OUTPUT);
  pinMode(digitOne, OUTPUT);
  pinMode(digitTwo, OUTPUT);
  pinMode(digitThree, OUTPUT);
  pinMode(digitFour, OUTPUT);

  pinMode(hoursButton, INPUT_PULLUP);
  pinMode(minsButton, INPUT_PULLUP);
  pinMode(enter, INPUT_PULLUP);
  pinMode(ap,OUTPUT);
 
  Wire.begin();
  RTC.begin();  

 
  if (! RTC.isrunning()) {
   
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }

 

 Wire.beginTransmission(0X68);
 Wire.write(0x07); 
 Wire.write(0x10); 
 Wire.endTransmission();

 digitalWrite(buzer,HIGH);
 delay(1000);
 digitalWrite(buzer,LOW);
 value = EEPROM.read(0);
 value2 = EEPROM.read(1);
 if(value>23){
  EEPROM.write(0, 0);
 }
 if(value2>59){
  EEPROM.write(1, 0);
 }
 
}

void loop() {
  
  aman:
  value = EEPROM.read(0);
  value2 = EEPROM.read(1);
  value3 = EEPROM.read(2);
  DateTime now = RTC.now(); 
  hours = now.hour(); 
  mm = now.minute();
  ss = now.second(); 
  if(hours>23){
  digitalWrite(pin,HIGH);
  }
  else digitalWrite(pin,LOW);
 
  hh = hours;

  if (hours < 1) {
    hh = 00;
  }

  if ((hours > 24) && (hours < 24)) {
    hh = hours - 24;
  }

  timecc = hh*100+mm;
  showNum(timecc);

  if (digitalRead(hoursButton) == LOW) {
    mati();
   
    delay(200);
    hours++;
    ss = 00;

    if (hours > 23) {
      hours = 00;
    }

    updateRTC();
  } 

 
  if (digitalRead(minsButton) == LOW) {
    mati();
    //debounce
    delay(200);

    mm++;
    ss = 00;

    if (mm >59) {
      mm = 00;
    }

    updateRTC();
  }

  if (digitalRead(enter) == LOW){
    mati();
    delay(200);
    if(digitalRead(enter)==LOW){
      if(value3==1){
        for(int a=0; a<500; a++){
          mati();
        digitalWrite(digitOne,LOW);
        digitalWrite(digitTwo,HIGH);
        digitalWrite(digitThree, LOW);
        digitalWrite(digitFour,LOW);
        digitalWrite(segmentA,LOW);
        digitalWrite(segmentB,LOW);
        digitalWrite(segmentC,LOW);
        digitalWrite(segmentD,LOW);
        digitalWrite(segmentE,LOW);
        digitalWrite(segmentF,LOW);
        digitalWrite(segmentG,HIGH);
        delay(4);
        mati();
        digitalWrite(digitOne,LOW);
        digitalWrite(digitTwo,LOW);
        digitalWrite(digitThree, HIGH);
        digitalWrite(digitFour,LOW);
        digitalWrite(segmentA,LOW);
        digitalWrite(segmentB,HIGH);
        digitalWrite(segmentC,HIGH);
        digitalWrite(segmentD,HIGH);
        digitalWrite(segmentE,LOW);
        digitalWrite(segmentF,LOW);
        digitalWrite(segmentG,LOW);
        delay(4);
        mati();
        digitalWrite(digitOne,LOW);
        digitalWrite(digitTwo,LOW);
        digitalWrite(digitThree, LOW);
        digitalWrite(digitFour,HIGH);
        digitalWrite(segmentA,LOW);
        digitalWrite(segmentB,HIGH);
        digitalWrite(segmentC,HIGH);
        digitalWrite(segmentD,HIGH);
        digitalWrite(segmentE,LOW);
        digitalWrite(segmentF,LOW);
        digitalWrite(segmentG,LOW);
        delay(4);

        }
        EEPROM.write(2, 0);
        goto aman;
      }
      else{
        for(int a=0; a<500; a++){
          mati();
        digitalWrite(digitOne,LOW);
        digitalWrite(digitTwo,LOW);
        digitalWrite(digitThree, HIGH);
        digitalWrite(digitFour,LOW);
        digitalWrite(segmentA,LOW);
        digitalWrite(segmentB,LOW);
        digitalWrite(segmentC,LOW);
        digitalWrite(segmentD,LOW);
        digitalWrite(segmentE,LOW);
        digitalWrite(segmentF,LOW);
        digitalWrite(segmentG,HIGH);
        delay(4);
        mati();
        digitalWrite(digitOne,LOW);
        digitalWrite(digitTwo,LOW);
        digitalWrite(digitThree, LOW);
        digitalWrite(digitFour,HIGH);
        digitalWrite(segmentA,LOW);
        digitalWrite(segmentB,LOW);
        digitalWrite(segmentC,LOW);
        digitalWrite(segmentD,HIGH);
        digitalWrite(segmentE,LOW);
        digitalWrite(segmentF,LOW);
        digitalWrite(segmentG,HIGH);
        delay(4);
        }
        EEPROM.write(2, 1);
        goto aman;
      }
    }
    timecc=value*100+value2;
    while(digitalRead(enter)==HIGH){
      timecc=value*100+value2;
      showNum(timecc);
      if(digitalRead(hoursButton)==LOW){
        mati();
        delay(200);
        value++;
        if(value>23){
          value=00;
        }
         EEPROM.write(0, value);
      }

      if(digitalRead(minsButton)==LOW){
        mati();
        delay(200);
        value2++;
        if(value2>59){
          value2=00;
        }
        EEPROM.write(1, value2);
      }
    }
    mati();
    delay(200);
  }
if(EEPROM.read(2)==1){
  
  if(value==hours&&value2==mm){
    for(int a=0;a<56;a++){
      showNum(timecc);
     if(a>0&&a<4){
      digitalWrite(buzer,HIGH);
     } 
     else if(a>4&&a<8){
      digitalWrite(buzer,LOW); 
     }
     else if(a>8&&a<12){
      digitalWrite(buzer,HIGH); 
     }
     else if(a>12&&a<16){
      digitalWrite(buzer,LOW); 
     }
     else if(a>16&&a<20){
      digitalWrite(buzer,HIGH); 
     }
     else if(a>20&&a<24){
      digitalWrite(buzer,LOW); 
     }
     else if(a>24&&a<28){
      digitalWrite(buzer,HIGH); 
     }
     else if(a>28&&a<56){
      digitalWrite(buzer,LOW); 
     }
    }

  }
}
  

} 


void showNum(int n) {

  
  mati();
  int n1 = (n%10000)/1000;
  if (n > 999) {
    printNumber(n1,1);
  }
  delay(4);
mati();
 
  int n2 = (n%1000)/100;
  printNumber(n2,2); 
  delay(4);
mati();
 
  int n3 = (n%100)/10;
  printNumber(n3,3);
  delay(4);
mati();
 
  int n4 = n%10;
  printNumber(n4,4); 
  delay(3);

}

void printNumber(int n, int d) {

  
  if(d==1)  {
    digitalWrite(digitOne,HIGH);
    digitalWrite(digitTwo,LOW);
    digitalWrite(digitThree,LOW);
    digitalWrite(digitFour,LOW);

    
    if ((hours == 12) || (hours == 22) || (hours == 23)) {
   
    } 
    else {
     
    }
  } 

  if(d==2) {
    digitalWrite(digitOne,LOW);
    digitalWrite(digitTwo,HIGH);
    digitalWrite(digitThree, LOW);
    digitalWrite(digitFour,LOW);

   
    if ((hours > 12) && (hours < 22)) {
   
    } 
    else {
      
    }
  }

  if(d==3) {
    digitalWrite(digitOne,LOW);
    digitalWrite(digitTwo,LOW);
    digitalWrite(digitThree,HIGH);
    digitalWrite(digitFour,LOW);
  
  }

  if(d==4) {
    digitalWrite(digitOne,LOW);
    digitalWrite(digitTwo,LOW);
    digitalWrite(digitThree,LOW);
    digitalWrite(digitFour,HIGH);
   
  }

//removed case

 
byte decToBcd(byte val) {
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val) {
  return ( (val/16*10) + (val%16) );
}

void updateRTC() {
  Wire.beginTransmission(104);
  Wire.write(0);
  Wire.write(decToBcd(ss));
  Wire.write(decToBcd(mm));
  Wire.write(decToBcd(hours));
  Wire.endTransmission();
}
void mati(){
    digitalWrite(segmentA,HIGH);
    digitalWrite(segmentB,HIGH);
    digitalWrite(segmentC,HIGH);
    digitalWrite(segmentD,HIGH);
    digitalWrite(segmentE,HIGH);
    digitalWrite(segmentF,HIGH);
    digitalWrite(segmentG,HIGH);
}

I Removed Case Program to decrease characters

byte value;
byte value2;
byte value3;

Do you count ‚Äúuh-huh, two, three‚ÄĚ? Or do you count ‚Äú1, 2, 3‚ÄĚ? Why number variables inconsistently?

        goto aman;

goto somewhere, yourself. There is NO excuse for using goto in any Arduino sketch.

     else if(a>4&&a<8){

Getyourspacekeyfixed.

    if ((hours == 12) || (hours == 22) || (hours == 23)) {
   
    }
    else {
     
    }

What the heck?

I might be missing something, but I don’t see where you use ss (seconds) anywhere. Specifically, I don’t see where you show ss on the seven segment display.

PaulS:

byte value;

byte value2;
byte value3;



Do you count "uh-huh, two, three"? Or do you count "1, 2, 3"? Why number variables inconsistently?



goto aman;



goto somewhere, yourself. There is NO excuse for using goto in any Arduino sketch.



else if(a>4&&a<8){



Getyourspacekeyfixed.



if ((hours == 12) || (hours == 22) || (hours == 23)) {
 
    }
    else {
   
    }



What the heck?

I might be missing something, but I don't see where you use ss (seconds) anywhere. Specifically, I don't see where you show ss on the seven segment display.

sorry for that code i got from google, but i don’t know how to add seconds

sorry for that code i got from google

Did you search for "Arduino + crappy code"?

but i don't know how to add seconds

But you do know how to add hours and minutes? The process would be exactly the same, except which 7 segment displays to show the value on. If you show hours on 1 and 2, and minutes on 3 and 4, then show seconds on 5 and 6.

PaulS:
Did you search for ‚ÄúArduino + crappy code‚ÄĚ?
But you do know how to add hours and minutes? The process would be exactly the same, except which 7 segment displays to show the value on. If you show hours on 1 and 2, and minutes on 3 and 4, then show seconds on 5 and 6.

mati();
  int n1 = (n%10000)/1000;
  if (n > 999) {
    printNumber(n1,1);
  }
  delay(4);
mati();
 
  int n2 = (n%1000)/100;
  printNumber(n2,2); 
  delay(4);
mati();
 
  int n3 = (n%100)/10;
  printNumber(n3,3);
  delay(4);
mati();
 
  int n4 = n%10;
  printNumber(n4,4); 
  delay(3);
mati();

  int n5 = (n%10)/10;
  printNumber(n5,5); 
  delay(2);
mati();
 
  int n6 = n%1;
  printNumber(n6,6); 
  delay(1);

i already found this code used to display numbers but i’m still don’t know how to add seconds, i add n5 and n6. but it only show 00.sorry for asking you too much

but it only show 00.sorry for asking you too much

That snippet of code seems to break up n into 6 pieces, and show each piece on a separate 7 segment display. What value is n supposed to hold?

  timecc = hh*100+mm;
  showNum(timecc);

If you want to display seconds, too, it seems to me that this is a good place to make a change. Do you understand what multiplying hh by 100 does? Can you see what to do to move the value 4 places, instead? Do you know what to do to move minutes two places? Can you see how to add seconds?

Start by changing:

  timecc = hh*100+mm;

to:

  timecc = (hh*100 + mm)*100 + ss;

You would expect this to give you a 6-digit number from 000000 to 235959. But a six digit number is larger than 32767 so it won't fit in an int variable on an Arduino UNO. You should change the data type to something larger:

unsigned long timecc;

and do the math in unsigned long integers:

  timecc = (hh * 100UL + mm) * 100UL + ss;

Then in showNum() you have to split off each of the six digits and send then to printNumber().

To eliminate ghosting you have to turn off all of the digit pins before you change the segment pins. If you set the segment pins while a digit pin is still set you will see those segments light up dimly on the wrong digit.

Streuth, I haven't seen goto used in a program for many decades.

Perhaps find a better starting point, that code uses gotos and doesn't use arrays where that
would be obviously right...

Here is an example I wrote of how to use arrays and bit masks to multiplex a multi-digit seven-segment display. Perhaps it will be of some use.

// Bit maps for the seven segment display
// Segment Cathodes (0/LOW for on, 1/HIGH for off)
const unsigned char 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);
  }

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

  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 100 seconds, then minutes:seconds
    clock = (seconds % 100) * 100 + (hundredths % 100);
  else if (minutes < 100)
    // Display minutes:seconds up to 100 minutes, 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);
  }
}

MarkT:
Streuth, I haven’t seen goto used in a program for many decades.

Perhaps find a better starting point, that code uses gotos and doesn’t use arrays where that
would be obviously right…

+1. Don’t hamstring yourself by starting out copying code that looks like it was written by a three year old. That is NOT code you want to learn from.