//Required libraries
#include <AccelStepper.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
//Display steps variables
unsigned char secs1sPos;
unsigned char secs10sPos;
unsigned char mins1sPos;
unsigned char mins10sPos;
unsigned char hours1sPos;
unsigned char hours10sPos;
const unsigned char StepsPerRev = 200; // Adjust this based on your motor's specs
// Stepper motors (using AccelStepper)
#define secs1sSTEP 2 // Define step pins for the first stepper (seconds 1's)
#define secs1sDIR 8 // Define direction pins for the first stepper
#define secs10sSTEP 3 // Define step pins for the second stepper (seconds 10's)
#define secs10sDIR 9 // Define direction pins for the second stepper
#define mins1sSTEP 4 // Define step pins for the third stepper (minutes 1's)
#define mins1sDIR 10 // Define direction pins for the third stepper
#define mins10sSTEP 5 // Define step pins for the fourth stepper (minutes 10's)
#define mins10sDIR 11 // Define direction pins for the fourth stepper
#define hours1sSTEP 6 // Define step pins for the fifth stepper (hours 1's)
#define hours1sDIR 12 // Define direction pins for the fifth stepper
#define hours10sSTEP 7 // Define step pins for the sixth stepper (hours 10's)
#define hours10sDIR 22 // Define direction pins for the sixth stepper
// Create AccelStepper objects for each motor
AccelStepper secs1s(AccelStepper::DRIVER, secs1sSTEP, secs1sDIR);
AccelStepper secs10s(AccelStepper::DRIVER, secs10sSTEP, secs10sDIR);
AccelStepper mins1s(AccelStepper::DRIVER, mins1sSTEP, mins1sDIR);
AccelStepper mins10s(AccelStepper::DRIVER, mins10sSTEP, mins10sDIR);
AccelStepper hours1s(AccelStepper::DRIVER, hours1sSTEP, hours1sDIR);
AccelStepper hours10s(AccelStepper::DRIVER, hours10sSTEP, hours10sDIR);
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();
// Set stepper motor speeds and acceleration
secs1s.setMaxSpeed(1000);
secs1s.setAcceleration(1000);
secs10s.setMaxSpeed(1000);
secs10s.setAcceleration(1000);
mins1s.setMaxSpeed(1000);
mins1s.setAcceleration(1000);
mins10s.setMaxSpeed(1000);
mins10s.setAcceleration(1000);
hours1s.setMaxSpeed(1000);
hours1s.setAcceleration(1000);
hours10s.setMaxSpeed(1000);
hours10s.setAcceleration(1000);
//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);
}
// Calculate positions for each motor (replace with appropriate steps per revolution)
if(secs % 10 != 0){
secs1sPos = (secs % 10) * (StepsPerRev / 10); // Seconds 1's (motor 1)
}else{
secs1sPos = 200;
}
if(mins % 10 != 0){
mins1sPos = (mins % 10) * (StepsPerRev / 10); // Minutes 1's (motor 3)
}else{
mins1sPos = 200;
}
if(hours % 10 != 0){
hours1sPos = (hours % 10) * (StepsPerRev / 10); // Hours 1's (motor 5)
}else{
hours1sPos = 200;
}
secs10sPos = (secs / 10) * (StepsPerRev / 6); // Seconds 10's (motor 2)
mins10sPos = (mins / 10) * (StepsPerRev / 6); // Minutes 10's (motor 4)
hours10sPos = (hours / 10) * (StepsPerRev / 3); // Hours 10's (motor 6)
secs1sPos = map(secs % 10, 0, 10, 0, 200) % StepsPerRev;
secs10sPos = map(secs / 10, 0, 6, 0, 200) % StepsPerRev;
mins1sPos = map(mins % 10, 0, 10, 0, 200) % StepsPerRev;
mins10sPos = map(mins / 10, 0, 6, 0, 200) % StepsPerRev;
hours1sPos = map(hours % 10, 0, 10, 0, 200) % StepsPerRev;
hours10sPos = map(hours / 10, 0, 3, 0, 200) % StepsPerRev;
// Move the motors to their calculated positions
secs1s.move(secs1sPos);
secs10s.move(secs10sPos);
mins1s.move(mins1sPos);
mins10s.move(mins10sPos);
hours1s.move(hours1sPos);
hours10s.move(hours10sPos);
// Run the motors
secs1s.runToPosition();
secs10s.runToPosition();
mins1s.runToPosition();
mins10s.runToPosition();
hours1s.runToPosition();
hours10s.runToPosition();
secs1s.run();
secs10s.run();
mins1s.run();
mins10s.run();
hours1s.run();
hours10s.run();
Serial.println("secsPos:"+String(secs1sPos)+"--");
//delay(200);
}goobergoobergoobergoobergoobergoobergoobergoobergoobergoobergoobergoober