Some of you are using data points that change over time. However they might be changing over rather large intervals, or perhaps slightly intermittently depending on the type of data you are retrieving.
Because of the nature of your data stream, the values coming in will end up ‘snapping’ from one value to another, over a longer interval, for example:
0
50 (new data point, 1s later)
100 (new data point, 1s later)
44 (new data point, 1s later)
This might result in ‘jerky’ or ‘laggy’ behaviour. We can write a very basic algorithm that allows a far smoother ‘transition’ by slowly ‘spreading’ and interpolating this change in value over time:
0
1 (new data point set: 50)
2 (interpolation begins...)
3
...
48
49
50 (data point reached: 50)
50 (interpolation begins...)
50
51 (new data point set, interrupting interpolation: 100)
52 (interpolation adjusts itself...)
53
...
98
99
100 (data point reached: 100)
100
Customisations made to this algorithm allow you to set how quickly you want the data to smooth over, making this a very handy tool for very quick, linear translations between your incoming data points.
Example flow used to test the output of the recipe. Open up a serial monitor using Coolterm to see the numerical output from your Photon
This example uses pure integers for simplicity and legibility. See if you can convert this example code to use doubles instead (doubles are double-precision floating point numbers, i.e. fractions with higher resolution than regular floating point numbers).
To see results, open a Serial monitor via Coolterm, to see the numbers printed out. You need to connect the Photon to your computer to use Coolterm this way.
NOTE: this recipe does not include any other bits of working with actuators/sensors – it is meant for you to integrate into your existing code.