Need help with getting the date as a countdown

Hello Particle enthusiasts,

I’m struggling to get my Photon to automatically get the current date, and then compare it with a future date, leaving me with the remaining numbers. A countdown timer to Christmas day effectively, for the kids on a 2x1 DMD.

So, I have had a look over on the [Submission] Real Time Clock Library for Spark thread, as well as the reference material https://docs.particle.io/reference/firmware/photon/#time. Plus many other forums on arduino code, but can’t wrap my head around why it won’t compile. It seems when i fix one piece, I break another.

Might I ask the great coders how I manage to get the current day of the month (10), then subtract that from the target end date (25), then combine the result of 15 with the string text (15 sleeps until Christmas). I am currently just changing the number every morning, but would much like it to be automatic.

My code is below.

    // Includes section
    #include "SparkIntervalTimer/SparkIntervalTimer.h"
    #include "SparkTime/SparkTime.h"
    #include "DMD/DMD.h"
    #include "DMD/SystemFont5x7.h"
    #include "DMD/Arial_black_16.h"
    
    
    #define DISPLAYS_ACROSS 2
    #define DISPLAYS_DOWN 1
    
    DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
    IntervalTimer scanTimer;
    
    UDP UDPClient;
    SparkTime rtc;
    String nowDay = "0";
    unsigned long currentTime = rtc.now();
    
    void ScanDMD()
    {
      dmd.scanDisplayBySPI();
    }
    
    void setup(void)
    {
    
       scanTimer.begin(ScanDMD, 5000, uSec);
            dmd.clearScreen( true );   
            
        rtc.begin(&UDPClient, "pool.ntp.org");
        rtc.setTimeZone(10); // GMT offset +10, Queensland Australia
        
    }
    
    void loop(void)
    {
       
       byte b;
    
       dmd.clearScreen( true );

// Message one section    
       nowDay = rtc.dayString(currentTime);
       dmd.selectFont(Arial_Black_16);
       
       char msgText1[] = " sleeps until Christmas";
//char msgText1[] = "15 sleeps until Christmas";
       int msgText1Length = 25;
       int targetDay = 25;
       resultDays = (targetDay - nowDay);
       
       remainingDays.concat(resultDays, msgText1)
       dmd.drawMarquee(remainingDays,msgText1Length,(32*DISPLAYS_ACROSS)-1,0);
       start=millis();
       timer=start;
       ret=false;
       while(!ret){
         if ((timer+30) < millis()) {
           ret=dmd.stepMarquee(-1,0);
           timer=millis();
    		}
    	}
   // THE SECOND MESSAGE  -  This works, and everything under here.
      dmd.selectFont(Arial_Black_16);
   char msgText2[] = "Merry Christmas Everyone";
   int msgText2Length = 24;
   dmd.drawMarquee(msgText2,msgText2Length,(32*DISPLAYS_ACROSS)-1,0);
   long start=millis();
   long timer=start;
   boolean ret=false;
   while(!ret){
     if ((timer+30) < millis()) {
       ret=dmd.stepMarquee(-1,0);
       timer=millis();
     }
   }    	

// More messages and stuff under here..
}

@KegRaider Here is how I would get the days left till Christmas.

void setup() {
    
Serial.begin(9600); // Open serial connection to report values to host
Serial.println("Starting up");

// Set time zone to Eastern USA
Time.zone(-4);
}

void loop() {
    
    int day = Time.day(); 
    int christmas = 25;
    int SleepsUntilChristmas = (christmas - day);
    
    Serial.print(SleepsUntilChristmas);
    delay(5000);
} 
1 Like

Did you look at the Time functions? Time.day() will give you the day of the month. You should set Time.zone to the correct offset for your time zone first. I don’t think you need either SparkTime or SparkIntervalTimer libraries for what you’re trying to do.

3 Likes

