Just an FYI this code is great but XIVELY wont report data accurately unless you alter it slightly.
if (millis()-LastUpTime>1000)
That is to fast for Xively to properly GRAPH the data. So your method will report the data to Xively but it is to fast to graph it. For those who want to use Xively’s graphing tools change 1000 to 5000. Perhaps I am the only one who uses graphing in Xively but wanted to share in case it might help another.
This is an old discussion, but may still be a good resource for people, so I am commenting. The part of the code where it checks current millis() vs the LastUpTime should fail when millis() exceeds the max value that can be stored as an unsigned long This is a big number but is really 4294967295 milliseconds, which equals less than 50 days. On the Arduino you can use a casting like this to avoid the rollover problem:
if ((unsigned long)(currentMillis - previousMillis) >= interval
But I am not sure if this syntax also works with Spark.IO…!
On the Spar Core millis() does return unsigned long and you should use the same type for any variable that should receive its value anyhow, so casting should not be required ([ulong] - [ulong] = [ulong]).
I am not sure it is necessary, but this was to account for rollover.
For example, if you were storing millis() and previousMillis() as 8-bit numbers, when you got past 255, the values would wrap around. So 258-254 would not be 4, but rather -252, and thus not greater than the threshold.
And to tak up your example you can either have an 8bit unsigned number which ranges from 0 to 255 an will wrap over to 0 again with no negative value possible or you hav an signed 8bit which ranges 0…+127…-128…-1…0.
This now taken for a unsigned 32bit number - as millis() and related variables - you get 0…(2^32-1) with no possibility for negative results.
In other words [unsigned] - [unsigned] can never result in a negative number (unless you overrule the automatic type resolution of the compiler).
Addition: In your example above, you would never have 258 - 254, since 8 bit only goes to 255. So you’d rather have 2 - 254 which would not result in -252 but 4