Calculating average and opening a valve


hello ,

i have pressure sensor able max , min values , difference (pdiff = max -min) .
i want calculate average of  first 10 values of pdiff. , want  calculate average once.( first 10 values )

based on average value of pdiff, want open valve once.
and 2 values of pdiff , valve opens , 3 values , on...

i have attached picture better understanding.

code: [select]

int numreadings = 10; // 10 values of pdiff
int readings[10]; // array storing values of 10 pdiff
int index = 0;

void setup(){
}

void loop()
{

static int ppeakcounter = 0;

//calculating pdiff
pdiff = max - min;
// enter values of pdiff array
      int value = pdiff;  
      readings[index] = value;
      index++;
      if (index >= numreadings)
      index = 0;

 // average of 10 pdiff :
  float total =0;
  for (int i=0; i<numreadings; i++)
       total += readings[i];
  float average = total/numreadings;

// based on average value, open valve

 if ( pdiff >= average)
      {
        ++ppeakcounter;
      }
      if(ppeakcounter==1){
        digitalwrite(4, high);
        ppeakcounter=0;
      }
    }
    
   else{
      digitalwrite(4, low);
      }


 

this counting 1 peak.
how count 2 peak , open valve , 3 peaks , on?

thank you

code: [select]
if (index >= numreadings)

that line checking see if have ten readings.  seems me next bit of code calculates averages should inside if calculates average when there 10 readings.  then, if reset index 0 start on , can ten. 


Arduino Forum > Using Arduino > Programming Questions > Calculating average and opening a valve


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts