Created
May 14, 2018 05:45
-
-
Save wesalvaro/d96b120af84aa0a58229ccd3f0fd64d2 to your computer and use it in GitHub Desktop.
Citizen Parent Clock (Arduino Nano)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Citizen Parent Clock Program | |
* Send an alternating polarity 24V signal to a child clock every 30 seconds. | |
* This will tick the clock half and full minutes via its internal electromagnet. | |
*/ | |
#define TICK_LENGTH_MS 500 | |
#define CHILD_CLOCK_TICK_INTERVAL 30000 // 30 Seconds | |
#define STATE_SLEEP 0 // No power signal | |
#define STATE_HALF 1 // Send half-minute power signal | |
#define STATE_FULL 2 // Send full-minute power signal | |
#define H_PLS 2 | |
#define H_MNS 3 | |
void setup() { | |
pinMode(H_PLS, OUTPUT); | |
pinMode(H_MNS, OUTPUT); | |
pinMode(LED_BUILTIN, OUTPUT); | |
} | |
/** | |
* 0s 500ms 30s 30.5s | |
* [FFFFF-----------------HHHHH-----------------] | |
*/ | |
int getTickState(unsigned long currentMillis) { | |
if (currentMillis % CHILD_CLOCK_TICK_INTERVAL < TICK_LENGTH_MS) { | |
return (currentMillis / CHILD_CLOCK_TICK_INTERVAL) % 2 ? STATE_HALF : STATE_FULL; | |
} | |
return STATE_SLEEP; | |
} | |
void tick() { | |
switch (getTickState(millis())) { | |
case STATE_SLEEP: | |
digitalWrite(H_PLS, LOW); | |
digitalWrite(H_MNS, LOW); | |
digitalWrite(LED_BUILTIN, LOW); | |
break; | |
case STATE_HALF: | |
digitalWrite(H_PLS, LOW); | |
digitalWrite(H_MNS, HIGH); | |
break; | |
case STATE_FULL: | |
digitalWrite(H_PLS, HIGH); | |
digitalWrite(H_MNS, LOW); | |
digitalWrite(LED_BUILTIN, HIGH); | |
break; | |
} | |
} | |
void loop() { | |
tick(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment