Sketch for clock pulse:
// Quartz Clock / Lavet Stepping motor Pulse Generator - Martin Lorton (mjlorton.com) Jan 2021
// Read more on how the Lavet Stepping motor works here:
https://en.wikipedia.org/wiki/Lavet-type_stepping_motor// Learn more about this code and how to drive a Quartz Clock /
// Lavet Stepping motor here:
https://youtu.be/NLwb-lgjv9M (
https://youtube.com/mjlorton)
unsigned long StartIntervalMillis = 0;
unsigned long CurrentIntervalMillis = 0;
const long PulseInterval = 45; //Sets pulse width in milliseconds
int PulsePos = 40; //Pin used for positive pulse (Arduino DUE, change for other boards)
int PulseNeg = 41; //Pin used for negative pulse (Arduino DUE, change for other boards)
int state = 0;
int PulsePin = PulsePos;
int NewPulse = 1;
void setup()
{
pinMode(PulsePos, OUTPUT);
pinMode(PulseNeg, OUTPUT);
digitalWrite(PulseNeg, LOW);
digitalWrite(PulsePos, LOW);
Serial.begin(9600);
}
void doPulse()
{
state = (digitalRead (PulsePin));
if (state == 0 && NewPulse == 1)
{
StartIntervalMillis = millis(); // Timing for pulse width and 1 second interval resets here.
digitalWrite(PulsePin, HIGH);
NewPulse = 0;
}
CurrentIntervalMillis = millis () - StartIntervalMillis;
if (state == 1 && CurrentIntervalMillis >= PulseInterval)
{
digitalWrite(PulsePin, LOW);
if (PulsePin == PulsePos) // Change polarity of pulse. Note polarity won't affect the rotation direction
{ // of the Lavet motor / rotor. However, depending on the cogging point / position
PulsePin = PulseNeg; // of the rotor, the polarity of the first pulse may or may not rotate the rotor.
}
else
{ // If it is critical for your clock to start on the first pulse, you will need to
PulsePin = PulsePos; // send a "test" pulse before running the clock.
}
}
}
void loop()
{
if (CurrentIntervalMillis > 1000)
{
NewPulse = 1;
}
doPulse();
}