Avarage from value of my sensor "X" and save the avarage in "Y" , PLS HELP :D

Hello :slight_smile:

So i got touch sensor and i save value from that sensor in "X"
and every sec the sensor would give value when i touch it, example "1200,2000,200,800".
And because sometimes the value give me such low value even i touch it, so i would like to make an avarage or mean from that value and save the result value in "Y"

And then i can make a code to looking only from "Y" in every 3 or 4 sec. Basicly i want make my touch sensor actived braking system when a value from "X" below 800 (when i dont touch the sensor), but when i looking to my serial monitor the sensor sometimes give me values below 1000 even when i touch the sensor. :confused:

And srry for my english, since i cant use proper grammar :o

So could u help me :D, i dont get it how to make avarage from a value from "X" or my sensor which is always changing every sec (or depend on my delay), and after that how to save it to new variable like "Y". So i could use the "Y" which is avarage from "X" .

THANK YOU :slight_smile: :slight_smile: :slight_smile:

To get an average you need to record several bits of data, sum them and divide by the number of values that mad up the sum. This is often done with a circular buffer and is commonly referred to as a moving or running average. If you Google moving average and check the Wikipedia link, there is a good discussion on how it's done.

This is a tricky problem. The avarage might lead to late reaction because the history of measurements is takon into accoutn. I would check, if consecutive measurements give values that will confirm the press/release of the button and ignore small peaks within the list of values.

I call this method "trust method". When you get consecutive pressure values, the trust, that there is someone pressing, is high. A single value below the "Press threshold" will lower the trust, but will not change the final measurement. If the next value is good again, the trust rises again. If the second value is also low, the trust lowers further and maybe now the result will be changed to "not pressed".

By defining the maximum trust possible and the acceptable tolerance you can adjust this system until you get stable readings.