/*
* 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
*/
unsigned int randNum = 0x8789;
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
{
/* int temp;
// LFSR 16-bit shifting loop to step the generator 16 times
for (int i = 0; i < 16; i++) {
temp = ((((randNum & 0x8000) >> 15) ^
((randNum & 0x2000) >> 13)) ^
((randNum & 0x1000) >> 12)) ^
((randNum & 0x0800) >> 10);
randNum = (randNum << 1) | (temp & 1);
i = randNum;
}
*/
i = random(minInterval, maxInterval);
}
Serial.println(i);
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);
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4