#include <Arduino.h>
// --- PIN CONFIGURATION ---
const int COIL_PIN_A = 19;
const int COIL_PIN_B = 18;
#define TICK_PIN 17
const int INDICATOR_LED_PIN = 2;
// --- CONFIGURATION CONSTANTS ---
const unsigned long PULSE_DURATION_MS = 30;
// --- FIXED VETINARI ALGORITHM VARIABLES ---
unsigned long lastTickTimeMs = 0; // When the last physical tick happened
unsigned long nextIntervalMs = 1000; // How long to wait before the next physical tick
unsigned long targetRealTimeMs = 0; // The absolute real-time timeline the clock should match
unsigned long clockDisplayedMs = 0; // The total real-time equivalent the clock hands have displayed
bool coilPolarity = false;
void setup() {
Serial.begin(115200);
pinMode(COIL_PIN_A, OUTPUT);
pinMode(COIL_PIN_B, OUTPUT);
pinMode(TICK_PIN,OUTPUT);
pinMode(INDICATOR_LED_PIN, OUTPUT);
digitalWrite(COIL_PIN_A, LOW);
digitalWrite(COIL_PIN_B, LOW);
digitalWrite(INDICATOR_LED_PIN, LOW);
randomSeed(analogRead(0));
// Synchronise timelines at boot
lastTickTimeMs = millis();
targetRealTimeMs = lastTickTimeMs;
clockDisplayedMs = lastTickTimeMs;
}
void Tick() {
// for(int i = 0;i < 5;i++) {
tone(TICK_PIN,1000,150);
// }
}
void Tock(){
// for(int i = 0;i < 5;i++) {
tone(TICK_PIN,500,150);
// }
}
void impulseCoil() {
if (coilPolarity) {
digitalWrite(COIL_PIN_A, HIGH);
digitalWrite(COIL_PIN_B, LOW);
Tick();
} else {
digitalWrite(COIL_PIN_A, LOW);
digitalWrite(COIL_PIN_B, HIGH);
Tock();
}
/* for(int i = 0;i < 5;i++) {
tone(TICK_PIN,1000,5);
}
*/
delay(PULSE_DURATION_MS);
digitalWrite(COIL_PIN_A, LOW);
digitalWrite(COIL_PIN_B, LOW);
coilPolarity = !coilPolarity;
}
void loop() {
unsigned long currentMillis = millis();
// 1. Handle visual indicator feedback outside the tick timer block
// This allows a rapid flashing effect for the "catch-up" state
long liveError = (long)currentMillis - (long)clockDisplayedMs;
if (liveError > 2000) {
// Clock is lagging behind: Blink the LED quickly (every 100ms)
digitalWrite(INDICATOR_LED_PIN, (currentMillis / 100) % 2);
}
else if (liveError < -2000) {
// Clock is too far ahead: Keep LED solid during the dramatic freeze
digitalWrite(INDICATOR_LED_PIN, HIGH);
}
else {
// Normal Vetinari operation: Keep the indicator dark
digitalWrite(INDICATOR_LED_PIN, LOW);
}
// 2. Check if the scheduled random interval has elapsed
if (currentMillis - lastTickTimeMs >= nextIntervalMs) {
// Fire the coil to move the second hand exactly 1 second forward
impulseCoil();
lastTickTimeMs = currentMillis;
// Advance displayed time record
clockDisplayedMs += 1000;
// Calculate strict drift boundaries
targetRealTimeMs = currentMillis;
long realTimeError = (long)targetRealTimeMs - (long)clockDisplayedMs;
// 3. Determine the next interval behavior
int behaviorDice = random(0, 100);
if (realTimeError > 2000) {
// Force rapid catch-up stutters
nextIntervalMs = random(150, 350);
}
else if (realTimeError < -2000) {
// Force an agonizing pause
nextIntervalMs = random(1800, 2500);
}
else {
// Standard Vetinari Cadence distribution:
if (behaviorDice < 60) {
nextIntervalMs = 1000; // Normal tick
}
else if (behaviorDice < 80) {
nextIntervalMs = random(300, 500); // Fast skip
}
else {
nextIntervalMs = random(1500, 2200); // Slow stretch
}
}
Serial.print("Tick! Next wait: ");
Serial.print(nextIntervalMs);
Serial.print("ms | Discrepancy: ");
Serial.print(realTimeError);
Serial.println("ms behind reality");
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4