How to set the RTC DS1307 in circuit?

Hey guys! How’re you doing today?

I need help to set the clock in circuit, that is, using a couple pushbuttons to do it…

I already have the clock running.

I create the SetClock and SetDate functions but no matter what I can’t figure out how to do it.

Can you help me out?

I also accept some coding tips :smiley:

I used the SetClockButtonState variable (which can be 0, 1 or 2) to change from DisplayClock, SetClock, SetDate.

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib

#include <Wire.h>
#include <LiquidCrystal.h>
#include "RTClib.h"

#define DS1307_I2C_ADDRESS 0x68 // each I2C object has a unique bus address, the DS1307 is 0x68

RTC_DS1307 RTC;

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// Create custom char
byte graus[8] = {
  0b01110,
  0b10001,
  0b10001,
  0b01110,
  0b00000,
  0b00000,
  0b00000,
  0b00000
};

unsigned char pin=0; // analog pin
int temp=0, Vin=0, j=0;
float minimum = (-424.0/6.25), samples[20];

const int SetClockButton = 9;     // Pushbutton switch between: display clock/date/temp or set clock or set date
int SetClockButtonState = 0;      // Current state of the button
const int ChangeClockButton = 8;  // Pushbutton to choose what to change - second, minute, hour, day, month or year.


void setup ()
{
  Serial.begin(57600);
  Wire.begin();
  RTC.begin();
  lcd.begin(16, 2);   
  lcd.createChar(0, graus);
  analogReference(INTERNAL);

  // initialize the pushbutton pin as an input:
  pinMode(SetClockButton, INPUT);  
  pinMode(ChangeClockButton, INPUT); 

  // following line sets the RTC to the date & time this sketch was compiled 
  // RTC.adjust(DateTime(__DATE__, __TIME__));

  if (! RTC.isrunning()) 
  {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled only if clock is not running.
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }  

  // Square-wave output
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0x07); // move pointer to SQW address
  Wire.write(0x13); // sends 0x13 (hex) 00010011 (binary)
  Wire.endTransmission();

}


void loop () 
{

  while (SetClockButtonState == 0)
  {
    WriteClockOnLCD();
    WriteDateOnLCD();
    WriteWeekDayOnLCD();
    WriteTemperatureOnLCD();
    //PrintmiliVoltOnLCD();

    if (digitalRead(SetClockButton) == 0)
    {     
      delay(250);
      if (digitalRead(SetClockButton) == 1)
      {
        lcd.clear();    
        SetClockButtonState=1;
      }
    }
  }

  while (SetClockButtonState == 1)
  {
    SetClock();
  }

  while (SetClockButtonState == 2)
  {
    SetDate();
  }

}


// Print time on LCD
void WriteClockOnLCD ()
{
  lcd.home();    
  DateTime now = RTC.now();
 
  if (now.hour() < 10)
  {
    lcd.print('0'); 
    lcd.print(now.hour(), DEC); 
    lcd.print(':');
  }
  else
  {
    lcd.print(now.hour(), DEC); 
    lcd.print(':');
  }

  if (now.minute() < 10)
  {
    lcd.print('0'); 
    lcd.print(now.minute(), DEC); 
    lcd.print(':');
  }
  else
  {
    lcd.print(now.minute(), DEC); 
    lcd.print(':');
  }

  if (now.second() < 10)
  {
    lcd.print('0'); 
    lcd.print(now.second(), DEC);
  }  
  else 
  {
    lcd.print(now.second(), DEC);
  }
}


// Print date on LCD
void WriteDateOnLCD()
{
  
  DateTime now = RTC.now();
  lcd.setCursor(0,1);

  if (now.day() < 10)
  {
    lcd.print('0'); 
    lcd.print(now.day(), DEC); 
    lcd.print('/');
  }
  else
  {
    lcd.print(now.day(), DEC); 
    lcd.print('/');
  }


  switch(now.month())
  {
  case 1:
    lcd.print("Jan/"); 
    break;     
  case 2:
    lcd.print("Feb/"); 
    break;
  case 3:
    lcd.print("Mar/"); 
    break;
  case 4:
    lcd.print("Apr/"); 
    break;
  case 5:
    lcd.print("May/"); 
    break;
  case 6:
    lcd.print("Jun/"); 
    break;
  case 7:
    lcd.print("Jul/"); 
    break;
  case 8:
    lcd.print("Aug/"); 
    break;     
  case 9:
    lcd.print("Sep/"); 
    break;
  case 10:
    lcd.print("Oct/"); 
    break;
  case 11:
    lcd.print("Nov/"); 
    break;
  case 12:
    lcd.print("Dec/"); 
    break;
  default:
    lcd.print("Err/");
  }

  lcd.print(now.year(), DEC); 
}


// Print weekday on LCD
void WriteWeekDayOnLCD()
{
 
  DateTime now = RTC.now();
  lcd.setCursor(13, 1);
  switch(now.dayOfWeek())
  {
  case 0:
    lcd.print("Sun"); 
    break;     
  case 1:
    lcd.print("Mon"); 
    break;
  case 2:
    lcd.print("Tue"); 
    break;
  case 3:
    lcd.print("Wed"); 
    break;
  case 4:
    lcd.print("Thu"); 
    break;
  case 5:
    lcd.print("Fri"); 
    break;
  case 6:
    lcd.print("Sat"); 
    break;
  default:
    lcd.print("Err");
  }
}


// Print temperature on LCD
void WriteTemperatureOnLCD()
{
  Vin=0;
  temp = 0;
  j=0;

  for(j=0 ; j<=19 ; j++)
  {
    samples[j] = (analogRead(pin)*(1100.0/1073.0));
    Vin = Vin + samples[j];
  }

  temp = (Vin/20 /1024.0 * 1073.0 / 6.25) + minimum;

  if (temp >=0 & temp <=9)
  {
    lcd.setCursor(11, 0);
    lcd.print("  "); 
    lcd.print(temp, DEC);    
  }

  if (temp >=10 & temp <=99)
  {
    lcd.setCursor(11, 0);
    lcd.print(" "); 
    lcd.print(temp, DEC);
  }

  if (temp >=100 & temp <=150)
  {
    lcd.setCursor(11, 0); 
    lcd.print(temp, DEC);
  }

  if (temp >=-9 & temp <=-1)
  {
    lcd.setCursor(11, 0);
    lcd.print(" "); 
    lcd.print(temp, DEC);
  }

  if (temp >=-99 & temp <=-10)
  {
    lcd.setCursor(11, 0);
    lcd.print(temp, DEC);
  }

  lcd.setCursor(14, 0);
  lcd.write((uint8_t)0);
  lcd.print("C");
}


// Print temperature on LCD
void PrintmiliVoltOnLCD()
{
  lcd.setCursor(8, 0);
  lcd.print(Vin/20, DEC);
  //lcd.print("mV");
}


void SetClock()
{
  DateTime now = RTC.now();
  //lcd.clear();
  lcd.home();  
  lcd.print("SET CLOCK");

  if (digitalRead(SetClockButton) == 0)
  {     
    delay(250);
    if (digitalRead(SetClockButton) == 1)
    {
      lcd.clear();    
      SetClockButtonState=2;
    }
  }
}


void SetDate()
{
  DateTime now = RTC.now();
  //lcd.clear();
  lcd.home();  
  lcd.print("SET DATE");

  if (digitalRead(SetClockButton) == 0)
  {     
    delay(250);
    if (digitalRead(SetClockButton) == 1)
    {
      lcd.clear();    
      SetClockButtonState=0;
    }
  }


}
  while (SetClockButtonState == 0)

I think you need to actually read the state of the button... In fact, I know that you do.

while(digitalRead(SetClockButton) == LOW)
{
   // Do something
}

PaulS:   while (SetClockButtonState == 0)

I think you need to actually read the state of the button... In fact, I know that you do.

while(digitalRead(SetClockButton) == LOW)
{
   // Do something
}

Thanks for the tip ;)

However, the problem is not the changing... This is working perfeclty :)

The problem is how to set the clock in circuit!

Regards

The problem is how to set the clock in circuit!

Sorry, I do not know what you are asking here...

PaulS:

The problem is how to set the clock in circuit!

Sorry, I do not know what you are asking here...

I know how to set the clock using the computer... that is, after each compiling, the clock is set the with the values I want.

However, I want to add a feature: set the clock while it is running. I can use 2 or 3 pushbuttons for that, but I don't know the function that can change the clock.

I think that this setup is related to something like that:

Example for minute change:

if (button pressed)
RTC.adjust(DateTime(year(), month(), day(), hour(); minute()+1; second()));

that is, after each compiling, the clock is set the with the values I want.

You can use that same process at any time. It doesn't have to be called just in setup().

However, I want to add a feature: set the clock while it is running. I can use 2 or 3 pushbuttons for that, but I don't know the function that can change the clock.

Search this forum. Someone else did much the same thing quite recently.

Thanks. I'll do the search :)