Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <TimeLib.h>
- #include <Adafruit_NeoPixel.h>
- #ifdef __AVR__
- #include <avr/power.h>
- #endif
- #define TIME_HEADER 'T' // Header tag for serial time sync message
- #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
- // Which pin on the Arduino is connected to the NeoPixels?
- // On a Trinket or Gemma we suggest changing this to 1
- #define PIN 6
- // How many NeoPixels are attached to the Arduino?
- #define NUMPIXELS 24
- #define SEC_COL 20
- #define HOUR_COL 20
- #define MIN_COL 20
- #define STATIC_COL 10
- // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
- // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
- // example for more information on possible values.
- Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- int delayval = 5; // delay for half a second
- //setting the colors
- char Colors[] = {'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'g', 'b', 'w', 'p', 'b', 'b', 'b', 'e', 'w', 'w', 'w', 'w', 'w', 'w', 'w', 'w'};
- void setup() {
- Serial.begin(9600);
- setSyncProvider(requestSync); //set function to call when sync required
- Serial.println("Waiting for sync message");
- // put your setup code here, to run once:
- // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
- #if defined (__AVR_ATtiny85__)
- if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
- #endif
- // End of trinket special code
- pixels.begin(); // This initializes the NeoPixel library.
- }
- void loop(){
- if (Serial.available()) {
- processSyncMessage();
- }
- /*
- if (timeStatus()!= timeNotSet) {
- digitalClockDisplay();
- }
- */
- int milis = ((millis()%1000)*24/1000);
- float m = (float) (minute()%60);
- float s = (float) (second()%60);
- float h = (float) (hour()%12);
- float minutes = (m + (s/60))*24/60;
- float hours = h + (m/60);
- hours = hours/0.5;
- int m2 = round(minutes)%24;
- int m1 = m2-1;
- if (m2 == 0){
- m1 = 23;
- }
- int h2 = round(hours)%24;
- int h1 = h2-1;
- if (h2 == 0){
- h1 = 23;
- }
- resetpixels();
- if (h2 != 23 && h1 != 23 && m1 != 23 && m2 != 23){
- pixels.setPixelColor(23, pixels.Color(STATIC_COL,STATIC_COL,STATIC_COL));
- }
- if (h2 != 0 && h1 != 0 && m1 != 0 && m2 != 0){
- pixels.setPixelColor(0, pixels.Color(STATIC_COL,STATIC_COL,STATIC_COL));
- }
- if (m2 == h2){
- pixels.setPixelColor(m1, pixels.Color(MIN_COL,0,MIN_COL));
- pixels.setPixelColor(m2, pixels.Color(MIN_COL,0,MIN_COL));
- } else {
- pixels.setPixelColor(h1, pixels.Color(HOUR_COL,0,0));
- pixels.setPixelColor(h2, pixels.Color(HOUR_COL,0,0));
- pixels.setPixelColor(m1, pixels.Color(0,0,MIN_COL));
- pixels.setPixelColor(m2, pixels.Color(0,0,MIN_COL));
- }
- pixels.setPixelColor(milis, pixels.Color(0,SEC_COL,0));
- pixels.show();
- delay(5);
- }
- void resetpixels(){
- for(int i=0;i<NUMPIXELS;i++){
- pixels.setPixelColor(i, pixels.Color(0, 0, 0));
- pixels.show(); // This sends the updated pixel color to the hardware
- }
- pixels.show();
- }
- void digitalClockDisplay(){
- // digital clock display of the time
- Serial.print(hour());
- printDigits(minute());
- printDigits(second());
- Serial.print(" ");
- Serial.print(day());
- Serial.print(" ");
- Serial.print(month());
- Serial.print(" ");
- Serial.print(year());
- Serial.println();
- }
- void printDigits(int digits){
- // utility function for digital clock display: prints preceding colon and leading 0
- Serial.print(":");
- if(digits < 10)
- Serial.print('0');
- Serial.print(digits);
- }
- void processSyncMessage() {
- unsigned long pctime;
- const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
- if(Serial.find(TIME_HEADER)) {
- pctime = Serial.parseInt();
- if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
- setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- }
- }
- time_t requestSync()
- {
- Serial.write(TIME_REQUEST);
- return 0; // the time will be sent later in response to serial mesg
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement