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
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;