Problem with clock

Hey everyone!
I have been having a problem with my code
I want to be able to set and hour and run a clock in my code so that it activates a LED
I have no idea how to program a clock.
I have been reading on how to make alarm clocks but the code does not work when I test it
Thank you to everyone in advance!
Plz help :smiling_face_with_tear:

It is hard to help you fix your code if we can't see it!

...and we have no idea what board(s) and display you have, or how you connected them.

1 Like

Welcome!

What clock are you using?

This is my code

int IN1 = 8;
int IN2 = 7;

int h=0;
int m=0;
int s=0;


void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}


void loop()
{ 

 s=s+1; 
 
 if(s==60){
  s=0;
  m=m+1;
 }
 if(m==60)
 {
  m=0;
  h=h+1;
 }
 if(h==24)
 {
  h=0;
 } 
 
if(h==1)
{
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
delay(5000);
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
delay(5000);
 }
else{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}

You can use my code as an example.

// Clock Project Version 1.5

// Use 4 digit 7 segment commun anode with a double dot
// with 2 button for time setting

// setting the digits pins

byte dig_pin[4] = {5,4,3,2};

// Pin 2 - Hours ( tens ) Pin 2 - Hours - Pin 4 - Minutes ( tens ) Pin 5 - minutes
// Zero will turn on the digit


// setting the segments pins A B C D E F G
byte segs[7] = {12,11,10,9,8,7,6};

// Segments that make each number in this format ABCDEFG
// A one will turn on the segment
// The array is : 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - all off
 
const byte numbers[11] = { B1111110, B0110000, B1101101, B1111001,
B0110011, B1011011, B1011111, B1110010, B1111111, B11111011, B0000000 }; 

// A double led that flash every second

byte second_pin = 14;

// the two push button switches
// Button to set the time

byte select_pin = 15;
byte set_pin = 16;

// the button states
boolean select_state;
boolean set_state;

// State of the second pin
boolean second_state;

byte digit[4];

// time to display the digit
// set 5 millisecond - 5

const unsigned long display_time = 3;

byte hours;
byte minutes;
byte second_delay_count;

unsigned int minute_delay_count;

// The variable to set for a second delay
const byte second_delay =84;

// The variable to set for the minute counter
const unsigned int minute_delay = 4996;

unsigned long check_time;
unsigned long check_time_difference;
unsigned long time_check;

const unsigned long minute_count = 60000;
const unsigned long max_value = 4294967295;

void setup()
{
  // debug

  // Serial.begin(9600);
  
  // Setup the output pins
  
  for (byte i=0;i<7;i++)
  {
    pinMode(segs[i], OUTPUT);
  }
  for (byte i=0;i<4;i++)
  {
    pinMode(dig_pin[i], OUTPUT);
  }
  
  pinMode(second_pin, OUTPUT);
  
  for (byte i=0;i<4;i++)
  {
    digitalWrite(dig_pin[i], HIGH);
  }

  // Setting the clock buttons.
  // setup the input pins

  pinMode(select_pin, INPUT);
  pinMode(set_pin, INPUT);

  // Init the second to zero and the state of the second pin

  second_delay_count = 0;
  second_state = true;

  minute_delay_count = 0;

  digitalWrite(second_pin, HIGH);

  // Set the time to 00:00

  hours = 0;
  minutes = 0;
  
  // Set the state of the select and set button

  select_state = true;
  set_state = true;

  check_time = millis();

 }

void loop() 
{
    // Check if the setup button is press

    select_state = digitalRead(select_pin);
    
    if (select_state == false)
    {
       time_setup();     
    }
  
    // Convert the hours and minutes for display
    time_digit_converter();
    
    // Display the hours and minutes    
    seg_display();

    // Display the second double LEDs

    if (second_delay_count >= second_delay)
    {
        if (second_state == true)
        {
           digitalWrite(second_pin, LOW);
        }
        else 
        {
           digitalWrite(second_pin, HIGH);
        }

        // Reset the second count counter and change second state
        
        second_delay_count = 0;
        second_state = !second_state;         
                
    }   

    second_delay_count++;

    time_check = millis();

    if (time_check < check_time)
    {
      check_time_difference = (max_value - check_time) + time_check;      
    }

    if (time_check > check_time)
    {
       check_time_difference = time_check - check_time;
    }   

    if (check_time_difference >= minute_count)
    {
        // minute_delay_count = 0;
      check_time = millis();
      minutes++;
      if (minutes == 60)
          {
            minutes = 0;
            hours++;
            if (hours == 24)
            {
              hours = 0;
            }
          }
    }

    // minute_delay_count++;

    // Debug
   // Serial.print(millis());
   // Serial.print(' ');
  
}

void time_digit_converter()
{
   digit[0]  = hours / 10;
   digit[1] = hours % 10;
   digit[2] = minutes / 10;
   digit[3] = minutes % 10;   
}

void seg_display()
{
  
     boolean bits;

     for (byte j=0;j<4;j++)
     {            
       digitalWrite(dig_pin[j], LOW);    
       for (byte i=0;i<7;i++)
       {
          bits = bitRead(numbers[digit[j]], i);
          digitalWrite(segs[i], bits);
       }
       delay(display_time);
       digitalWrite(dig_pin[j], HIGH);
     } 

}

void time_setup()
{ 
   // re-state the button to it original state

  select_state = true;
   
  delay(25);
  
   // Blank the display
   for (byte i=0;i<4;i++)
   {
     digit[i] = 10;
   }

   seg_display();

   delay(100);
  
   // Flash the second double leds

   digitalWrite(second_pin, HIGH);
   delay(250);
   digitalWrite(second_pin, LOW);
   delay(250);
   digitalWrite(second_pin, HIGH);
   delay(100);
     
   // Display the numbers and stay until the select button is press

   while (select_state == true)

   {
      time_digit_converter();
      seg_display();

      // Check if the set button is press

      set_state = digitalRead(set_pin);

      // if set button is press, minutes and hours will increament until the right time is setup 

      if (set_state == false)
      {
         delay(2);
         minutes++;
         if (minutes == 60)
          {
            minutes = 0;
            hours++;
            if (hours == 24)
            {
              hours = 0;
            }
          }
      } 

      // Reset the set state button
      
      set_state = true;

      // Press the select button to escape the loop

      select_state = digitalRead(select_pin);
      
    }
    
   // Return the select state to it original state

   select_state = true;
   delay(250);
  //  minute_delay_count = 0;
  check_time = millis();
   
}  

Just bear in mind, the input switches are "a bit quick"

1 Like

Try adding a one second delay:

int IN1 = 8;
int IN2 = 7;

int h = 0;
int m = 0;
int s = 0;


void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}


void loop()
{
  delay(1000);  // wait one second (approx)
  s = s + 1;

  if (s >= 60) {
    s = 0;
    m = m + 1;
  }
  if (m >= 60)
  {
    m = 0;
    h = h + 1;
  }
  if (h >= 24)
  {
    h = 0;
  }

  if (h == 1)
  {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    delay(5000);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    delay(5000);
  }
  else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }
}

The way you test it, you have to wait an hour for the indicator IN1 to come on.

1 Like

Add the delay(1000); like @anon57585045 shows.

And to test, you could temporarily change the blinking part to

  if ((m % 5) == 0)
  {
    digitalWrite(IN1, HIGH);
    digitalWrite(IN2, LOW);
    delay(100);
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
    delay(100);
  }
  else {
    digitalWrite(IN1, LOW);
    digitalWrite(IN2, LOW);
  }

to blink for a minute every five minutes. Life too short to wait hours for tests.

When it works, change the blinking to whatever you want.

And then move on to a better clock code. Yours is fun, but will be inaccurate, especially when you take 10 seconds to blink, that hour will last, well, a very long time.

The above blinking idea would make the minute that it blinks take more like 1 minute 12 seconds because of the extra time you waste.

Yes, that can be fixed. Yes, probably with code that looks entirely different, and certainly makes no use of delay().

Look forward to it. Save this project to look back on and marvel at your progress!

a7

1 Like

Thanks for your help!
It works know!
:smile:

Thanks!
It works!
:smile: :+1:

Thanks!
I'm going to try it out!
:smile:

Here my version 1 of my clock project using 4 7 segments display.
I was using "counting the main loop" method. It count for every minute. So in one minute, how many time the main loop is execute. But with that method, it is not accurate. The clock will be late ( about 30 min ) per 24 Hr. Version 1.5 use milli method. I set up to check for 1 minute, but I have to solve when millis value reach max value of a unsigned long, and the next millis check reset to 0, when it take the difference, that will not work out, so I have to check that out in the eventuality. The main issue with ver 1 is the delay when it display the numbers. and still the same issue with ver 1.5 but a bit more accurate and less late. Version 2, a more accurate, I use the RTC DS1307. It got an 1 Hz pulse output - use for the seconds leds, time can be kept when the power runout and when setting the clock for Day Time Saving or the other, I easily setup the time by increment / decrement. But it is using 3 button instead of two - increment only.

Here the Version 1

// Clock Project Version 1.0

// Use 4 digit 7 segment commun anode with a double dot
// with 2 button for time setting

// setting the digits pins

byte dig_pin[4] = {5,4,3,2};

// Pin 2 - Hours ( tens ) Pin 2 - Hours - Pin 4 - Minutes ( tens ) Pin 5 - minutes
// Zero will turn on the digit


// setting the segments pins A B C D E F G
byte segs[7] = {12,11,10,9,8,7,6};

// Segments that make each number in this format ABCDEFG
// A one will turn on the segment
// The array is : 0 - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 - all off
 
const byte numbers[11] = { B1111110, B0110000, B1101101, B1111001,
B0110011, B1011011, B1011111, B1110010, B1111111, B11111011, B0000000 }; 

// A double led that flash every second

byte second_pin = 14;

// the two push button switches
// Button to set the time

byte select_pin = 15;
byte set_pin = 16;

// the button states
boolean select_state;
boolean set_state;

// State of the second pin
boolean second_state;

byte digit[4];

// time to display the digit
// set 5 millisecond - 5

const unsigned long display_time = 3;

byte hours;
byte minutes;
byte second_delay_count;

unsigned int minute_delay_count;

// The variable to set for a second delay
const byte second_delay =84;

// The variable to set for the minute counter
const unsigned int minute_delay = 4996;

void setup()
{
  // debug

  // Serial.begin(9600);
  
  // Setup the output pins
  
  for (byte i=0;i<7;i++)
  {
    pinMode(segs[i], OUTPUT);
  }
  for (byte i=0;i<4;i++)
  {
    pinMode(dig_pin[i], OUTPUT);
  }
  
  pinMode(second_pin, OUTPUT);
  
  for (byte i=0;i<4;i++)
  {
    digitalWrite(dig_pin[i], HIGH);
  }

  // Setting the clock buttons.
  // setup the input pins

  pinMode(select_pin, INPUT);
  pinMode(set_pin, INPUT);

  // Init the second to zero and the state of the second pin

  second_delay_count = 0;
  second_state = true;

  minute_delay_count = 0;

  digitalWrite(second_pin, HIGH);

  // Set the time to 00:00

  hours = 0;
  minutes = 0;
  
  // Set the state of the select and set button

  select_state = true;
  set_state = true;

 }

void loop() 
{
    // Check if the setup button is press

    select_state = digitalRead(select_pin);
    
    if (select_state == false)
    {
       time_setup();     
    }
  
    // Convert the hours and minutes for display
    time_digit_converter();
    
    // Display the hours and minutes    
    seg_display();

    // Display the second double LEDs

    if (second_delay_count >= second_delay)
    {
        if (second_state == true)
        {
           digitalWrite(second_pin, LOW);
        }
        else 
        {
           digitalWrite(second_pin, HIGH);
        }

        // Reset the second count counter and change second state
        
        second_delay_count = 0;
        second_state = !second_state;         
                
    }   

    second_delay_count++;

    if (minute_delay_count >= minute_delay)
    {
      minute_delay_count = 0;
      minutes++;
      if (minutes == 60)
          {
            minutes = 0;
            hours++;
            if (hours == 24)
            {
              hours = 0;
            }
          }
    }

    minute_delay_count++;

    // Debug
   // Serial.print(millis());
   // Serial.print(' ');
  
}

void time_digit_converter()
{
   digit[0]  = hours / 10;
   digit[1] = hours % 10;
   digit[2] = minutes / 10;
   digit[3] = minutes % 10;   
}

void seg_display()
{
  
     boolean bits;

     for (byte j=0;j<4;j++)
     {            
       digitalWrite(dig_pin[j], LOW);    
       for (byte i=0;i<7;i++)
       {
          bits = bitRead(numbers[digit[j]], i);
          digitalWrite(segs[i], bits);
       }
       delay(display_time);
       digitalWrite(dig_pin[j], HIGH);
     } 

}

void time_setup()
{ 
   // re-state the button to it original state

  select_state = true;
   
  delay(25);
  
   // Blank the display
   for (byte i=0;i<4;i++)
   {
     digit[i] = 10;
   }

   seg_display();

   delay(100);
  
   // Flash the second double leds

   digitalWrite(second_pin, HIGH);
   delay(250);
   digitalWrite(second_pin, LOW);
   delay(250);
   digitalWrite(second_pin, HIGH);
   delay(100);
     
   // Display the numbers and stay until the select button is press

   while (select_state == true)

   {
      time_digit_converter();
      seg_display();

      // Check if the set button is press

      set_state = digitalRead(set_pin);

      // if set button is press, minutes and hours will increament until the right time is setup 

      if (set_state == false)
      {
         delay(2);
         minutes++;
         if (minutes == 60)
          {
            minutes = 0;
            hours++;
            if (hours == 24)
            {
              hours = 0;
            }
          }
      } 

      // Reset the set state button
      
      set_state = true;

      // Press the select button to escape the loop

      select_state = digitalRead(select_pin);
      
    }
    
   // Return the select state to it original state

   select_state = true;
   delay(250);
   minute_delay_count = 0;
}  


And I have Version 2

// get the Wire.h library 
#include <Wire.h>

// Set the output pins
byte dig_pin[4] = {2,3,4,5}; // Digit 1, Digit 2, Digit 3, Digit 4 - Minutes , 10's minutes, Hours, 10's hours 
byte segs[7] = {12,11,10,9,8,7,6}; // Segment a,b,c,d,e,f,g
// byte second_pin = 17; // The Double dot on the display - The dot of Digit 3
// Set the input pins
const byte set_button_pin = 16; // Press this button to set the clock
const byte go_up_pin = 15; // Press this button to increment the time
const byte go_down_pin = 14; // Press this button to decrement the time

boolean set_button; // setup button state

// RTC DS1307 Data setup
// Format : BCD format - Example : 23 --> B00100011
// Turn the chip on / off and Seconds - Minutes - Hours - Day of the week - Date - Month - Year - Output pulse setup of the RTC chip
/*  Location 0x00 - 0xxxxxxx - Enable oscillator - Seconds - from 0 to 59 - BCD format xxx xxxx
 *        Default - 1xxxxxxx - Disable oscillator - Seconds - from 0 to 59 - BCD format xxx xxxx
 * Location 0x01 - 0xxxxxxx - Minutes - from 0 to 59 - BCD format - xxx xxxx               
 * Location 0x02 - 00xxxxxx - 24 hr setting - Hours - from 0 to 23 - BCD format xx xxxx - Default
 *                 010xxxxx - 12 hr setting - AM - Hours - from 1 to 12 - BCD format x xxxx
 *                 011xxxxx  - 12 hr setting - PM - Hours - from 1 to 12 - BCD format x xxxx
 * Location 0x03 - 00000xxx - Day of the week - Sunday - 1 to Saturday - 7 - BCD format xxx                 
 * Location 0x04 - 00xxxxxx - Date - from 1 to 31 - BCD format xx xxxx
 * Location 0x05 - 000xxxxx - Month - from 1 to 12 - BCD format x xxxx
 * Location 0x06 - xxxxxxxx - Year - from 0 to 99 - BCD format xxxx xxxx
 * Location 0x07 - 00000011 - Default - No pulse at the SQW/OUT pin 7 of the DS1307 - Logic level Zero
 *                 10000011 - No pulse at the SQW/OUT pin 7 of the DS1307 - Logic level One
 *                 x0010011 - A pulse of 32.768 kHz at the SQW/OUT pin 7 of the DS1307 
 *                 x0010010 - A pulse of 8.192 kHz at the SQW/OUT pin 7 of the DS1307
 *                 x0010001 - A pulse of 4.096 kHz at the SQW/OUT pin 7 of the DS1307
 *                 x0010000 - A pulse of 1 Hz - second pulse - at the SQW/OUT pinn 7 of the DS1307
 */
 // { Enable / Disable Oscillator - Seconds , Minutes , 24/12 AM/PM Hours, Day of the week, Date, Month, Year, Oscilator setup }

// Enter all data in Hexadicimal format 0xXX 
// Minutes - from 0x00 to 0x59
const byte data_minutes = 0x22;
// Hours - 24 hr format from 0x00 to 0x23
const byte data_hours = 0x09;
 // Day of the week from 0x01 to 0x07
 const byte data_week = 0x06;
 /*  Sunday = 1
  *  Monday = 2
  * Tuesday = 3 
  * Wednesday = 4
  * Thursday = 5
  * Friday = 6
  * Saturday = 7
  */
 // Today's date from 0x01 to 0x31
 const byte data_date = 0x05;
 // Month from 0x01 to 0x12
const byte data_month = 0x08;
 // Years from 0x00 to 0x99
const  byte data_year = 0x22; //20XX format
byte rtc_data[8] = {B00000000, data_minutes, data_hours, data_week, data_date, data_month, data_year, B00010000}; 
// The RTC DS1307 address of the device
byte rtc_address = B01101000;
// This information is in the datasheet of the DS1307 - Search : DS1307 datasheet

// Look-up Table of the numbers on the 8 segments display - format - abcdefg
// Depend on the configuration of the circuit and the type of segnment display - Common cathode or Commun Anode
// A one or zero will turn on the segment led.
// The data array is setup for a zero will turn the segment on. A one will turn off the segment.
// The format is : g-f-e-d-c-b-a
const byte numbers[11] = {B1000000, B1111001, B0100100, B0110000, B0011001, B0010010, B0000010, B1011000, B0000000, B0010000, B1111111};
// Digit - 0 - Minutes , Digit - 1 - 10's Minutes, Digit - 2 - Hours, Digit - 3 - 10's hours
byte digit[4];
byte time_data; 

void setup() 
{
  // Init the I2C mode of the Ardiuno and place into the I2C bus.  
  Wire.begin();
  Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
  Wire.write(0x06); // set the register pointer to check the year data
  Wire.endTransmission();
  Wire.requestFrom(rtc_address, 1); // setup to read one byte of the RTC DS1307
  time_data = Wire.read(); // Read the Year data of the RTC chip
  if (time_data == 0x00)//Check for the value zero - Year 2000. Default year of the RTC chip
  {
      // Setup the RTC DS1307 chip and place the proper data.
      Wire.beginTransmission(rtc_address);
      Wire.write(0x00); // Set the data location register pointer to 0x00
      // The device - DS1307 will increament the register pointer from 0x00
      for (byte x = 0; x<8; x++)
      {
         Wire.write(rtc_data[x]);
      }   
      Wire.endTransmission(); 
  }    
  // Setup the segments, digits and seconds pins as OUTPUT 
  for (byte i=0;i<7;i++)
  {
    pinMode(segs[i], OUTPUT);
  }
  for (byte i=0;i<4;i++)
  {
    pinMode(dig_pin[i], OUTPUT);
  }
  // Setup the buttons pins as INPUT
  pinMode(set_button_pin, INPUT);
  pinMode(go_up_pin, INPUT);
  pinMode(go_down_pin, INPUT);

  set_button = true; // set the setup button state
  
}

void loop()
{
   set_button = digitalRead(set_button_pin); // check if the setup button is press
   if (set_button == false)
   {
     time_setup();
   }
   get_time_info(); // sub-routine function to get the time data from the rtc chip
   display_time(); // display the time data
}

void get_time_info()// Sub-routine function to extract the time data from the RTC1307 chip
{
   byte minutes_register = 0x01; // Minutes rtc data location
   byte hours_register = 0x02;  // Hours rtc data location

   Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
   Wire.write(minutes_register); // set the register pointer for the minutes
   Wire.endTransmission();
   Wire.requestFrom(rtc_address, 1); // setup to read one byte of the RTC DS1307
   time_data = Wire.read(); // Place the reading of the minutes data

   digit[0] = time_data & B00001111; // Mask the data to extract unit of minutes
   time_data = time_data >> 4; // Shift the 10's of minutes to 4 bits to the right
   digit[1] = time_data & B00001111; // Mask the data to extract 10's of minutes
    
   Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
   Wire.write(hours_register); // set the register pointer for the hours
   Wire.endTransmission();
   Wire.requestFrom(rtc_address, 1); // setup to read one byte of the RTC DS1307
   time_data = Wire.read(); // Place the reading of the hours data
 
   digit[2] = time_data & B00001111; // Mask the data to extract unit of hours
   time_data = time_data >> 4; // Shift the 10's of hoursto 4 bits to the right
   digit[3] = time_data & B00001111; // Mask the data to extract 10's of hours
   
}

// Display the time numbers on the 7 segments display
void display_time()
{
   boolean bits; // the numbers bits in the data array
   const unsigned long digit_turn_on_time = 3; // time to display in milliseconds

   for (byte j=0;j<4;j++) // four digit to be display
   {            
      digitalWrite(dig_pin[j], LOW); // Turn the digit to display on   
      for (byte i=0;i<7;i++) // turn on 7 segments according to the look-up table
      {
          bits = bitRead(numbers[digit[j]], i); // read the data bit of the numbers array
          digitalWrite(segs[i], bits); // turn on the segment
      }
   delay(digit_turn_on_time); // Display for only a few milliseconds
   digitalWrite(dig_pin[j], HIGH); // Turn off the digit that been display
   }
}

void time_setup()
{
    boolean go_up_state; // Up button state
    boolean go_down_state; // Down button state
    int time_minutes; // Minutes in decimal for calculation - need negative for the decrement routine
    int time_hours; // Hours in decimal for calculation - need negative for the decrement routine

    delay(50); // delay after pressing the setup button
    go_up_state = true; // Init the state of the up button
    go_down_state = true; // Init the state of the down button
    // re-state the button to it original state
    set_button = true;
    // Blank the display
    for (byte i=0;i<4;i++)
    {
       digit[i] = byte(10);// the blank number to be display
    }
    display_time();//display the blank numbers
    delay(1000);// a one second delay to show the blank display
    // halt the RTC chip
    Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
    Wire.write(0x00); // set the register poiter at the oscillator / seconds
    Wire.write(B10000000); // Halt the oscillator
    Wire.endTransmission();
    // Stop the seconds leds
    Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
    Wire.write(0x07); // set the register poiter at the oscillator control bits
    Wire.write(B00000000); // Turn off the second leds
    Wire.endTransmission();
    get_time_info(); // get the rtc time data for BCD to Decimal convertion calculation
    // Conver BCD to decimal
    time_minutes = int((digit[1] * byte(10)) + digit[0]);
    time_hours = int((digit[3] * byte(10)) + digit[2]);
    
    while (set_button == true) // stay in this loop until the setup button is pressed
    {
        display_time(); // display the time being setup
        go_up_state = digitalRead(go_up_pin); // check for up button that is pressed
        go_down_state = digitalRead(go_down_pin); // check for down button that is pressed
        if (go_up_state == false) // the up button is press - increment the minutes and hours.
        {
          time_minutes++;// increment minutes
          if (time_minutes == 60) // check for if over 59
          {
             time_minutes = 0;// reset the minutes
             time_hours++;// increment the hours if over 59
             if (time_hours == 24)// check if over 23
             {
               time_hours = 0;// reset the hours
             }
          }
          // Place into the digit array
          // convert the decimal data into BCD format for display
          digit[3]  = byte(time_hours / 10);
          digit[2] = byte(time_hours % 10);
          digit[1] = byte(time_minutes / 10);
          digit[0] = byte(time_minutes % 10);
        }
        while (digitalRead(go_up_pin) == false)// stay in this loop if the up button is still pressed
        {
           display_time();// display the time setting while waiting for the up button is being release
        }
        if (go_down_state == false)//the down button is pressed. decremant the minutes and hours
        {
           time_minutes--;// decrement minutes
           if (time_minutes == -1)// check if under 0
           {
              time_minutes = 59;// reset the minutes to start at 59
              time_hours--;// decrement the hours if it is under 0
              if (time_hours == -1)// check if under 0
              {
                 time_hours = 23;// reset the hours to start at 23
              }
           }
           // Place into the digit array
           // convert the decimal data into BCD format for display
           digit[3]  = byte(time_hours / 10);
           digit[2] = byte(time_hours % 10);
           digit[1] = byte(time_minutes / 10);
           digit[0] = byte(time_minutes % 10);
        }
        while (digitalRead(go_down_pin) == false)// stay in this loop if the down button is still pressed
        {
           display_time();// display the time setting while waiting for the down button is being release
        }
        go_up_state = true; // Re-state the up button
        go_down_state = true; // Re-state the down button
        set_button = digitalRead(set_button_pin); // Check if the setup button is press       
    }
    set_button = true;// re-state the setup button
    delay(500);// delay to make sure the setup button is release 
    // Convert Decimal into BCD data
    time_minutes = (digit[1] * byte(0x10)) + digit[0];
    time_hours = (digit[3] * byte(0x10)) + digit[2];
    // Setup the minutes
    Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
    Wire.write(0x01); // set the register poiter at the minutes data
    Wire.write(byte(time_minutes)); // Write the minutes data in BCD format
    Wire.endTransmission();
    // Setup the hours
    Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
    Wire.write(0x02); // set the register poiter at the hours data
    Wire.write(byte(time_hours)); // Write the hours data in BCD format
    Wire.endTransmission();
    // Setup to start the oscillator
    Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
    Wire.write(0x00); // set the register poiter at the oscillator / seconds
    Wire.write(B00000000); // Start the oscillator
    Wire.endTransmission();
    // Start the seconds leds
    Wire.beginTransmission(rtc_address);// Address of the RTC DS1307
    Wire.write(0x07); // set the register poiter at the oscillator control bits
    Wire.write(B00010000); // Turn on the second leds
    Wire.endTransmission();
}

Yes it will. If you do it right.

a7

Yes, if you use an RTC, you don't need to worry about cumulative timing delays. As mentioned, millis() timing is not broken when it rolls over to 0, if you do the math right.

My only criticism, you don't need to poll the RTC constantly, at the display update rate. In my clock code, I do that at human readable speed, 10Hz.

If you use a board with wifi, you can get the clocktime from internet.

... the bit banged LED display might be problematic with some processors that do wifi...

Then do not use bit banged display but ordinary :smiley:

In a museum clock I use ic2 lcd to adjust several different settings and for time NTPClient timeClient(ntpUDP); // By default 'pool.ntp.org' is used with 60 seconds update interval and no offset

Sorry, my english is not my native so please do not pin me on language fault

Yes, all my clocks are ESP32 and I2C display based now.

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