Analog averaging not works

hello,

i use analog reading cumulated average 8 times to get more resolution
my problem is : cumul operation is not made with this part of code

ValADC = analogRead(analogPin);  // read the input pin A0 on Lolin
  SommeValADC = SommeValADC + ValADC;
  Serial.printf("  SommeValADC = %5d  ",SommeValADC);
   i = i + 1;
  if (i > 7)
  {
    //problemo ci dessous
  // pour test only
    LM35DZ_temp = (float) SommeValADC * LM35DZ_MAE ; // MAE lottant initialisé avec 0.0402832;<U+202C>
    // OK ci dessous  !
   //LM35DZ_temp = (float) SommeValADC *  330.0 / 8192.0 ; // avec  10mV par °C  et 8x0124
   Serial.printf("LM35DZ = %5.2f °C ",LM35DZ_temp);
   SommeValADC = 0;

   snprintf(Ana0char,6,"%5.2f",LM35DZ_temp);
   display.setCursor(0,0); 
   display.print(F("A0 LM35DZ         ")); 
   display.setCursor(70,0); 
   display.print(Ana0char); 
   i=0;
  }
  else
  {
    Serial.print(" Ana0=");
    Serial.print(ValADC); 
    Serial.print(" "); 
    
    display.setCursor(0,0); 
       //            123456789012345678
    display.print(F("Compteur          ")); 
    display.setCursor(70,0); 
    snprintf(Oled_Msg,6,"% 5d",Compteur);
    display.print(Oled_Msg);

  }

the printout show not accumulation even i add
SommeValADC = SommeValADC + ValADC;

Compteur= 00303 SommeValADC = 74 LM35DZ = 2.98 °C Pression : 991 Temperat. 23.20 °C Humidite 63.6 BP= BP= OFF ConsigneV = 00000
Compteur= 00304 SommeValADC = 74 Ana0=74 Pression : 991 Temperat. 22.60 °C Humidite 66.5 BP= BP= OFF ConsigneV = 00000
Compteur= 00305 SommeValADC = 74 Ana0=74 Pression : 991 Temperat. 22.70 °C Humidite 63.3 BP= BP= OFF ConsigneV = 00000
Compteur= 00306 SommeValADC = 80 Ana0=80 Pression : 991 Temperat. 23.30 °C Humidite 63.7 BP= BP= OFF ConsigneV = 00000
Compteur= 00307 SommeValADC = 80 Ana0=80 Pression : 991 Temperat. 22.20 °C Humidite 62.4 BP= BP= OFF ConsigneV = 00000
Compteur= 00308 SommeValADC = 80 Ana0=80 Pression : 991 Temperat. 23.30 °C Humidite 69.9 BP= BP= OFF ConsigneV = 00000
Compteur= 00309 SommeValADC = 80 Ana0=80 Pression : 991 Temperat. 22.00 °C Humidite 62.5 BP= BP= OFF ConsigneV = 00000
Compteur= 00310 SommeValADC = 80 Ana0=80 Pression : 991 Temperat. 22.40 °C Humidite 63.0 BP= BP= OFF ConsigneV = 00000
Compteur= 00311 SommeValADC = 80 LM35DZ = 3.22 °C Pression : 991 Temperat. 23.10 °C Humidite 63.5 BP= BP= OFF ConsigneV = 00000

so i get a result of 2.98°C
instead of
about 75 (elementaty ADC measures ) x8 times * 0.0402832= 24.16°C ;
i don't understand why SommeValADC doesnt' increase during the loop from i=0 to i=7 ?

other functions like OLED display , DHT11 , BPM805 are OK ..

How and where is SommeValADC declared ?

It looks like some code you didn't show is setting SommeValADC to zero before entering the code you did show.

We ask you to post all of your code in code tags for very good reasons.

Hello,

your are right, a friend tell me my mistake
i had a trailling bad init to zero of SommeValADC near the end of loop

Even with big glasses , sometimes we can't see the evidence ..
because this mistake was not inside my previous version ..

  TempAndHumidity lastValues = dht.getTempAndHumidity();
   if (digitalRead(BP1)==HIGH) BP1_status="OFF"; else BP1_status="ON";
   Compteur ++;
  //  Serial.printf(" Compteur= %05d ",Compteur);
  //  Serial.printf(" LM35DZ= %5.2f °C ",LM35DZ_temp);
        
    SommeValADC = 0;
   Serial.print(" Pression : " + String(p,0));
   Serial.printf(" Temperat. % 5.2f °C ",lastValues.temperature);
   Serial.printf(" Humidite % 3.1f %",lastValues.humidity);
   Serial.printf(" BP= %s",BP1_status);
   Serial.printf(" ConsigneV = %s",Consignechar);
   Serial.println();
   delay(2500);
}

:ok_hand:
thank's

how to mark solved ?

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