Where came three prints?

Hi,
This short sketch sort the [90] array and print out the highest value and its position, but it prints out 4 of that, why?
Thanks
Adam


unsigned long prev;
unsigned long found = 0;
int foundNO[90];
int i = 0;
int j = 0;
int calculate = 0; 

int Sig[90]; 

int C = 0; //min analog read value
int t = 0;
int t2 = 0;
int Val;

void setup() {
 // put your setup code here, to run once:

 Serial.begin(9600);
 Serial.println("xxx_setup!");
 Serial.print("File   : "), Serial.println(__FILE__);
 Serial.print("Date   : "), Serial.println(__DATE__);

 prev = millis();
 //irrecv.enableIRIn(); // Start the receiver

}

void loop() {
 // put your main code here, to run repeatedly:

{
 if (calculate == 0)
 {
   int val = analogRead(A5);
   if (val < 500) {
     found++;
   }
   if (millis() - prev >= 100) {
     j++;
     if (j >= 90) ///// WAS: J>90 DOESN'T WORK WELL.
     {
       j = 0;
       calculate = 1;
       IR_Reading_Cal(); ///// PUT HERE IS OK! got 4 highest value.
     }
     foundNO[j] = found;
     Sig[j] = foundNO[j];

     Serial.print("IR_Read_Avg foundNO[");
     Serial.print(j);
     Serial.print("]=");
     Serial.println(foundNO[j]);

     Serial.print("IR_Read_Avg Sig[");
     Serial.print(j);
     Serial.print("]=");
     Serial.println(Sig[j]);

     prev = millis();
     found = 0;
   }
 }
}
}

void IR_Reading_Cal()
{
 if (calculate == 1)
 {
   //Fill array with values, done by 'IR_read_avg'
   for (i = 0; i < 90; i++) {

     //find highest value in array ////////////////////////////////////////////
     if (Sig[i] > C) {
       C = Sig[i];
       //t= position in array of highest value ////////////////////////////?
       t = i;

       Serial.print("L23 Highest val = Sig[");
       Serial.print(t);
       Serial.print("]=");
       Serial.println(C);

     }//end if
     delayMicroseconds(1);
   }//end for

   //find next value that is close to C value
   for (i = 0; i < 90; i++) {
     if ((Sig[i] - C) < 30 && (Sig[i] - C) > -30) {
       C = Sig[i];
       //t2= position in array of next highest value
       t2 = i;

       Serial.print("L39 Second Highest = Sig[");
       Serial.print(t2);
       Serial.print("]=");
       Serial.println(C);

       //if t2 is not = t, or near t
       if ((t2 != t) && ((t2 < t - 10) || (t2 > t + 10))) {
         break;
       }//end if2
     }//end if
   }//end for

   //Find distance between t and t2
   if (t > t2) {
     Val = t - t2 - 48;
   }//end if
   else {
     Val = t2 - t - 48;
   }//end else

   //if distance between t and t2 (positions in array)
   //is close to 48 - signal detected
   if (Val < 4 && Val > -4) {
     //if signal dectected make output value negitive
     C *= (-1);
   }//end if
   return C;
 }
}

serial monitor:

xxx_setup!
File   : C:\Users\HUA.DELLV-PC\Documents\Arduino\avg_3\avg_3.ino
Date   : Feb 10 2022
IR_Read_Avg foundNO[1]=0
IR_Read_Avg Sig[1]=0
IR_Read_Avg foundNO[2]=0
IR_Read_Avg Sig[2]=0
IR_Read_Avg foundNO[3]=0
IR_Read_Avg Sig[3]=0
IR_Read_Avg foundNO[4]=0
IR_Read_Avg Sig[4]=0
IR_Read_Avg foundNO[5]=0
IR_Read_Avg Sig[5]=0
IR_Read_Avg foundNO[6]=0
IR_Read_Avg Sig[6]=0
IR_Read_Avg foundNO[7]=0
IR_Read_Avg Sig[7]=0
IR_Read_Avg foundNO[8]=0
IR_Read_Avg Sig[8]=0
IR_Read_Avg foundNO[9]=0
IR_Read_Avg Sig[9]=0
IR_Read_Avg foundNO[10]=0
IR_Read_Avg Sig[10]=0
IR_Read_Avg foundNO[11]=0
IR_Read_Avg Sig[11]=0
IR_Read_Avg foundNO[12]=0
IR_Read_Avg Sig[12]=0
IR_Read_Avg foundNO[13]=0
IR_Read_Avg Sig[13]=0
IR_Read_Avg foundNO[14]=0
IR_Read_Avg Sig[14]=0
IR_Read_Avg foundNO[15]=0
IR_Read_Avg Sig[15]=0
IR_Read_Avg foundNO[16]=0
IR_Read_Avg Sig[16]=0
IR_Read_Avg foundNO[17]=0
IR_Read_Avg Sig[17]=0
IR_Read_Avg foundNO[18]=0
IR_Read_Avg Sig[18]=0
IR_Read_Avg foundNO[19]=0
IR_Read_Avg Sig[19]=0
IR_Read_Avg foundNO[20]=0
IR_Read_Avg Sig[20]=0
IR_Read_Avg foundNO[21]=0
IR_Read_Avg Sig[21]=0
IR_Read_Avg foundNO[22]=0
IR_Read_Avg Sig[22]=0
IR_Read_Avg foundNO[23]=0
IR_Read_Avg Sig[23]=0
IR_Read_Avg foundNO[24]=0
IR_Read_Avg Sig[24]=0
IR_Read_Avg foundNO[25]=0
IR_Read_Avg Sig[25]=0
IR_Read_Avg foundNO[26]=0
IR_Read_Avg Sig[26]=0
IR_Read_Avg foundNO[27]=0
IR_Read_Avg Sig[27]=0
IR_Read_Avg foundNO[28]=0
IR_Read_Avg Sig[28]=0
IR_Read_Avg foundNO[29]=0
IR_Read_Avg Sig[29]=0
IR_Read_Avg foundNO[30]=0
IR_Read_Avg Sig[30]=0
IR_Read_Avg foundNO[31]=0
IR_Read_Avg Sig[31]=0
IR_Read_Avg foundNO[32]=134
IR_Read_Avg Sig[32]=134
IR_Read_Avg foundNO[33]=338
IR_Read_Avg Sig[33]=338
IR_Read_Avg foundNO[34]=386
IR_Read_Avg Sig[34]=386
IR_Read_Avg foundNO[35]=380
IR_Read_Avg Sig[35]=380
IR_Read_Avg foundNO[36]=358
IR_Read_Avg Sig[36]=358
IR_Read_Avg foundNO[37]=79
IR_Read_Avg Sig[37]=79
IR_Read_Avg foundNO[38]=0
IR_Read_Avg Sig[38]=0
IR_Read_Avg foundNO[39]=0
IR_Read_Avg Sig[39]=0
IR_Read_Avg foundNO[40]=0
IR_Read_Avg Sig[40]=0
IR_Read_Avg foundNO[41]=0
IR_Read_Avg Sig[41]=0
IR_Read_Avg foundNO[42]=0
IR_Read_Avg Sig[42]=0
IR_Read_Avg foundNO[43]=0
IR_Read_Avg Sig[43]=0
IR_Read_Avg foundNO[44]=0
IR_Read_Avg Sig[44]=0
IR_Read_Avg foundNO[45]=0
IR_Read_Avg Sig[45]=0
IR_Read_Avg foundNO[46]=0
IR_Read_Avg Sig[46]=0
IR_Read_Avg foundNO[47]=0
IR_Read_Avg Sig[47]=0
IR_Read_Avg foundNO[48]=0
IR_Read_Avg Sig[48]=0
IR_Read_Avg foundNO[49]=0
IR_Read_Avg Sig[49]=0
IR_Read_Avg foundNO[50]=0
IR_Read_Avg Sig[50]=0
IR_Read_Avg foundNO[51]=0
IR_Read_Avg Sig[51]=0
IR_Read_Avg foundNO[52]=0
IR_Read_Avg Sig[52]=0
IR_Read_Avg foundNO[53]=0
IR_Read_Avg Sig[53]=0
IR_Read_Avg foundNO[54]=0
IR_Read_Avg Sig[54]=0
IR_Read_Avg foundNO[55]=0
IR_Read_Avg Sig[55]=0
IR_Read_Avg foundNO[56]=0
IR_Read_Avg Sig[56]=0
IR_Read_Avg foundNO[57]=0
IR_Read_Avg Sig[57]=0
IR_Read_Avg foundNO[58]=0
IR_Read_Avg Sig[58]=0
IR_Read_Avg foundNO[59]=0
IR_Read_Avg Sig[59]=0
IR_Read_Avg foundNO[60]=0
IR_Read_Avg Sig[60]=0
IR_Read_Avg foundNO[61]=0
IR_Read_Avg Sig[61]=0
IR_Read_Avg foundNO[62]=0
IR_Read_Avg Sig[62]=0
IR_Read_Avg foundNO[63]=0
IR_Read_Avg Sig[63]=0
IR_Read_Avg foundNO[64]=0
IR_Read_Avg Sig[64]=0
IR_Read_Avg foundNO[65]=0
IR_Read_Avg Sig[65]=0
IR_Read_Avg foundNO[66]=153
IR_Read_Avg Sig[66]=153
IR_Read_Avg foundNO[67]=385
IR_Read_Avg Sig[67]=385
IR_Read_Avg foundNO[68]=380
IR_Read_Avg Sig[68]=380
IR_Read_Avg foundNO[69]=387
IR_Read_Avg Sig[69]=387
IR_Read_Avg foundNO[70]=353
IR_Read_Avg Sig[70]=353
IR_Read_Avg foundNO[71]=34
IR_Read_Avg Sig[71]=34
IR_Read_Avg foundNO[72]=0
IR_Read_Avg Sig[72]=0
IR_Read_Avg foundNO[73]=0
IR_Read_Avg Sig[73]=0
IR_Read_Avg foundNO[74]=0
IR_Read_Avg Sig[74]=0
IR_Read_Avg foundNO[75]=0
IR_Read_Avg Sig[75]=0
IR_Read_Avg foundNO[76]=0
IR_Read_Avg Sig[76]=0
IR_Read_Avg foundNO[77]=0
IR_Read_Avg Sig[77]=0
IR_Read_Avg foundNO[78]=0
IR_Read_Avg Sig[78]=0
IR_Read_Avg foundNO[79]=0
IR_Read_Avg Sig[79]=0
IR_Read_Avg foundNO[80]=0
IR_Read_Avg Sig[80]=0
IR_Read_Avg foundNO[81]=0
IR_Read_Avg Sig[81]=0
IR_Read_Avg foundNO[82]=0
IR_Read_Avg Sig[82]=0
IR_Read_Avg foundNO[83]=0
IR_Read_Avg Sig[83]=0
IR_Read_Avg foundNO[84]=0
IR_Read_Avg Sig[84]=0
IR_Read_Avg foundNO[85]=0
IR_Read_Avg Sig[85]=0
IR_Read_Avg foundNO[86]=0
IR_Read_Avg Sig[86]=0
IR_Read_Avg foundNO[87]=0
IR_Read_Avg Sig[87]=0
IR_Read_Avg foundNO[88]=0
IR_Read_Avg Sig[88]=0
IR_Read_Avg foundNO[89]=0
IR_Read_Avg Sig[89]=0
L23 Highest val = Sig[32]=134
L23 Highest val = Sig[33]=338
L23 Highest val = Sig[34]=386
L23 Highest val = Sig[69]=387
L39 Second Highest = Sig[34]=386
IR_Read_Avg foundNO[0]=0
IR_Read_Avg Sig[0]=0

