/*
This sketch sets a biaxial stepper motor clock to a random time.
The hands are not coupled to each other like in usual clocks
so the movement looks weird.
Interesting links:
https://www.arduino.cc/en/Reference/Stepper
https://github.com/arduino-libraries/Stepper
Written in 2024 by raphik
*/
#include <Stepper.h>
// simulation works fine at 200 spr ONLY
const int stepsPerRevolution = 200; // 200 spr
// define stepper motor connections
Stepper stepper1(stepsPerRevolution, 2, 3, 4, 5); // minuteHand
Stepper stepper2(stepsPerRevolution, 8, 9, 10, 11); // hourHand
// minute hand: 1 revolution takes 3600 seconds (60x60)
const int secondsPerTicMinuteHand = 3600/stepsPerRevolution; // 1 tic every 18s (at 200 spr)
// hour hand: 1 revolution takes 43200 seconds (12x60x60)
const int secondsPerTicHourHand = 43200/stepsPerRevolution; // 1 tic every 216s (at 200 spr)
// set the initial position of the hands
String targetTime = "00:00:00";
int posMinuteHand = 0;
int posHourHand = 0;
// this function does the magic
void setClock( String time ) {
int h = time.substring(0,2).toInt();
if (h >= 12) h -= 12;
int m = time.substring(3,5).toInt();
int s = time.substring(6,9).toInt();
int newPosMinuteHand = ( m * 60 + s ) / secondsPerTicMinuteHand;
int newPosHourHand = (unsigned int)( h * 3600 + m * 60 + s ) / secondsPerTicHourHand;
stepper1.step( newPosMinuteHand - posMinuteHand );
stepper2.step( newPosHourHand - posHourHand );
posMinuteHand = newPosMinuteHand;
posHourHand = newPosHourHand;
}
void setup() {
Serial.begin(9600);
Serial.println(F("Setting the clock randomly"));
stepper1.setSpeed(60);
stepper2.setSpeed(60);
}
void loop() {
targetTime = "";
int hour = random(0, 23);
int minute = random(0, 59);
int second = random(0, 59);
if (hour < 10) targetTime += 0;
targetTime += hour;
targetTime += ":";
if (minute < 10) targetTime += 0;
targetTime += minute;
targetTime += ":";
if (second < 10) targetTime += 0;
targetTime += second;
Serial.print(F("Setting it to: "));
Serial.print(targetTime);
setClock( targetTime );
Serial.println(F(" ✓ Done!"));
delay(3000);
// Instead of typing it manually,time could be taken from
// an NTP server, from a GPS receiver or even from an FM
// radio with RDS capabilities...
}
I
II
III
IV
V
VI
VII
VIII
IX
X
XI
XII