Aug-12-2020, 10:12 PM
You can't simply merge them. The first one is looping continuously, allowing the LEDs to be updated as fast as the sensor can be read and the loop updated. There's no history that needs to be kept, so this is fine.
The second one is keeping an average, and that depends on how fast the updates happen. So it sleeps every 30 seconds. If it ran as fast as the LED loop, the average would be lost. If it caused the LED loop to pause every 30 seconds, the scale would be very non-responsive.
I would probably use datetime to pull a timestamp. Then in the LED loop if the timestamp is more than 30 seconds old, update the timestamp and call a function to update the moving average and print the text.
The second one is keeping an average, and that depends on how fast the updates happen. So it sleeps every 30 seconds. If it ran as fast as the LED loop, the average would be lost. If it caused the LED loop to pause every 30 seconds, the scale would be very non-responsive.
I would probably use datetime to pull a timestamp. Then in the LED loop if the timestamp is more than 30 seconds old, update the timestamp and call a function to update the moving average and print the text.