7segment common cathode display with rtc

Hi everyone, i am developing a project where it has 4 digit 7segment common cathode display. The project works when there is a signal from machine, it will start the stopwatch and when the machine pause/stop, the stopwatch will pause and continue back when the signal is high...first i used manual count by integrating a code but its is not very accurate..the offset is very obvious...i decided to use rtc and i have ds1307 (tiny rtc module)...can anyone help me on how to write the code for rtc module where we can get the elapsed time?

Welcome to the forum!

Did you notice what a mess the forum software makes of your code?

Please format/indent the code correctly in Arduino IDE, using CTRL-T, and use code tags ("</>" editor button) when posting. You can edit the post to replace the code.

#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 = 9;

int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;

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.
long m = 0;
int x = 100;
int y = 60;

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
int seconds = 0, minutes = 0, hours = 0;

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);

  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()
{
  // 1. Load number 1

  int digit_value_1 = seconds % 10;
  int digit_value_2 = (seconds / 10) % 10;

  // 2. Load number 2

  int digit_value_3 = (minutes) % 10;
  int digit_value_4 = (minutes / 10) % 10;


  //3. Update number to dispaly
  pickDigit(0);//Light up 7-segment display d1
  pickNumber(digit_value_1);// get the value of thousand
  delay(del);//delay 5ms

  pickDigit(1);//Light up 7-segment display d2
  pickNumber(digit_value_2);// get the value of hundred
  delay(del);//delay 5ms

  pickDigit(2);//Light up 7-segment display d3
  pickNumber(digit_value_3);//get the value of ten
  delay(del);//delay 5ms

  pickDigit(3);//Light up 7-segment display d4
  pickNumber(digit_value_4);//Get the value of single digit
  delay(del);//delay 5ms

  delay(2);//loop delay update every 20 ms

}

void pickDigit(int x) //light up a 7-segment display
{
  switch (x)
  {
    case 0:
      digitalWrite(d1, HIGH); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 1:
      digitalWrite(d1, LOW); digitalWrite(d2, HIGH); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 2:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, HIGH); digitalWrite(d4, LOW);
      break;

    case 3:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, HIGH);
      break;

    default:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
  }
}

void pickNumber(int x)
{
  switch (x)
  {
    case 0:
      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;
    default:
      zero();
      break;
  }
}

void clearLEDs()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
  digitalWrite(p, HIGH);
}

void zero() //display 0
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

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

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

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

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

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

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

void eight() //display 8
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void nine() //display 9
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void add()
{
  if (count < 10)
  {

    count ++;
  }

  else
  {
    count = 0;
    if (seconds < 59) seconds++;

    else
    {
      seconds = 0;
      if (minutes < 60)minutes++;

      else
      {
        minutes = 0;
        if (hours < 24)hours++;
        else hours = 0;
      }
    }
  }

}

Thank you jremington. I correct it

Thanks!

For simple timing, use the built in function millis(). I think it is more accurate than what you have now.

For keeping track of time and date and formatting it properly, the Arduino time library TimeLib.h is pretty good. Depending on the clock accuracy you need, you may not need an RTC, but one is easy to integrate if you later decide to do so. I do NOT recommend the DS1307, the DS3231 is much, much more accurate.

Learn how the time library works by reading the documentation and trying some of the examples built into the library.

https://www.arduino.cc/reference/en/libraries/time/

1 Like

Thank you jremington

hi everyone, i developed a programmed where the count up will start when the machine runs..with the same arduino, i have to make a countup for downtime as well when the machine stops..im so confused on how to code this...i have done for the runtime part, can anyone help me how to code for both runtime and downtime using the same arduino?

#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 machine = 9;

int d4 = 10;
int d3 = 11;
int d2 = 12;
int d1 = 13;

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.
long m = 0;
int x = 100;
int y = 60;

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
int seconds = 0, minutes = 0, hours = 0;

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(machine, INPUT);


  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()
{
  int digit_value_1;
  int digit_value_2;
  int digit_value_3;
  int digit_value_4; 
  if (digitalRead(machine) == HIGH)
  {
    // 1. Load number 1

    digit_value_1 = seconds % 10;
    digit_value_2 = (seconds / 10) % 10;

    // 2. Load number 2

    digit_value_3 = (minutes) % 10;
    digit_value_4 = (minutes / 10) % 10;
    }

    //3. Update number to display
    pickDigit(0);//Light up 7-segment display d1
    pickNumber(digit_value_1);// get the value of thousand
    delay(del);//delay 5ms

    pickDigit(1);//Light up 7-segment display d2
    pickNumber(digit_value_2);// get the value of hundred
    delay(del);//delay 5ms

    pickDigit(2);//Light up 7-segment display d3
    pickNumber(digit_value_3);//get the value of ten
    delay(del);//delay 5ms

    pickDigit(3);//Light up 7-segment display d4
    pickNumber(digit_value_4);//Get the value of single digit
    delay(del);//delay 5ms

    delay(1);//loop delay update every 1 ms
}

void pickDigit(int x) //light up a 7-segment display
{
  switch (x)
  {
    case 0:
      digitalWrite(d1, HIGH); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 1:
      digitalWrite(d1, LOW); digitalWrite(d2, HIGH); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
    case 2:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, HIGH); digitalWrite(d4, LOW);
      break;

    case 3:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, HIGH);
      break;

    default:
      digitalWrite(d1, LOW); digitalWrite(d2, LOW); digitalWrite(d3, LOW); digitalWrite(d4, LOW);
      break;
  }
}

void pickNumber(int x)
{
  switch (x)
  {
    case 0:
      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;
    default:
      zero();
      break;
  }
}

void clearLEDs()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void zero() //display 0
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

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

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

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

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

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

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

void eight() //display 8
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void nine() //display 9
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void add()
{
  if (digitalRead(machine)==HIGH)
  {
  if (count < 10)
  {

    count ++;
  }

  else
  {
    count = 0;
    if (seconds < 59) seconds++;

    else
    {
      seconds = 0;
      if (minutes < 59)minutes++;

      else
      {
        minutes = 0;
        if (hours < 23)hours++;
        else hours = 0;
      }
    }
  }
  }
}

this is the schematic diagram that i want to code using arduino mega

There is no point helping you with the code, because the circuit you plan to use will damage the Arduino.

I would suggest you use a max7219 chip to drive your 2 displays. It is easier if you use common cathode displays with this chip.

Once the circuit is fixed, we can return to your code problem.

hi sir, the display board has all the drivers...initially it has pic16f883 and i removed it to use arduino to drive it...i attached the display board for you...the DIG0-DIG7 represents the digit and the abcdefgdp are as usual...Have a look sir

Please post an accurate and complete schematic showing the circuits on that board and how you intend to connect it to the Arduino.

I don't want to waste my time and your time helping you with code unless I can be sure the circuit will not damage the Arduino. Your attempts at schematics give me zero confidence that the Arduino will be safe. Good luck with your project. Perhaps other forum members will be willing to risk their time helping you.

Sir, please refer the code i have attached and the diagram i attached, the code works for single display and runs without any problem for 1 week. now i need to do for 2nd display...i can use another arduino, but i decided to use one arduino

can you evaluate if the display is common anode or common cathode? Have you already tried to drive one single segment?
what's the polarity - meaning in the sense of the former PIC digit/ABCD pins . which one must be HIGH, which one must be low to enable a digit?

sir it is common cathode and pic digit should be high and abcd should be low to enable a number

ok and the "runtime" part ist already working - the display shows the right digits?

1 Like

yes sir...now we just need the downtime....i can use the same code and program on another arduino since the idea is the same only...but im trying to save cost by using one arduino...im not sure how to compile the program because we cant have two void loop() in same program

instead of putting all the code in loop make to functions
one for doRuntime(), one for doDowntime()
and in loop you call either the first or the other function depending on your criterion.

I'm just gessing - it seems to be that
if (digitalRead(machine) == HIGH)

add an else and call the doDowntime() instead

Hi,
Are the displays on the other side of the PCB you posted, or do they plug into the two vertical 9pin connectors?

Thanks.. Tom... :smiley: :+1: :coffee: :australia: