Turn on realy in every hour with rtc using arduino

i want to turn on realy in every hour using rtc DS1307, but want to stop it between 10pm to 8am (night sleeping time). please help me to creat that code....

There should be examples available that came with the RTC library.

Should find them under examples.

BTW, a DS3231 is a much much better RTC.

Where are you stuck ?
Can you read the time from the RTC and print it ?

using this code i want to turn on a realy in every hour using RTC DS1307 with a inturrept between 10pm to 7am. please help to modify this code...

#define RELAY1 8 //Relay1
#define RELAY2 7 //Relay2
#define an_hour 60000UL // 1 second = 1000 milliseconds,,1 minute = 60 seconds,,1 hour = 60 minutes,,12 hours = 12 * 60 * 60 * 1000 = 43,200,000
unsigned long startTime;
void setup()

{
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
startTime = millis();

}

void loop()

{
if (millis() - startTime > an_hour)
{
digitalWrite(RELAY2,LOW); // Turns ON Relays 1
delay(10000); // Wait 2 seconds

digitalWrite(RELAY1,LOW); // Turns ON Relays 1
delay(20000); // Wait 2 seconds

digitalWrite(RELAY2,HIGH); // Turns Relay Off
delay(5000);

digitalWrite(RELAY1,HIGH); // Turns Relay Off
//delay(2000);

startTime = millis();
}
}

using this code i want to turn on a realy in every hour for only 2 seconds using RTC DS1307 with a inturrept between 10pm to 7am. please help to modify this code...

#define RELAY1 8 //Relay1
#define RELAY2 7 //Relay2
#define an_hour 60000UL // 1 second = 1000 milliseconds,,1 minute = 60 seconds,,1 hour = 60 minutes,,12 hours = 12 * 60 * 60 * 1000 = 43,200,000
unsigned long startTime;
void setup()

{
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
digitalWrite(RELAY1, HIGH);
digitalWrite(RELAY2, HIGH);
startTime = millis();

}

void loop()

{
if (millis() - startTime > an_hour)
{
digitalWrite(RELAY2,LOW); // Turns ON Relays 1
delay(10000); // Wait 2 seconds

digitalWrite(RELAY1,LOW); // Turns ON Relays 1
delay(20000); // Wait 2 seconds

digitalWrite(RELAY2,HIGH); // Turns Relay Off
delay(5000);

digitalWrite(RELAY1,HIGH); // Turns Relay Off
//delay(2000);

startTime = millis();
}
}

Are you going to use the RTC ?

If yes, look at the RTC library examples and see if you can come up with a rudimentary sketch.

NEVER ever use delay( ) in your sketches (unless you know what ramifications it causes).

Does this sketch work with your DS1307 RTC connected ?

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

byte lastReadSecond;

//******************************************************************************************
void setup ()
{
  Serial.begin(9600);
  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    // rtc.adjust(DateTime(2021, 9, 28, 11, 18, 0));
  }

} //END of setup()


//******************************************************************************************
void loop ()
{
  DateTime now = rtc.now();

  if (lastReadSecond != now.second())
  {
    //update to the new second
    lastReadSecond = now.second();

    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(" (");
    Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
    Serial.print(") ");
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
  }

} //END of loop()

//******************************************************************************************



Then try:

//
//  Version   YY/MM/DD     Comments
//  1.01      21/09/26     Running code

//https://forum.arduino.cc/t/turn-on-realy-in-every-hour-with-rtc-using-arduino/909683?u=larryd

#include <Wire.h>
#include "RTClib.h"       //https://github.com/adafruit/RTClib            Using the DS1307 RTC

#define RELAYon             LOW
#define RELAYoff            HIGH

#define RELAY1              8
#define RELAY2              7

RTC_DS1307 rtc;

int lastSeconds           = 0;


//******************************************************************************************
void setup()
{
  pinMode(RELAY1, OUTPUT);
  digitalWrite(RELAY1, RELAYoff);

  pinMode(RELAY2, OUTPUT);
  digitalWrite(RELAY2, RELAYoff);

  Serial.begin(9600);

  if (! rtc.begin())
  {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (! rtc.isrunning())
  {
    Serial.println("RTC is NOT running!");
    
    // following line sets the RTC to the date & time this sketch was compiled
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    
    // This line sets the RTC with an explicit date & time, for example to set
    //                     YYYY MM  DD  hh  mm ss
    // rtc.adjust(DateTime(2021, 9, 28, 11, 18, 0));
  }

} //END of setup()


//******************************************************************************************
void loop()
{
  //Read the RTC 
  DateTime now = rtc.now();

  //*********************************************************
  //when seconds has changed, should we turn ON/OFF the relay ?
  if (lastSeconds != now.second())
  {
    //update to the new seconds value
    lastSeconds = now.second();

    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();

    //***********************************
    //from 8AM to 9PM inclusive
    if (now.hour() > 7 && now.hour() < 22 && now.minute() == 0)
    {
      //**************
      //at 0 seconds the relays turn ON
      if (now.second() == 0)
      {
        Serial.println("Relay ON");

        digitalWrite(RELAY1, RELAYon);
        digitalWrite(RELAY2, RELAYon);
      }

      //**************
      //at 2 seconds the relays turn OFF
      else if (now.second() == 2)
      {
        Serial.println("Relay OFF");

        digitalWrite(RELAY1, RELAYoff);
        digitalWrite(RELAY2, RELAYoff);
      }

    } //END of   if (now.hour() > 7 && now.hour() < 22)

  } //END of   if (lastSeconds != now.second())

} //END of loop()


//******************************************************************************************


thanks for replying . this code works perfectly. but now i want to use this code in my 7 segment led clock, please help me to combine this code with my led clock code...

/*
4 digit 7 segment display: 7-Segment Display - 4-Digit (Red) - COM-09483 - SparkFun Electronics
Datasheet: http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf
*/
// modified connexion by niq_ro from http://nicuflorica.blogspot.com
// dataseet: http://www.tme.eu/ro/Document/dfc2efde2e22005fd28615e298ea2655/KW4-563XSA.pdf
// Code modified by BMIAK Basnayaka
// http://www.setnfix.com

int digit1 = 12;
int digit2 = 11;
int digit3 = 10;
int digit4 = 9;

//Pin mapping from Arduino to the ATmega DIP28 if you need it
//http://www.arduino.cc/en/Hacking/PinMapping
int segA = 0; //Display pin 0
int segB = 1; //Display pin 1
int segC = 2; //Display pin 2
int segD = 3; //Display pin 3
int segE = 4; //Display pin 4
int segF = 5; //Display pin 5
int segG = 6; //Display pin 6
int segDP =13; // Display pin 13

#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/
// add part with SQW=1Hz from Tutorial: Arduino and the I2C bus – Part One | tronixstuff.com
// add part with manual adjust Arduino Interfacing DS1307 Real Time Clock

byte SW0 = A0;
byte SW1 = A2;
byte SW2 = A1;
byte SWT = A3;
int Adhr=0;
int Admnt=0;
int D = 0;
int Z =0;

// use for hexa in zecimal conversion
long zh, uh, ore;
long zm, um, miniti;

void setup() {

//Serial.begin(57600);
Wire.begin();
RTC.begin();
// RTC.adjust(DateTime(F(DATE), F(TIME)));
// if you need set clock... just remove // from line above this

// part code for flashing LED
Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW address
// Wire.write(0x00); // turns the SQW pin off
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave at 1Hz
// Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary) 32kHz

Wire.endTransmission();

pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(segDP, OUTPUT);

pinMode(digit1, OUTPUT);
pinMode(digit2, OUTPUT);
pinMode(digit3, OUTPUT);
pinMode(digit4, OUTPUT);

//Serial.begin(9600);

pinMode(SW0, INPUT); // N.O. push button switch
pinMode(SW1, INPUT); // N.O. push button switch
pinMode(SW2, INPUT); // N.O. push button switch
pinMode(SWT, INPUT); // N.O. push button switch

digitalWrite(SW0, HIGH); // pull-ups on
digitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
digitalWrite(segDP, LOW);

}

void loop() {

DateTime now = RTC.now();
long HR = now.hour()1000;
long timp = (HR
10)+now.minute()*100+now.second();
Adhr = now.hour();
Admnt=now.minute();

int DIM = 0;
if (timp >=250000)
timp=timp-240000;
//------------------------------------------------------
//12/24 hours shitching
//------------------------------------------------------
if (timp < 130000){
digitalWrite(segDP, LOW);}

if (digitalRead(SWT)==0){delay(300);
if (D == 0) {
D =1;delay(200);}

else{
D =0;}
}

if (D == 0){
if (timp>=130000){
timp=timp-120000;
digitalWrite(segDP, HIGH);}
}

if (timp>=130000){
digitalWrite(segDP, LOW);
}

if ((D==1)& (timp <130000))
digitalWrite(segDP, LOW);

//------------------------------------------------------

//int timp = (now.minute(), DEC);
// displayNumber(12); // this is number to diplay

for(int i = 100 ; i >0  ; i--) {
 if (timp >= 100000) displayNumber01(timp); 
 else displayNumber02(timp); 

}

for(int i = 100 ; i >0 ; i--) {
if (timp >= 100000) displayNumber03(timp);
else displayNumber04(timp);
}

for(int i = 100 ; i >0 ; i--) {
if (timp >= 100000) displayNumber05(timp);
else displayNumber06(timp);
}

if (digitalRead(SW0)==0){delay(100);Z=1; set_time(); }// hold the switch to set time
}

void set_time() {
byte minutes1 = 0;
byte hours1 = 0;
byte minutes = 0;
byte hours = 0;
hours=Adhr;
minutes=Admnt;

if ((hours & 0x0f) > 9) hours =  hours + 6;
  if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 12:00 (With PM LED on) or 24:00
    //if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 0:00
    if ((minutes & 0x0f) > 9) minutes = minutes + 6;
  if (minutes > 0x59) minutes = 0;

while (!(Z==0)){ // set time switch must be released to exit

int TST = digitalRead(SW2);
while (TST==0) // set hours
{
hours++;

// converting hexa in zecimal:
zh = hours / 16;
uh = hours - 16 * zh ;
ore = 10 * zh + uh;
zm = minutes / 16;
um = minutes - 16 * zm ;
miniti = 10 * zm + um;

  if ((hours & 0x0f) > 9) hours =  hours + 6;
        if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 12:00 (With PM LED on) or 24:00
    //if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 0:00
    if (hours <= 9) 
delay(1);

  for(int i = 400 ; i >0  ; i--) {
 displayNumber01(ore*10000+miniti*100); 
 }

TST = digitalRead(SW2);
}

while (!(digitalRead(SW1))) // set minutes
{
minutes++;
// converting hexa in zecimal:
zh = hours / 16;
uh = hours - 16 * zh ;
ore = 10 * zh + uh;
zm = minutes / 16;
um = minutes - 16 * zm ;
miniti = 10 * zm + um;

 for(int i = 400 ; i >0  ; i--) {
 displayNumber01(ore*10000+miniti*100); 
 }

  if ((minutes & 0x0f) > 9) minutes = minutes + 6;
  if (minutes > 0x59) minutes = 0;
  if (minutes >= 9) 
delay(1);    
}

Wire.beginTransmission(0x68); // activate DS1307
Wire.write(0); // where to begin
Wire.write(0x00);          //seconds
Wire.write(minutes);          //minutes
Wire.write(0x80 | hours);    //hours (24hr time)
Wire.write(0x06);  // Day 01-07
Wire.write(0x01);  // Date 0-31
Wire.write(0x05);  // month 0-12
Wire.write(0x09);  // Year 00-99
Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. 
Wire.endTransmission();

// converting hexa in zecimal:
zh = hours / 16;
uh = hours - 16 * zh ;
ore = 10 * zh + uh; 
zm = minutes / 16;
um = minutes - 16 * zm ;
miniti = 10 * zm + um; 



for(int i = 400 ; i >0  ; i--) {
 displayNumber01(ore*10000+miniti*100); 
 }

delay(100);
//Serial.print(digitalRead(SW0));
if (digitalRead(SW0)==0) Z = 0;
delay(300);
}
//Serial.print(SW2);
}

void displayNumber01(long toDisplay) {
#define DISPLAY_BRIGHTNESS 25

#define DIGIT_ON HIGH
#define DIGIT_OFF LOW

for(int digit = 6 ; digit > 0 ; digit--) {

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
 digitalWrite(digit1, DIGIT_ON);
 
  break;

case 2:
digitalWrite(digit2, DIGIT_ON);
//digitalWrite(segDP, LOW);
break;
case 3:
digitalWrite(digit3, DIGIT_ON);

  break;
case 4:
  digitalWrite(digit4, DIGIT_ON);
  
  break;

}
lightNumber(toDisplay % 10);
toDisplay /= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS); 


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

}
}

void displayNumber02(long toDisplay) {
#define DISPLAY_BRIGHTNESS 25

#define DIGIT_ON HIGH
#define DIGIT_OFF LOW

for(int digit = 6 ; digit > 0 ; digit--) {

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
 lightNumber(10); 
 
 break;

case 2:
digitalWrite(digit2, DIGIT_ON);
//digitalWrite(segDP, LOW);
break;
case 3:
digitalWrite(digit3, DIGIT_ON);

  break;
case 4:
  digitalWrite(digit4, DIGIT_ON);
  
  break;
 
}
lightNumber(toDisplay % 10);
toDisplay /= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

}
}

void displayNumber03(long toDisplay) {
#define DISPLAY_BRIGHTNESS 25

#define DIGIT_ON HIGH
#define DIGIT_OFF LOW

for(int digit = 6 ; digit > 0 ; digit--) {

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
 digitalWrite(digit1, DIGIT_ON);
 
  break;

case 2:
digitalWrite(digit2, DIGIT_ON);

  break;
case 3:
  digitalWrite(digit3, DIGIT_ON);
  
  break;
case 4:
  digitalWrite(digit4, DIGIT_ON);
  
  break;

}
lightNumber(toDisplay % 10);
toDisplay /= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

}
}

void displayNumber04(long toDisplay) {
#define DISPLAY_BRIGHTNESS 25

#define DIGIT_ON HIGH
#define DIGIT_OFF LOW

for(int digit = 6 ; digit > 0 ; digit--) {

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
 lightNumber(10); 
 
 break;

case 2:
digitalWrite(digit2, DIGIT_ON);

  break;
case 3:
  digitalWrite(digit3, DIGIT_ON);
  
  break;
case 4:
  digitalWrite(digit4, DIGIT_ON);
  
  break;

}
lightNumber(toDisplay % 10);
toDisplay /= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

}
}

void displayNumber05(long toDisplay) {
#define DISPLAY_BRIGHTNESS 25

#define DIGIT_ON HIGH
#define DIGIT_OFF LOW

for(int digit = 6 ; digit > 0 ; digit--) {

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
 digitalWrite(digit1, DIGIT_ON);
 
  break;

case 2:
digitalWrite(digit2, DIGIT_ON);

  break;
case 3:
  digitalWrite(digit3, DIGIT_ON);
  
  break;
case 4:
  digitalWrite(digit4, DIGIT_ON);
  
  break;

}
lightNumber(toDisplay % 10);
toDisplay /= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

}
}

void displayNumber06(long toDisplay) {
#define DISPLAY_BRIGHTNESS 25

#define DIGIT_ON HIGH
#define DIGIT_OFF LOW

for(int digit = 6 ; digit > 0 ; digit--) {

//Turn on a digit for a short amount of time
switch(digit) {
case 1:
 //digitalWrite(digit1, DIGIT_ON);
 
  break;

case 2:
digitalWrite(digit2, DIGIT_ON);

  break;
case 3:
  digitalWrite(digit3, DIGIT_ON);
  
  break;
case 4:
  digitalWrite(digit4, DIGIT_ON);
  
  break;

}
lightNumber(toDisplay % 10);
toDisplay /= 10;
delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

}
}
//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

//Common Anode *************************
#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;

// all segment are ON
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;

}
// End of the coding, BUDHUSARANAI, Best of luck.

}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

/*
 4 digit 7 segment display: http://www.sparkfun.com/products/9483
 Datasheet: http://www.sparkfun.com/datasheets/Components/LED/7-Segment/YSD-439AR6B-35.pdf
  */
// modified connexion by niq_ro from http://nicuflorica.blogspot.com
// dataseet: http://www.tme.eu/ro/Document/dfc2efde2e22005fd28615e298ea2655/KW4-563XSA.pdf
// Code modified by BMIAK Basnayaka
// http://www.setnfix.com

int digit1 = 12; 
int digit2 = 11;
int digit3 = 10; 
int digit4 = 9;

//Pin mapping from Arduino to the ATmega DIP28 if you need it
//http://www.arduino.cc/en/Hacking/PinMapping
int segA = 0; //Display pin 0
int segB = 1; //Display pin 1
int segC = 2; //Display pin 2
int segD = 3; //Display pin 3
int segE = 4; //Display pin 4
int segF = 5; //Display pin 5
int segG = 6; //Display pin 6
int segDP =13; // Display pin 13



#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
// original sketck from http://learn.adafruit.com/ds1307-real-time-clock-breakout-board-kit/
// add part with SQW=1Hz from http://tronixstuff.wordpress.com/2010/10/20/tutorial-arduino-and-the-i2c-bus/
// add part with manual adjust http://www.bristolwatch.com/arduino/arduino_ds1307.htm



byte SW0 = A0;
byte SW1 = A2;
byte SW2 = A1;
byte SWT = A3;
int Adhr=0;
int Admnt=0;
int D = 0;
int Z =0;

// use for hexa in zecimal conversion
long zh, uh, ore;
long zm, um, miniti;

void setup() {
  
 //Serial.begin(57600);
  Wire.begin();
  RTC.begin();
 // RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
// if you need set clock... just remove // from line above this

// part code for flashing LED
Wire.beginTransmission(0x68);
Wire.write(0x07); // move pointer to SQW address
// Wire.write(0x00); // turns the SQW pin off
 Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave at 1Hz
// Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary) 32kHz

Wire.endTransmission();
  
  
  pinMode(segA, OUTPUT);
  pinMode(segB, OUTPUT);
  pinMode(segC, OUTPUT);
  pinMode(segD, OUTPUT);
  pinMode(segE, OUTPUT);
  pinMode(segF, OUTPUT);
  pinMode(segG, OUTPUT);
  pinMode(segDP, OUTPUT);

  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);
  pinMode(digit3, OUTPUT);
  pinMode(digit4, OUTPUT);
 


//Serial.begin(9600);

pinMode(SW0, INPUT);  // N.O. push button switch
pinMode(SW1, INPUT);  // N.O. push button switch
pinMode(SW2, INPUT);  // N.O. push button switch
pinMode(SWT, INPUT);  // N.O. push button switch

digitalWrite(SW0, HIGH); // pull-ups on
digitalWrite(SW1, HIGH);
digitalWrite(SW2, HIGH);
digitalWrite(segDP, LOW);


}


void loop() {

  DateTime now = RTC.now();
  long HR = now.hour()*1000;
  long timp = (HR*10)+now.minute()*100+now.second();
  Adhr = now.hour();
  Admnt=now.minute();



int DIM = 0;
if (timp >=250000)
timp=timp-240000;
//------------------------------------------------------
//12/24 hours shitching 
//------------------------------------------------------
if (timp < 130000){
digitalWrite(segDP, LOW);}


if (digitalRead(SWT)==0){delay(300);
 if (D == 0) {
 D =1;delay(200);}
 
else{
D =0;}
}




if (D == 0){  
if (timp>=130000){
timp=timp-120000;
digitalWrite(segDP, HIGH);}
}

if (timp>=130000){ 
digitalWrite(segDP, LOW);
}

if ((D==1)& (timp <130000))
digitalWrite(segDP, LOW);





//------------------------------------------------------


  



//int timp = (now.minute(), DEC);
//   displayNumber(12); // this is number to diplay


     
    for(int i = 100 ; i >0  ; i--) {
     if (timp >= 100000) displayNumber01(timp); 
     else displayNumber02(timp); 
   } 

   for(int i = 100 ; i >0  ; i--) {
     if (timp >= 100000) displayNumber03(timp); 
     else displayNumber04(timp); 
   } 

   for(int i = 100 ; i >0  ; i--) {
     if (timp >= 100000) displayNumber05(timp); 
     else displayNumber06(timp); 
   } 


  

if (digitalRead(SW0)==0){delay(100);Z=1;  set_time(); }// hold the switch to set time
}

void set_time()   {
  byte minutes1 = 0;
  byte hours1 = 0;
  byte minutes = 0;
  byte hours = 0;
  hours=Adhr;
  minutes=Admnt;


    if ((hours & 0x0f) > 9) hours =  hours + 6;
      if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 12:00 (With PM LED on) or 24:00
        //if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 0:00
        if ((minutes & 0x0f) > 9) minutes = minutes + 6;
      if (minutes > 0x59) minutes = 0;
      
while (!(Z==0)){ // set time switch must be released to exit


 


    
   int TST = digitalRead(SW2);
    while (TST==0) // set hours
    { 
     hours++;          
     
   // converting hexa in zecimal:
    zh = hours / 16;
    uh = hours - 16 * zh ;
    ore = 10 * zh + uh; 
    zm = minutes / 16;
    um = minutes - 16 * zm ;
    miniti = 10 * zm + um; 
    
   
   
      
      if ((hours & 0x0f) > 9) hours =  hours + 6;
            if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 12:00 (With PM LED on) or 24:00
        //if (hours > 0x24) hours = 1; // Mid night 12.00 will show as 0:00
        if (hours <= 9) 
    delay(1);
    
      for(int i = 400 ; i >0  ; i--) {
     displayNumber01(ore*10000+miniti*100); 
     }
    
    TST = digitalRead(SW2);
    }

   while (!(digitalRead(SW1))) // set minutes
    { 
     minutes++;  
   // converting hexa in zecimal:
    zh = hours / 16;
    uh = hours - 16 * zh ;
    ore = 10 * zh + uh; 
    zm = minutes / 16;
    um = minutes - 16 * zm ;
    miniti = 10 * zm + um; 
    
     for(int i = 400 ; i >0  ; i--) {
     displayNumber01(ore*10000+miniti*100); 
     }

      if ((minutes & 0x0f) > 9) minutes = minutes + 6;
      if (minutes > 0x59) minutes = 0;
      if (minutes >= 9) 
    delay(1);    
    }

    Wire.beginTransmission(0x68); // activate DS1307
    Wire.write(0); // where to begin
    Wire.write(0x00);          //seconds
    Wire.write(minutes);          //minutes
    Wire.write(0x80 | hours);    //hours (24hr time)
    Wire.write(0x06);  // Day 01-07
    Wire.write(0x01);  // Date 0-31
    Wire.write(0x05);  // month 0-12
    Wire.write(0x09);  // Year 00-99
    Wire.write(0x10); // Control 0x10 produces a 1 HZ square wave on pin 7. 
    Wire.endTransmission();
  
    // converting hexa in zecimal:
    zh = hours / 16;
    uh = hours - 16 * zh ;
    ore = 10 * zh + uh; 
    zm = minutes / 16;
    um = minutes - 16 * zm ;
    miniti = 10 * zm + um; 

 
    
    for(int i = 400 ; i >0  ; i--) {
     displayNumber01(ore*10000+miniti*100); 
     }
delay(100); 
//Serial.print(digitalRead(SW0));
if (digitalRead(SW0)==0) Z = 0;
 delay(300);   
  }
//Serial.print(SW2);
}



void displayNumber01(long toDisplay) {
#define DISPLAY_BRIGHTNESS  25

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 6 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
     digitalWrite(digit1, DIGIT_ON);
     
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      //digitalWrite(segDP, LOW);
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      
      break;
   
    }
    lightNumber(toDisplay % 10);
    toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 


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

void displayNumber02(long toDisplay) {
#define DISPLAY_BRIGHTNESS  25

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 6 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
     lightNumber(10); 
     
     break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      //digitalWrite(segDP, LOW);
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      
      break;
     
    }
    lightNumber(toDisplay % 10);
    toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

void displayNumber03(long toDisplay) {
#define DISPLAY_BRIGHTNESS  25

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 6 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
     digitalWrite(digit1, DIGIT_ON);
     
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      
      break;
    
    }
    lightNumber(toDisplay % 10);
    toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

void displayNumber04(long toDisplay) {
#define DISPLAY_BRIGHTNESS  25

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 6 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
     lightNumber(10); 
     
     break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      
      break;
   
    }
    lightNumber(toDisplay % 10);
    toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

void displayNumber05(long toDisplay) {
#define DISPLAY_BRIGHTNESS  25

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 6 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
     digitalWrite(digit1, DIGIT_ON);
     
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      
      break;
   
    }
    lightNumber(toDisplay % 10);
    toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

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

void displayNumber06(long toDisplay) {
#define DISPLAY_BRIGHTNESS  25

#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW

  for(int digit = 6 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
     //digitalWrite(digit1, DIGIT_ON);
     
      break;
   case 2:
      digitalWrite(digit2, DIGIT_ON);
      
      break;
    case 3:
      digitalWrite(digit3, DIGIT_ON);
      
      break;
    case 4:
      digitalWrite(digit4, DIGIT_ON);
      
      break;
   
    }
    lightNumber(toDisplay % 10);
    toDisplay /= 10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 

     //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);
   
}
} 
//Given a number, turns on those segments
//If number == 10, then turn off number
void lightNumber(int numberToDisplay) {

//Common Anode *************************
#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;

  // all segment are ON
  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;
  
  }
// End of the coding, BUDHUSARANAI, Best of luck.
  
}

this is the full code

So we see.

But please go back and edit each of your previous posts to correctly put the code in code tags. :face_with_raised_eyebrow:

Otherwise people start to wonder whether they will ever get to the end. :grin:

fortunately I have got it... I have successfully compile these two code..