I have the code below and it works to a point. I can get it to display the time and date but unable to change any of the setting. The code is an amalgamation of two programs, an RTC sketch and Encoder sketch. I think I have most things in their but need some guidance on how to change setting of the RTC. Would really appreciate help on this.
// Example 54.1 - PCF8563 RTC write/read demonstration
#include "Wire.h"
#define PCF8563address 0x51
#include <LiquidCrystal.h>
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12 );
const int ledPin = 13; // the pin that the LED is attached to
// encoder settings
const int EncoderPushButton = 45; // the pin that the pushbutton is attached to
const int encoder0PinA = 2;
const int encoder0PinB = 3;
int Rotor = 0;
int encoder0PinALast = LOW;
int n = HIGH;
// Variables will change: Push Buttons
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
String days[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
//Binary coded decimal calculation
byte bcdToDec(byte value)
{
return ((value / 16) * 10 + value % 16);
}
byte decToBcd(byte value){
return (value / 10 * 16 + value % 10);
}
void setup()
{
Wire.begin();
//encoder pins
pinMode (encoder0PinA,INPUT);
pinMode (encoder0PinB,INPUT);
pinMode(EncoderPushButton, INPUT);// initialize the button pin as a input:
pinMode(ledPin, OUTPUT);// initialize the LED as an output:
lcd.begin(16, 2);
// change the following to set your initial time
second = 0;
minute = 42;
hour = 20;
dayOfWeek = 6;
dayOfMonth = 24;
month = 8;
year = 13;
// comment out the next line and upload again to set and keep the time from resetting every reset
//setPCF8563();
}
void loop()
{
LcdDisplayTime();
if (buttonPressed()){
dealWithButton();
}
// encoder
n = digitalRead(encoder0PinA);
if ((encoder0PinALast == HIGH) && (n == LOW)) {
if (digitalRead(encoder0PinB) == LOW) {
Rotor--;
}
else {
Rotor++;
}
lcd.print (Rotor);
}
encoder0PinALast = n;
delay(1000);// delay for clock seconds
}
boolean buttonPressed(){
boolean wasPressed;
// read the state of the pushbutton value:
buttonState = digitalRead(EncoderPushButton);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
//digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
void dealWithButton(){
// read the EncoderPushButton input pin:
buttonState = digitalRead(EncoderPushButton);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, SetClock();
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
digitalWrite(ledPin, HIGH);
SetClock();
}
else {
// if the current state is LOW then the button
// wend from on to off:
LcdDisplayTime();
digitalWrite(ledPin, LOW);
}
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
void LcdDisplayTime(){
readPCF8563();//get time from pcf8563 then format it below
// date
lcd.clear();
lcd.setCursor(2,1);
lcd.print(days[dayOfWeek]);
lcd.print(" ");
lcd.print(dayOfMonth, DEC);
lcd.print("/");
lcd.print(month, DEC);
lcd.print("/");
lcd.print(year, DEC);
//time
lcd.setCursor(4,0);
lcd.print(hour, DEC);
lcd.print(":");
if (minute < 10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second < 10)
{
lcd.print("0");
}
lcd.print(second, DEC);
}
void setPCF8563()
// this sets the time and date to the PCF8563
{
Wire.beginTransmission(PCF8563address);
Wire.write(0x02);
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void readPCF8563()
// this gets the time and date from the PCF8563
{
Wire.beginTransmission(PCF8563address);
Wire.write(0x02);
Wire.endTransmission();
Wire.requestFrom(PCF8563address, 7);
second = bcdToDec(Wire.read() & B01111111); // remove VL error bit
minute = bcdToDec(Wire.read() & B01111111); // remove unwanted bits from MSB
hour = bcdToDec(Wire.read() & B00111111);
dayOfMonth = bcdToDec(Wire.read() & B00111111);
dayOfWeek = bcdToDec(Wire.read() & B00000111);
month = bcdToDec(Wire.read() & B00011111); // remove century bit, 1999 is over
year = bcdToDec(Wire.read());
}
void SetClock() {
// This is the user interface routine for setting the clock.
// Here's where we set the hour
Rotor = hour + 48; // The extra value on rotor allows easier turn-back.
while (!buttonPressed()) {
// delay(UIDelay);
hour = Rotor % 24;
lcd.setCursor(4,0);
lcd.print(hour, DEC);
lcd.print(":");
if (minute < 10)
{
lcd.print("0");
}
lcd.print(minute, DEC);
lcd.print(":");
if (second < 10)
{
lcd.print("0");
}
lcd.print(second, DEC);
lcd.cursor();
}
// Here we set the minute
Rotor = minute + 120; // The extra value on rotor allows easier turn-back.
while (!buttonPressed()) {
minute = Rotor % 60;
lcd.setCursor(3,0);
lcd.cursor();
}
// Here's where we set the seconds
Rotor = second + 120; // The extra value on rotor allows easier turn-back.
while (!buttonPressed()) {
second = Rotor % 60;
lcd.setCursor(6,0);
lcd.cursor();
}
// Here we set the year
Rotor = year;
while (!buttonPressed()) {
year = Rotor;
lcd.setCursor(10,0);
lcd.cursor();
}
while (!buttonPressed()) {
lcd.setCursor(13,0);
lcd.cursor();
}
// Set the day of the week
Rotor = dayOfWeek + 16; // The extra value on rotor allows easier turn-back.
while (!buttonPressed()) {
dayOfWeek = constrain((Rotor % 8), 1, 7);
lcd.setCursor(0,1);
lcd.cursor();
}
// Set the Month
Rotor = month + 26; // The extra value on rotor allows easier turn-back.
while (!buttonPressed()) {
month = constrain((Rotor % 13),1,12);
lcd.setCursor(11,1);
lcd.cursor();
}
// Set the Date
Rotor = dayOfMonth;
while (!buttonPressed()) {
switch (month) {
case 1://jan
case 3://mar
case 5://may
case 7://jul
case 8://aug
case 10://oct
case 12://Dec
dayOfMonth = Rotor % 32;
break;
case 4://apr
case 6://jun
case 9://sep
case 11://nov
dayOfMonth = Rotor % 31;
break;
case 2://feb
dayOfMonth = Rotor % 30;
break;
}
if (dayOfMonth==0) {
dayOfMonth++;
}
lcd.setCursor(14,1);
lcd.cursor();
}
// Turn cursor off
lcd.noCursor();
// Save the new stuff
setPCF8563();//year, month, dayOfWeek, dayOfMonth, hour, minute, second);//0x00);
}