/*
* vetinariclock - an unsettling timepiece driver for Arduino
*
* Copyleft (GPL) Rick Miller <[email protected]> 2010-09-12
*
* NOTE: Pins 2 & 3 should be connected to either ends of the clock coil
*/
const int pinA = 2;
const int pinB = 3;
void setup() {
Serial.begin(115200);
while (!Serial);
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
digitalWrite(pinA, LOW);
digitalWrite(pinB, LOW);
}
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");
delay(100);
digitalWrite(pin, LOW);
Serial.print(pin);
Serial.println(" LOW");
polarity = (! polarity);
}