Last active
August 29, 2015 13:56
-
-
Save chris-gunawardena/9324546 to your computer and use it in GitHub Desktop.
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
int SER_Pin = 8; //connects to pin 14 on the 75HC595 | |
int RCLK_Pin = 9; //connects to pin 12 on the 75HC595 | |
int SRCLK_Pin = 10; //connects to pin 11 on the 75HC595 | |
const int number_of_74hc595s = 3; | |
const int number_of_register_pins = number_of_74hc595s * 8; | |
boolean registers[number_of_register_pins]; | |
//Mapping of LED order to serial converter pins | |
int hour_led_order[] = { 17, 18, 19, 20, 21, 22, 23, 12, 13, 14, 15, 16 }; | |
int minute_led_order[] = { 5, 4, 3, 2, 1, 0, 6, 7, 8, 9, 10, 11 }; | |
void setup() { | |
pinMode(SER_Pin, OUTPUT); | |
pinMode(RCLK_Pin, OUTPUT); | |
pinMode(SRCLK_Pin, OUTPUT); | |
//Serial.begin(9600); | |
} | |
//set current time | |
int hours = 9; | |
int minutes = 29; | |
int seconds = 55; | |
int milliseconds = 0; | |
long previous_millis = 0; // store last time loop was run in milli seconds | |
long last_blink_millis = 0; // store time when seconds tick | |
void loop() { | |
//increment by seconds and cascade down | |
unsigned long current_millis = millis(); | |
milliseconds = milliseconds + (current_millis - previous_millis ); | |
previous_millis = current_millis; | |
//Serial.print(hours); Serial.print(":"); Serial.print(minutes); Serial.print(":"); Serial.print(seconds);Serial.print("."); Serial.print(milliseconds); Serial.println(""); | |
//increment time | |
seconds = seconds + (milliseconds/1000); | |
minutes = minutes + (seconds/60); | |
hours = hours + (minutes/60); | |
//reset to 0 when over 60, 12 etc | |
milliseconds = milliseconds % 1000; | |
seconds = seconds % 60; | |
minutes = minutes % 60; | |
hours = hours % 12; | |
display_time( hours, minutes, seconds, milliseconds ); | |
//Flash all led's every second to simulate seconds ticking | |
if( (current_millis - last_blink_millis ) > 1000 )//ms | |
{ last_blink_millis = current_millis; | |
seconds_blink(); | |
delay(150); | |
}else{ | |
delay(200); | |
} | |
} | |
void seconds_blink() | |
{ for(int i=0; i<number_of_register_pins; i++) | |
{ registers[i] = HIGH; | |
} | |
write_registers(); | |
} | |
void display_time( int hours, int minutes, int seconds, int milliseconds ) | |
{ clear_registers(); | |
registers[ hour_led_order[hours] ] = HIGH; | |
registers[ hour_led_order[minutes/5] ] = HIGH; | |
registers[ minute_led_order[minutes/5] ] = HIGH; | |
write_registers(); | |
} | |
void write_registers() | |
{ digitalWrite(RCLK_Pin, LOW); | |
for(int i=0; i<number_of_register_pins; i++) | |
{ digitalWrite(SRCLK_Pin, LOW); | |
digitalWrite(SER_Pin, registers[i]); | |
digitalWrite(SRCLK_Pin, HIGH); | |
} | |
digitalWrite(RCLK_Pin, HIGH); | |
} | |
void clear_registers() | |
{ for(int i=0; i<number_of_register_pins; i++) | |
{ registers[i] = LOW; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment