Reading sensor values in Spark.function() vs loop()?

The loop() example will run about 200 times per second.

The function example will run as fast as you can call it from the web, which is probably once or twice per second reliably.

Advantages for speed would be depending on what your analog sensor is. If the sensor updates fast (accelerometer) you would want to read it fast so you don't miss any data. If the sensor updates slow (temperature) you don't need to oversample it so much and slow reads would be appropriate.

Advantages for function are remote polling obviously, which is one reason you probably have the Spark Core in the first place :wink:

Obviously you are remotely polling for the variable & loop() example as well, but the sensor is read faster. This is probably the best of both worlds, that is of course if you need the speed.

If you don't need the speed, then you are wasting real time by oversampling your analog input that could be used for other processing. Right now you have about 5-6 milliseconds of real time to play with per loop() execution. This may change later so check the current specs if you are reading this in the future!

pinMode(A0, INPUT); should be in setup() for both examples. I know I know, free-hand... just saying!

Here's an example sketch to control how fast your loop() executes code: