DIY wall chain clock - Page 1
Forum section: electronics / projects

author: Kaptein QK
author_rank: Regular Contributor
timestamp: September 03, 2014, 10:45:55 pm
content: 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:3D files:http://www.thingiverse.com/thing:450985Code:Code://** 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 InterrupttimeStart = (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 AOCR0A = i;delayMicroseconds(10000);}PIND = 0b00000011;//flip coil A polarityfor(uint8_t i = 1; i <= 5; i++) delayMicroseconds(250000);delayMicroseconds(50000);for(uint8_t i=0x01; i<= 0xa0; i++){//ramp up coil AOCR0A = i;delayMicroseconds(10000);}for(uint8_t i=0xa0; i>=0x01; i--){//ramp down coil BOCR0B = i;delayMicroseconds(10000);}PIND = 0b00001100;//flip coil B polarityfor(uint8_t i = 1; i <= 5; i++) delayMicroseconds(250000);delayMicroseconds(50000);//adjust this delay to correct the clockfor(uint8_t i=0x01; i<= 0xa0; i++){//ramp up coil BOCR0B = i;delayMicroseconds(10000);}}}

author: miguelvp
author_rank: Super Contributor
timestamp: September 03, 2014, 11:30:01 pm
content: Nice, and to set the time you just need to pick up the chain and place it in the right time

author: Dave
author_rank: Super Contributor
timestamp: September 04, 2014, 02:53:55 am
content: 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.

author: Kaptein QK
author_rank: Regular Contributor
timestamp: September 04, 2014, 08:59:58 pm
content: ThanksIt 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

author: JohnWard
author_rank: Contributor
timestamp: September 04, 2014, 10:39:33 pm
content: Quote from: Dave on September 04, 2014, 02:53:55 amAC 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/

author: SArepairman
author_rank: Frequent Contributor
timestamp: September 04, 2014, 11:50:47 pm
content: i thought they deliberately slow/speed it up sometime to sync up line clocks.

author: Dave
author_rank: Super Contributor
timestamp: September 05, 2014, 04:02:29 am
content: Quote from: JohnWard on September 04, 2014, 10:39:33 pmTheinstantaneousfrequency is all over the place. The long-term accuracy, however, isverygood.Quote from: SArepairman on September 04, 2014, 11:50:47 pmThat is exactly what they do.

author: VK5RC
author_rank: Unknown
timestamp: September 05, 2014, 08:34:14 am
content: THE time nut Tom V B has measured the accuracy of mainshttp://leapsecond.com/pages/mains/.Great clock.