/*
* vetinariclock - an unsettling timepiece driver for ESP32
*
* Copyleft (GPL) Rick Miller <[email protected]> 2010-09-12
*
* NOTE: Pins 18 & 19 should be connected to either ends of the clock coil
*/
const int pinA = 18;
const int pinB = 19;
#define TICK_PIN 17
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
pinMode(TICK_PIN,OUTPUT);
for(int i = 0;i < 5;i++) {
tone(TICK_PIN,1000,5);
// delay(1000);
}
}
void loop() {
watchTime();
}
unsigned long lastTick = 0;
unsigned long nextTick = 1000;
const int minInterval = 250;
const int maxInterval = 1750;
int ahead = 0;
void watchTime()
{
unsigned long now = millis();
int i;
if ((now > nextTick) &&
((now - nextTick) < 10000)) // in case nextTick rolled over
{
lastTick = nextTick;
if (ahead < (-5000))
{
i = minInterval;
}
else if (ahead > 5000)
{
i = 2000;
}
else
{
i = random(minInterval, maxInterval);
}
nextTick += i;
ahead += (1000 - i);
tick();
}
}
void tick()
{
static boolean polarity = false;
int pin = (polarity ? pinA : pinB);
digitalWrite(pin, HIGH);
Serial.print(pin);
Serial.println(" HIGH");
tone(TICK_PIN,1000,5);
delay(100);
digitalWrite(pin, LOW);
Serial.print(pin);
Serial.println(" LOW");
tone(TICK_PIN,500,5);
polarity = (! polarity);
}