Keeping average value within the loop??

Hello everybody ,

I have a strain sensor and depending on direction of strain (initial, up or down) leds turn on or off.I am able to read sensor value [e.g 10 times] within the loop and finding the max,min and running average value of each 10 readings.

while there is no strain I would like to store my average value into average_offset variable…and then as I apply strain in up or down direction the average value will change but I want to keep the average_offset value same and then depending on the comparison between average and average_offsett i will turn one of the led on.(eg if sensor reading <average direction UP ,otherwise DOWN)

 // Up and Down Condition  
  float deltaMax=0;
  float deltaMin=0;
  float average_offset=0;
  deltaMax=sensorMax-average;       // when there is no strain sensorMax,sensorMin and average values are equal
  deltaMin=sensorMin-average;        // therefore deltaMin and deltaMax =0

  for (int i = 0; i< 10 ; i++)
    if(deltaMax<=2 &&deltaMin>=-2){    //+- 2 is my threshold value --Initial state
      average_offset=average;                //store the average value into average_offset
  if (readings[index]<average>){  //UP
  if (readings[index]>average ){  //DOWN

within the loop it seems to be hard to keep the average_offset value because this average_offset value will change when i do not apply any strain or when i come back to initial state…any idea?

Thanks a lot…

Sorry I am new and not entirely familiar with your problem, but maybe you could use a weighted average? Then you woulden't need the data from the previous loop and can update it on the fly. Im not sure if this will solve your problem however, just my 2 cents. Best of luck!

@ abdkdrsnl
If this is a continuation of your other thread, please merge them. Keeping a discussion in one place gives far more oversight of the whole scope of the problems.