//Required libraries
#include <Servo.h>
#include <Wire.h>
#include "RTClib.h"
//Create object for RTC, ||CHANGE TO RTC_DS3231||
RTC_DS3231 rtc;
//Serial system variables
unsigned long SerialStart;
bool SerialConnected = false;
//Time variables
unsigned char secs; //Unpadded seconds
unsigned char mins; //Unpadded minutes
unsigned char hours; //Unpadded hours
unsigned char DayDATE; //Digit day
unsigned char MonthDATE; //Digit month
unsigned int YearDATE; //Digit year
unsigned char DayNUM; //Day of the week number 1 to 7
unsigned char MonthNUM; //Month of the year number 1 to 12
bool is24Hour = false; //Time display mode 24/12 hour
//Servo variables
Servo secs1s; //Servo object for the one's second display
Servo secs10s; //Servo object for the ten's second display
Servo mins1s; //Servo object for the one's minute display
Servo mins10s; //Servo object for the ten's minute display
Servo hours1s; //Servo object for the one's hour display
Servo hours10s; //Servo object for the ten's hour display
unsigned int secs1sANGLE; //Servo angle for the one's second display
unsigned int secs10sANGLE; //Servo angle for the ten's second display
unsigned int mins1sANGLE; //Servo angle for the one's minute display
unsigned int mins10sANGLE; //Servo angle for the ten's minute display
unsigned int hours1sANGLE; //Servo angle for the one's hour display
unsigned int hours10sANGLE; //Servo angle for the ten's hour display
void setup(){
//Serial initialisation or bypass system
Serial.begin(9600);
delay(1000);
Serial.println("Press any key to begin serial monitor");
SerialStart = millis();
while (millis() - SerialStart < 5000) {
if (Serial.available()) {
SerialConnected = true;
Serial.println("Serial monitor started");
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
break;
}
}
if (!SerialConnected) {
Serial.println("Serial monitor disconnected, normal operation will begin now");
Serial.end();
}
Wire.begin();
//Attach servos to relevant pins
secs1s.attach(2);
secs10s.attach(3);
mins1s.attach(4);
mins10s.attach(5);
hours1s.attach(6);
hours10s.attach(7);
//RTC setup
//RTC connection
if (!rtc.begin()) {
if (SerialConnected) {
Serial.println("RTC not found");
}
while (!rtc.begin())
;
}
//Serial
if (SerialConnected) {
delay(300);
Serial.println("RTC found");
delay(300);
Serial.println("RTC connected");
delay(700);
}
//RTC running check and time set
if (!rtc.lostPower()) {
//Serial
if (SerialConnected) {
delay(200);
Serial.println("Saved time not found");
delay(200);
Serial.println("Setting time from device");
delay(600);
}
//Setting time on RTC
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
//Serial
if (SerialConnected) {
DateTime Time = rtc.now();
Serial.print("Time set to: ");
Serial.print(Time.year(), DEC);
Serial.print("/");
Serial.print(Time.month(), DEC);
Serial.print("/");
Serial.print(Time.day(), DEC);
Serial.print(" - ");
Serial.print(Time.hour(), DEC);
Serial.print(":");
Serial.print(Time.minute(), DEC);
Serial.print(":");
Serial.println(Time.second(), DEC);
}
}
}
void loop() {
//Get time from RTC and assign to variables
DateTime Time = rtc.now();
secs = Time.second();
mins = Time.minute();
//Set variable "hours" according to 24/12 hour setting
if (is24Hour) {
hours = Time.hour();
} else {
if (Time.hour() > 12) {
hours = Time.hour() - 12;
} else {
hours = Time.hour();
}
}
//Serial
if (SerialConnected) {
Serial.print(hours);
Serial.print(":");
Serial.print(mins);
Serial.print(":");
Serial.println(secs);
}
//Set angles for each servo
if(secs < 10){
secs1sANGLE = secs;
secs10sANGLE = 0;
}else{
secs1sANGLE = secs % 10;
secs10sANGLE = secs / 10;
}
if (mins < 10){
mins1sANGLE = mins;
mins10sANGLE = 0;
}else{
mins1sANGLE = mins % 10;
mins10sANGLE = mins / 10;
}
if (hours < 0){
hours1sANGLE = hours;
hours10sANGLE = 0;
}else{
hours1sANGLE = hours % 10;
hours10sANGLE = hours / 10;
}
//Map time value to angle value
secs1sANGLE = map(secs1sANGLE, 0, 9, 0, 180);
secs10sANGLE = map(secs10sANGLE, 0, 5, 0, 180);
mins1sANGLE = map(mins1sANGLE, 0, 9, 0, 180);
mins10sANGLE = map(mins10sANGLE, 0, 5, 0, 180);
hours1sANGLE = map(hours1sANGLE, 0, 9, 0, 180);
hours10sANGLE = map(hours10sANGLE, 0, 2, 0, 180);
//Servo angle write
secs1s.write(secs1sANGLE);
secs10s.write(secs10sANGLE);
mins1s.write(mins1sANGLE);
mins10s.write(mins10sANGLE);
hours1s.write(hours1sANGLE);
hours10s.write(hours10sANGLE);
delay(500);
}