I am relatively new to the world of c++

I would like to know what that 'for' does in the code, it should be noted that said code is to transform analog data into ph values
the ph sensor its DF ROBOT V.1

for(int i=0;i<10;i++)       //Get 10 sample value from the sensor for smooth the value
  { 
    buf[i]=analogRead(SensorPin);
    delay(10);
  }
  for(int i=0;i<9;i++)        //sort the analog from small to large
  {
    for(int j=i+1;j<10;j++)
    {
      if(buf[i]>buf[j])
      {
        temp=buf[i];
        buf[i]=buf[j];
        buf[j]=temp;
      }
    }
  }
  avgValue=0;
  for(int i=2;i<8;i++)                      //take the average value of 6 center sample
    avgValue+=buf[i];
  float phValue=(float)avgValue/1024/6; //convert the analog into millivolt
  phValue=3.5*phValue;  //convert the millivolt into pH value
   

Welcome to the forum

If you are looking for help in the English speaking section of the forum then please edit the topic title and translate it to English

Which one ?

You have 4 "fors" in the snipper you posted.
The first "for" reads 10 times the values from the analog input and stores the values in an array of 10 elements;

The second "for" executes the third "for" 9 times;

The third "for" sorts the array of 10 elements together with the second "for" in ascending order;

The fourth "for" uses the second to 7th value of the matrix to take the average;

you might also consider reading the description in The C Programming Language on pg 60 (pdf pg 74).

of course the rest of the book may interest you as well

The Arduino reference is a good place to look for answers

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.