Confused by delay behavior

So I am confused by the behavior of this code. I havent written any spark/c++
code before so I cant figure out what Im doing wrong.

timing.ino

int unix_time = 0;
void setup()
{
  Spark.variable("unix_time", &unix_time, INT);
}

void loop()
{
  unix_time=Time.now();
  delay(1000);
}

poll.sh

#! /bin/bash
while true
do
  echo
  echo ------------------------------
  date
  echo unix time
  spark get rockvole3 unix_time
  
  sleep 30
done

output

------------------------------
Tue Aug 26 16:00:28 PDT 2014
unix time
1409094032

------------------------------
Tue Aug 26 16:01:05 PDT 2014
unix time
1409094063

------------------------------
Tue Aug 26 16:01:36 PDT 2014
unix time
1409094094

I believe I am putting the time into the unix_time variable every minute, but the output shows that it is being updated more regularly than that?

Hi @Rockvole

Your delay(1000); measures delay in milliseconds so 1000 milliseconds is 1 second–you are updating the variable around once per second. I say around once per second because the Spark code the runs your loop() function takes some time too.

Oh I see - sorry I thought that was 1 minute.
Thanks @bko :blush:

1 Like