@RWB, I’ll try and merge that into what I have and work on the squishing of the ‘SleepsUntilChristmas’ result with the text to scroll on the DMD.

@Ric, I needed the “SparkIntervalTimer” for the DMD panel, doesn’t work without it. I had all that working before I decided to try and get the counting down part to be automatic. I did link to the time functions, but thought I needed to see the NTP time. Oh well, live and learn.

I have this “rtc.setTimeZone(10); // GMT offset +10, Queensland Australia” in the void Setup part. I gather it’s not right? I shall change it back to “Time.zone(10);” and keep playing.

Thanks for the pointers

You can find more of the Particle Cloud Time Functions here:

https://docs.particle.io/reference/firmware/core/#zone-

I added the time zone code to the example code above FYI.

// Set time zone to Eastern USA
Time.zone(-4);

Well, that was fun! I coded up an example for a count down clock until Christmas. A few caveats:

  • This only works in December
  • It does not stop on/after Christmas
  • It shows the seconds as 02…01…60…59 which you may not like but I was too lazy to hold the minutes/hours one extra second.
  • It is very useful to have a Christmas day variable or constant so you can set it to today or tomorrow for debugging

Here it is:

float localtimezone = -5.0;
long int tlast = 0;
int xmasday = 25;

void setup() {
    Particle.syncTime();
    Time.zone(localtimezone);
    Serial.begin(9600);

}

void loop() {
    int tnow = Time.now();

    tlast = tnow;
    int day = Time.day(tnow);
    
    Serial.print(xmasday-day-1);
    Serial.print(" days ");
    int hoursremain = 24-Time.hour(tnow); 
    if (hoursremain<10) {
        Serial.print("0");
    }
    Serial.print(hoursremain);
       
    Serial.print(":");
        
    int minutesremain =  60-Time.minute(tnow); 
    if (minutesremain<10) {
        Serial.print("0");
    }
    Serial.print(minutesremain);
        
    Serial.print(":");
        
    int secondsremain =  60-Time.second(tnow); 
    if (secondsremain<10) {
        Serial.print("0");
    }
    Serial.print(secondsremain);
        
    Serial.print("until Christmas!");

}

1 Like

Cool, thanks @BKO. I’ll merge with the DMD code I have and report back when the photon is online to upload. I’m currently 1/2 way across the country (Australia) and have to wait until the house Christmas lights engage, powering the Photon. I was merely struggling to get my head around the date difference and concat functions. Looks like I don’t need the latter. I might have to use the smaller font to get it on the 64x16 LED array.

I did manage go get it to compile with the help of @RWB suggested code, which is a LOT further than what I was before. Thanks again!


void setup()
{

    scanTimer.begin(ScanDMD, 5000, uSec);
        dmd.clearScreen( true );
   Time.zone(10); 
}


void loop()
{
   
   byte b;

   dmd.clearScreen( true );

   dmd.selectFont(Arial_Black_16);
   int day = Time.day();
   int christmas = 25;
   int SleepsUntilChristmas = (christmas - day);
   
   char msgText1[] = " sleeps until Christmas";
   int msgText1Length = 25;
  
   remainingDays = SleepsUntilChristmas + msgText1;
   
   dmd.drawMarquee(remainingDays,msgFiveLength,(32*DISPLAYS_ACROSS)-1,0);
   start=millis();
   timer=start;
   ret=false;
   while(!ret){
     if ((timer+30) < millis()) {
       ret=dmd.stepMarquee(-1,0);
       timer=millis();
     }
   }

// bla bla, rest of stuff here.....
}
1 Like

I just called the wife back home, and the display is scrolling “15 sleeps until Christmas”, plus a bunch of new pixel art screens I added. Totally stoked, thanks @RWB. I will examine the working of @bko code when I get home on Tuesday, so I can see in person what is happening with the panel. Pretty sure I will need to split the lines or scroll it.

Anyway, really appreciate the help on this one.

3 Likes