EEVblog Electronics Community Forum

Electronics => Projects, Designs, and Technical Stuff => Topic started by: Kaptein QK on September 03, 2014, 10:45:55 pm

Title: DIY wall chain clock
Post by: Kaptein QK on September 03, 2014, 10:45:55 pm
It uses 3D printed parts (mostly painted),
an Arduino Nano (code written in C in Atmel Studio 6),
a L298N dual H bridge (no micro-stepping, but uses software controlled PWM soft-stepping, completely silent)
and a Nema17 stepper motor (coil resistance 32 Ohm).

Video:
https://www.youtube.com/watch?v=Cqu5ZlmXVh8 (https://www.youtube.com/watch?v=Cqu5ZlmXVh8)

3D files:
http://www.thingiverse.com/thing:450985 (http://www.thingiverse.com/thing:450985)

Code:
Code: [Select]
//*
 * chainclock.c
 *
 * Created: 8/29/2014 11:06:29 PM
 *  Author: Rolf R Bakke
 */

#include <avr/io.h>

void delayMicroseconds( unsigned long delay) {
static unsigned long timeStart;
unsigned long timeCurrent;
if (delay == 0) {
TCCR1B = 0b00000011; //set TCNT1 to run at 250kHz.
TIMSK1 = 0b00000010; //turn on Timer/Counter1, Output Compare A Match Interrupt
timeStart = (unsigned long)TCNT1 << 2;
return;
}
do {
timeCurrent = (unsigned long)TCNT1 << 2;
} while (((timeCurrent - timeStart) & (unsigned long)0x0003ffff) < delay);
timeStart = timeCurrent;
return;
}

int main(void) {

DDRD = 0b11111111;
PORTD =0b00001001;

TCCR0A = 0b10100001;
TCCR0B = 0b00000001;

delayMicroseconds(0);

while(1) {
for(uint8_t i=0xa0; i>=0x01; i--){ //ramp down coil A
OCR0A = i;
delayMicroseconds(10000);
}
PIND = 0b00000011; //flip coil A polarity
for(uint8_t i = 1; i <= 5; i++) delayMicroseconds(250000);
delayMicroseconds(50000);

for(uint8_t i=0x01; i<= 0xa0; i++){ //ramp up coil A
OCR0A = i;
delayMicroseconds(10000);
}
for(uint8_t i=0xa0; i>=0x01; i--){ //ramp down coil B
OCR0B = i;
delayMicroseconds(10000);
}
PIND =  0b00001100; //flip coil B polarity
for(uint8_t i = 1; i <= 5; i++) delayMicroseconds(250000);
delayMicroseconds(50000); //adjust this delay to correct the clock
for(uint8_t i=0x01; i<= 0xa0; i++){ //ramp up coil B
OCR0B = i;
delayMicroseconds(10000);
}
}
}



Title: Re: DIY wall chain clock
Post by: miguelvp on September 03, 2014, 11:30:01 pm
Nice, and to set the time you just need to pick up the chain and place it in the right time :)
Title: Re: DIY wall chain clock
Post by: Dave on September 04, 2014, 02:53:55 am
Lovely project. :-+

Seeing that your circuit is powered by a wall wart (is it AC?), you could use the line frequency as a super accurate time reference (many clocks do that). No need to play with delays and hope that you get it right. ;)
Title: Re: DIY wall chain clock
Post by: Kaptein QK on September 04, 2014, 08:59:58 pm
Thanks :)

It is a DC adapter.
The delay routine uses a hardware timer, and current delay is started from where the previous ended.
Thus code run-time does not affect the delay time.
All delays in the loops adds up to the required 4.5 seconds per step, and I have not seen any inaccuracy yet.
We will see how good it is over long time :)
Title: Re: DIY wall chain clock
Post by: JohnWard on September 04, 2014, 10:39:33 pm
could use the line frequency as a super accurate time reference (many clocks do that).
AC line frequency is typically rather inaccurate, and will vary depending on the total load on the system.

UK line frequency in real time: http://www2.nationalgrid.com/uk/industry-information/electricity-transmission-operational-data/ (http://www2.nationalgrid.com/uk/industry-information/electricity-transmission-operational-data/)


Title: Re: DIY wall chain clock
Post by: SArepairman on September 04, 2014, 11:50:47 pm
i thought they deliberately slow/speed it up sometime to sync up line clocks.
Title: Re: DIY wall chain clock
Post by: Dave on September 05, 2014, 04:02:29 am


AC line frequency is typically rather inaccurate, and will vary depending on the total load on the system.
The instantaneous frequency is all over the place. The long-term accuracy, however, is very good.

i thought they deliberately slow/speed it up sometime to sync up line clocks.
That is exactly what they do.
Title: Re: DIY wall chain clock
Post by: VK5RC on September 05, 2014, 08:34:14 am
THE time nut Tom V B has measured the accuracy of mains http://leapsecond.com/pages/mains/. (http://leapsecond.com/pages/mains/.)
Great clock.