/* Analog clock drive Forword and Reverse Pin8:mode switch (anather side of switch shuld be connected to GND) switch OFF for start position adjusutment. switch ON will start endless demo loop Pin9,10:connect clock coil through 220ohm between tose tow 2019/07/15 by radiopench http://radiopench.blog96.fc2.com/ */ boolean flag = true; // drive plality flag // tune following parameter for your clock. unit=0.1ms int cwPulse = 255; // CW pulse width int cwPeriod = 120; // CW pulse interval int ccwPulse1 = 40; // CCW pre pulse width int ccwWait = 2; // pulse gap int ccwPulse2 = 155; // CCW main pulse width int ccwPeriod = 800; void setup() { pinMode(13, OUTPUT); // monitor LED pinMode(9, OUTPUT); // conect coil thrugh register(220Ħ) pinMode(10, OUTPUT); // coil return pinMode(8, INPUT_PULLUP); // mode switch // testCW(); // remove comment out when CW adjusttment // testCCW(); // remove comment out when CCW adjusyment if (digitalRead(8) == LOW) { // if switch pushed (pin8=LOW) when start while (digitalRead(8) == LOW) { // while switch pushed cwP(); // rotate cw for clock position adjustment delay(500); } while (digitalRead(8) == HIGH) { // wait switch pushed again } } } void loop() { for (int p = 5; p <= 15; p += 5) { // demo run cw(p); // forwarad delay(500); ccw(p); // revers same angle (return ORG.) delay(500); ccw(p); // reverse delay(500); cw(p); // forword same angle (return ORG.) delay(500); } cw(120); // forwarad 2 turn delay(500); ccw(60); // reverese 1 turn delay(1000); } void cw(int n) { // Rotate clock wise at specified nunber of pulse if (n != 0) { for (int x = 1; x <= n; x++) { // up to n count cwP(); // output clock wise pulse delay100Micros(cwPeriod); // wait specified duration } } } void ccw(int n) { // Rotate counter clock wise at specified number of pulse if (n != 0) { for (int x = 1; x <= n; x++) { // up to n count ccwP(); // output couter clock wise pulse delay100Micros(ccwPeriod); // wait specified duration } } } void cwP() { // 1-setp CW drive digitalWrite(13, HIGH); // LED ON flag = ! flag; // reverse polality from last drive if (flag == true) { // in accrdance with flag posiDrive(cwPulse); // porality is positive } else { negaDrive(cwPulse); // porality is negative } digitalWrite(13, LOW); // LED OFF } void ccwP() { // 1-step CCW drive digitalWrite(13, HIGH); // LED ON flag = ! flag; // reverse drive polality if (flag == true) { posiDrive(ccwPulse1); // positive pulse for pre drive delay100Micros(ccwWait); // wait between second drive negaDrive(ccwPulse2); // negative pulse for secound drive } else { negaDrive(ccwPulse1); // positive pulse for pre drive delay100Micros(ccwWait); // wait between second drive posiDrive(ccwPulse2); // negative pulse for secound drive } digitalWrite(13, LOW); // LED flash end } void posiDrive(int t) { // output positive pulse at designated width digitalWrite(10, LOW); digitalWrite(9, HIGH); // coil drive positive delay100Micros(t); // wait spcified time digitalWrite(9, LOW); // coil drive end } void negaDrive(int t) { // output negative pulse at designated width digitalWrite(9, LOW); digitalWrite(10, HIGH); // coil drive negative delay100Micros(t); // wait specified time digitalWrite(10, LOW); // coil drive end } void testCW() { // this for CW drive test for (;;) { // run forever cw(10); // output 10 fowarad pulse delay(1000); // wait 1 second } } void testCCW() { // this for CW drive test for (;;) { // run forever ccw(10); // output 10 reverse pulse delay(1000); // wait 1 second } } void delay100Micros(int x) { // wait x 100micron(0.1ms) for (int i = 0; i <= x; i++) { delayMicroseconds(100); // wait 100us } }