Click to view
/*
* vetinariclock - an unsettling timepiece driver for Arduino
*
* Copyleft (GPL) Rick Miller 2010-09-12
*/
const int pinA = 2;
const int pinB = 3;
void setup() {
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);
delay(100);
digitalWrite(pin, LOW);
polarity = (! polarity);
}