mborgerson
Well-known member
I was using printf() to show some intermediate results in a data collection program. I found that using printf() to show the time and two other variables didn't work as expected:
var1 = 1; var2 = 2;
Serial.printf("now: %lu var1: %u var2: %u\n", now(),var1,var2);
produced: "now: 1725965454 var1: 0 var2: 1"
Something about calling now() inside printf was messing up the following variable, so I put together the following program which illustrates the issue:
Running this code showed anomalous values in three cases, which seems to show that printf() doesn't know how to handle the time_t data type and something gets messed up in its internal parameter stack. I thought that Arduino defined time_t to be an unsigned long. What am I missing?
var1 = 1; var2 = 2;
Serial.printf("now: %lu var1: %u var2: %u\n", now(),var1,var2);
produced: "now: 1725965454 var1: 0 var2: 1"
Something about calling now() inside printf was messing up the following variable, so I put together the following program which illustrates the issue:
Code:
#include "TimeLib.h"
uint16_t var1, var2;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
setSyncProvider(get_TeensyTime); // Use the time provided from loader
delay(2000);
Serial.println("\n\nTesting now() function in printf");
var1 = 1;
var2 = 2;
Serial.printf("Starting with var1: %u var2: %u\n", var1, var2);
}
void loop() {
delay(2000);
Serial.println("\n");
Serial.printf("now: %lu\n", now());
Serial.printf("var1: %u var2: %u\n", var1, var2);
Serial.printf("now: %lu var1: %u var2: %u\n", now(),var1,var2);
uint32_t lnow = now();
Serial.printf("lnow: %lu var1: %u var2: %u\n", lnow,var1,var2);
time_t tnow = now();
Serial.printf("tnow: %lu var1: %u var2: %u\n", tnow,var1,var2);
Serial.printf("(uint32_t)now(): %lu var1: %u var2: %u\n", (uint32_t)now(),var1,var2);
Serial.printf("(time_t)now(): %lu var1: %u var2: %u\n", (time_t)now(),var1,var2);
}
time_t get_TeensyTime(void) {
return Teensy3Clock.get();
}
Running this code showed anomalous values in three cases, which seems to show that printf() doesn't know how to handle the time_t data type and something gets messed up in its internal parameter stack. I thought that Arduino defined time_t to be an unsigned long. What am I missing?
Code:
Testing now() function in printf
Starting with var1: 1 var2: 2
now: 1725965974
var1: 1 var2: 2
now: 1725965974 var1: 0 var2: 1 <--------------
lnow: 1725965974 var1: 1 var2: 2
tnow: 1725965974 var1: 0 var2: 1 <--------------
(uint32_t)now(): 1725965974 var1: 1 var2: 2
(time_t)now(): 1725965974 var1: 0 var2: 1 <--------------
now: 1725965976
var1: 1 var2: 2
now: 1725965976 var1: 0 var2: 1 <--------------
lnow: 1725965976 var1: 1 var2: 2
tnow: 1725965976 var1: 0 var2: 1 <--------------
(uint32_t)now(): 1725965976 var1: 1 var2: 2
(time_t)now(): 1725965976 var1: 0 var2: 1 <--------------