SparkTime.hourString(value) returning wrong value

I have created a timer that is triggered by a button being unpressed, The timer track elapsed time and update the timer on an 16x2 lcd.

Everything works OK, but the hourString returns “01” when the timer starts, and it should be “00”

What am I doing wrong?

The statment that is failing is in line 60.
Here is my code:


// This #include statement was automatically added by the Particle IDE.
// This is a program to trac time elapsed since a button is unnpresed, and display the time and elapsed time on a 16x2 lcd display (1602a)
#include "SparkTime/SparkTime.h"	
#include "application.h"
//#include "SparkTime/SparkTime.h"
#include "LiquidCrystal.h"
//#include "LiquidCrystal/LiquidCrystal.h"

LiquidCrystal lcd(D0, D1, D2, D3, D4, D5);


UDP UDPClient;
SparkTime rtc;



unsigned long currentTime;
unsigned long lastTime = 0UL;
String timeStr;
unsigned long start;
unsigned long timeUsed;

void setup() {
rtc.begin(&UDPClient, "north-america.pool.ntp.org");
rtc.setTimeZone(+1); // gmt offset
// set up the LCD's number of columns and rows: 
lcd.begin(16,2);
start = rtc.now();
}


void loop() {

// set the cursor to column 4, line 0 and print time
lcd.setCursor(4, 0);
currentTime = rtc.now();
if (currentTime != lastTime) {
timeStr = "";
timeStr += rtc.hourString(currentTime);
timeStr += ":";
timeStr += rtc.minuteString(currentTime);
timeStr += ":";
timeStr += rtc.secondString(currentTime);	

lcd.print(timeStr);
  
// check sensor to detect change, this trigers the timer
if (analogRead(0) < 3500) {
  start = rtc.now();
  }
// used for debuging 
// lcd.setCursor(10, 1);
// lcd.print(analogRead(0));

// Print timeUsed since trigger started
  lcd.setCursor(1,1);
  timeUsed = (rtc.now()-start);
  timeStr = "";
  
  timeStr += rtc.hourString(timeUsed);  // ********* Why does this line display "01" when initiatd from 0?
  timeStr += ":";
  timeStr += rtc.minuteString(timeUsed); // ******** this is corretly showing "00" from initiation.
  timeStr += ":";
  timeStr += rtc.secondString(timeUsed); // ******** this is corretly showing "00" from initiation.

  lcd.print(timeStr);
  }
  
  lastTime = currentTime;

}

I believe that the code is applying your Time Zone offset of +1 to time. The library is not really set up to print time differences as you are doing.

You can probably set your time zone offset to zero and set it back to +1 after you get the values you want.

3 Likes

Thank you! This did it.

1 Like