Internet button shield clock

Hi all!

Last Wednesday there was a build night in Aachen. During that night @moors7 and I could experiment with the new shield. I already had an idea to build a clock with it, but not exactly figured out how to do it exactly. @moors7 came with the input of blinking one of the leds to indicate the minutes and well, after some programming, this was born. It was working generally that same build night, although I broke it later on :smile: When I got home I found that @GregF made something similar but I talked to him about this and this was ok with him, so thanks for that Greg :smile:

How it works:
It uses Spark.time to get the time from the cloud. You can set your GMT time diff in the code. You can also adjust some other things, see the code and comments for more info.

Green light means hour, simple.
The tricky part was the minutes, since the board only has 11 LEDs, some thinking had to be done. @Moors7 came with the input of flashes as individual minutes which comes down to this: The blue led means the minute had, so if it is on at where normally 10 is on your clock, there are 10 minutes. Then every 5 seconds (Aprox) it flashes X times in purple, indicating individual minutes to add to those 10. So if the 10 minute hand is blue and flashes twice in purple, it is XX:12. It does not show seconds.
It also uses the onboard RGB led so 12 leds show the time properly :smile:

Gif:

The time in the gif is 10:34 :smile:

It synchronizes once everyday to keep accurate.

The code:

    //Spark internet button shield clock. Pulsating.
//Created by TheHawk1337 together with input from Moors7.

//Working as following:
//Green is the hour hand.
//Blue is where the minute hand (kindof) is.
//The purple flashes are the individual minutes. Add these up to where the minute hand is, so if it points at 5 and
//flashes 2 times in purple, that 7 minutes past where the green led is.

//Setting below

//Enjoy!


// This #include statement was automatically added by the Spark IDE.
#include "SparkButton/SparkButton.h"

SparkButton klok = SparkButton();
int hour = 0;
int minutes = 0;
int fiveminutes = 0;
int singleminutes = 0;
int syncday;
int GMT = 1;  //GMT time diff, use 1 for +1 and -1 for -1 GMT
int brightness = 50; //Brightness control. Set at 50 because the neopixels are insanely bright. Sunglasses adviced at >150.
int speed = 1; //speed for the pulsating of the for loop, increase if you want the pulse to be faster.

void setup() {
    Spark.syncTime();
    klok.begin();
    Serial.begin(9600);
    RGB.control(true);
    RGB.color(0,0,0);
    syncday = Time.day();
    
}

void loop() {
    Serial.print(Time.timeStr());
    klok.ledOff(hour);
    hour = Time.hourFormat12();
    hour += GMT;
    Serial.print(hour);
    minutes = Time.minute();
    
    if (hour > 12)
    {
        hour -= 12;
    }
    
    if (hour < 0)
    {
        hour += 12;
    }
    
    if (hour == 12||hour == 0)
    {
        RGB.color(0,brightness*3,0);
    }
    
    
    else
    {
        klok.ledOn(hour,0,brightness,0);
    }
    
    singleminutes = minutes%5;
    fiveminutes = (minutes - singleminutes) / 5;
    
    if (fiveminutes > 0 && singleminutes > 0 )
    {
        if (fiveminutes != hour)
        {
            do {
                for(int i = 0; i<singleminutes;i++)
                    {
                        for(int b = 0; b<brightness; b+=speed)
                        {
                            klok.ledOn(fiveminutes,b,0,brightness);
                            myDelay(1);
                        }
                        
                        for(int c = brightness; c>1; c-=speed)
                        {
                            klok.ledOn(fiveminutes,c,0,brightness);
                            myDelay(1);
                        }
                        klok.ledOn(fiveminutes,0,0,brightness);
                        myDelay(100);
                    }
                
                klok.ledOn(fiveminutes,0,0,brightness);
                myDelay(5000);
                
            } while (minutes == Time.minute());
        klok.ledOff(fiveminutes);
            
        }
        
        else if (fiveminutes == hour)
        {
            do {
                for(int i = 0; i<singleminutes;i++)
                    {
                        for(int b = 0; b<brightness; b+=speed)
                        {
                            klok.ledOn(fiveminutes,b,brightness,brightness);
                            myDelay(1);
                        }
                        
                        for(int c = brightness; c>1; c-=speed)
                        {
                            klok.ledOn(fiveminutes,c,brightness,brightness);
                            myDelay(1);
                        }
                        klok.ledOn(fiveminutes,0,brightness,brightness);
                        myDelay(100);
                    }
                
                klok.ledOn(fiveminutes,0,brightness,brightness);
                myDelay(5000);
                
            } while (minutes == Time.minute());
        klok.ledOn(fiveminutes,0,brightness,0);
        }
            
    }
    
    else if (fiveminutes == 0 && singleminutes > 0)
    {
        if (hour != 12&& hour!=0)
        {
            do {
                for(int i = 0; i<singleminutes;i++)
                    {
                        
                        for(int b = 0; b<brightness*3; b+=speed)
                        {
                            RGB.color(b,0,brightness*3);
                            myDelay(1);
                        }
                        
                        for(int c = brightness*3; c>1; c-=speed)
                        {
                            RGB.color(c,0,brightness*3);
                            myDelay(1);
                        }
                        RGB.color(0,0,brightness*3);
                        myDelay(100);
                    }
                
                RGB.color(0,0,brightness*3);
                myDelay(5000);
                
            } while (minutes == Time.minute());
        RGB.color(0,0,0);
        }
            
        else if (hour == 12|| hour == 0)
        {
                do {
                for(int i = 0; i<singleminutes;i++)
                    {
                        
                        for(int b = 0; b<brightness*3; b+=speed)
                        {
                            RGB.color(b,brightness*3,brightness*3);
                            myDelay(1);
                        }
                        
                        for(int c = brightness; c>1; c-=speed)
                        {
                            RGB.color(c,brightness*3,brightness*3);
                            myDelay(1);
                        }
                        RGB.color(0,brightness*3,brightness*3);
                        myDelay(100);
                    }
                
                RGB.color(0,brightness*3,brightness*3);
                myDelay(5000);
                
            } while (minutes == Time.minute());
        RGB.color(0,brightness*3,0);
        }
    }
    
    else if (fiveminutes > 0 && singleminutes == 0)
    {
        if (hour != fiveminutes)
        {
            do {
                klok.ledOn(fiveminutes,0,0,brightness);
            } while (minutes == Time.minute());
        }
        
        else if (hour == fiveminutes)
        {
            do {
                klok.ledOn(fiveminutes,0,brightness,brightness);
            } while(minutes == Time.minute());
        }
    }
    
    else if (fiveminutes == 0 && singleminutes == 0)
    {
        do{
            myDelay(1000); //nothing lol
        }while(minutes == Time.minute());
    }
    
    if (syncday != Time.day())
    {
        Serial.print('Syncing!');
        Spark.syncTime();
        syncday = Time.day();
    }
}// end main loop



void myDelay(unsigned long duration)
{
    unsigned long start = millis();
    while (millis() - start <= duration)
    {
      SPARK_WLAN_Loop();
    }
}

Enjoy :smile:

3 Likes

Important edit:

Fixed a hazard where the GMT time diff could lead to times >12 and <0 so the ‘hour’ could be 14 or -4 so the green LED woulden’t go on. Fixed with some if statements :smile:

1 Like

GAHHH this is so cool! Amazing project @TheHawk1337!

2 Likes