I am trying to run a clock off of an arduino following this site: http://www.cibomahto.com/2008/03/controlling-a-clock-with-an-arduino/comment-page-1/ and I tried to combine it with this to get it to run accurately. https://contractorwolf.com/arduino-pwm-interrupt-clock/
For the project that I want to do I need to slow down the minute hand by 23.9345 (So that it will run on sidereal time)
Now the bare code of the first link with some mods works perfectly, but the delay does not seem to be accurate:
const int a1 = 8; //use digital I/O pin 8
const int a2 = 9;
const int tick1 = 1000;
const int tick2 = 50;
void setup()
{
pinMode(a1,OUTPUT); //set pin 8 to be an output output
pinMode(a2,OUTPUT); //set pin 9 to be an output output
}
void loop()
{
delay(tick1); //delay 1000 milliseconds
digitalWrite(a1,HIGH); //set pin 8 HIGH, turning on LED
delay(tick2); //delay 1000 milliseconds
digitalWrite(a1,LOW); //set pin 8 LOW, turning off LED
delay(tick1); //delay 1000 milliseconds
digitalWrite(a2,HIGH); //set pin 9 HIGH, turning on LED
delay(tick2); //delay 1000 milliseconds
digitalWrite(a2,LOW); //set pin 9 LOW, turning off LED
}
For this reason I have tried to merge it with the 1 wire clock project and got
const int a1 = 8; //use digital I/O pin 8
const int a2 = 9;
const int tick1 = 1000;
const int tick2 = 50;
int cyclesPerSecond = 974;
//pins
int clockInterrupt = 0; //interrupt 0 is pin 2 on UNO
int pwmOut = 6; //pin 6
//timekeeping
int seconds = 0;
int masterClock = 0;//number of square waves
void setup()
{
attachInterrupt(clockInterrupt, clockCounter, RISING);
pinMode(a1,OUTPUT); //set pin 8 to be an output output
pinMode(a2,OUTPUT); //set pin 8 to be an output output
Serial.println(pwmOut);
analogWrite(pwmOut, 127); // this starts our PWM 'clock' with a 50% duty cycle//digital pin 10 for analogWrite pwm out
}
void clockCounter() // called by interrupt 0 (pin 2 on the UNO) receiving a rising clock edge PWM
{
masterClock ++; // with each clock rise add 1 to masterclock count
if(masterClock >= cyclesPerSecond*23.9345) // 974hz on pin 6, may be 490Hz if you use pin 9 or 10
{
seconds ++; // after one cycle add 1 second
masterClock = 0; //reset clock counter
}
}
void loop()
{
if (seconds == 1){
digitalWrite(a1,HIGH); //set pin 8 HIGH, turning on LED
delay(tick1); //delay 1000 milliseconds
digitalWrite(a1,LOW); //set pin 8 LOW, turning off LED
else if(seconds == 2)
seconds = 0;
digitalWrite(a2,HIGH); //set pin 9 HIGH, turning on LED
delay(tick1); //delay 1000 milliseconds
digitalWrite(a2,LOW); //set pin 9 LOW, turning off LED
}
}
However this does not seem to move the second hand properly, so the clock does tick but not strong enough to actually work. Is this a hardware thing or a processor thing? I have no idea why this would not work while the other code does.
millis()
or, for better accuracy, use a proper watch crystal. – Edgar Bonet Nov 16 '16 at 9:21tick1
has a way to large value (1000), if it has to wait one second, and the do some other stuff, but it has to do this every second, it will fail. I also miss a way to prevent the firstif
from being called multiple times. – Gerben Nov 16 '16 at 10:26millis()
interrupt. There is no way this can be more accurate thanmillis()
itself, thus the whole technique serves no purpose at all. Ultimately, the accuracy of bothmillis()
and the PWM is limited by the ceramic resonator clocking the Arduino, which is quite poor. – Edgar Bonet Nov 16 '16 at 16:44