1 Like

Whenever it finds a value higher than any it's found before, it prints it.

Maybe you want your code to wait until it's looked at everything before it reports?

1 Like

Thank you DaveX.
Yes, I'd like it just print one highest value of the whole array.

Then move the printing out of the for loop to after the for loop.

2 Likes

Great!
works.
Thank you.

1 Like

Sorry of one more question.
I changed int Sig[90]; as int Sig[36]; and int foundNO[90]; as int foundNO[36]; the serial monitor as below.
where the 'L65 Highest val = Sig[36]=1507' come from?
Thanks

IR_Read_Avg foundNO[19]=0
IR_Read_Avg Sig[19]=0
IR_Read_Avg foundNO[20]=0
IR_Read_Avg Sig[20]=20
IR_Read_Avg foundNO[21]=0
IR_Read_Avg Sig[21]=0
IR_Read_Avg foundNO[22]=0
IR_Read_Avg Sig[22]=0
IR_Read_Avg foundNO[23]=0
IR_Read_Avg Sig[23]=0
IR_Read_Avg foundNO[24]=0
IR_Read_Avg Sig[24]=0
IR_Read_Avg foundNO[25]=0
IR_Read_Avg Sig[25]=0
IR_Read_Avg foundNO[26]=0
IR_Read_Avg Sig[26]=0
IR_Read_Avg foundNO[27]=0
IR_Read_Avg Sig[27]=0
IR_Read_Avg foundNO[28]=0
IR_Read_Avg Sig[28]=0
IR_Read_Avg foundNO[29]=0
IR_Read_Avg Sig[29]=0
IR_Read_Avg foundNO[30]=0
IR_Read_Avg Sig[30]=0
IR_Read_Avg foundNO[31]=0
IR_Read_Avg Sig[31]=0
IR_Read_Avg foundNO[32]=0
IR_Read_Avg Sig[32]=0
IR_Read_Avg foundNO[33]=0
IR_Read_Avg Sig[33]=0
IR_Read_Avg foundNO[34]=0
IR_Read_Avg Sig[34]=0
IR_Read_Avg foundNO[35]=0
IR_Read_Avg Sig[35]=0
IR_Read_Avg foundNO[36]=0
IR_Read_Avg Sig[36]=0
L65 Highest val = Sig[36]=1507
t1=36


"L65 Highest" doesn't look like it would match any code posted in this thread.

Besides the code, we also don't have your input setup, so I think it would have to come from your code and data.

1 Like

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