Maximum integer value recorder does not record the maximum value

Hi,

I am doing a basic program where I need to activate a servo if a lux meter goes over a certain value. To calibrate the value at which the servo activate I want to record the maximum value of the lux meter however my code send me unexpected results. Here is the code:

#include <Servo.h>

Servo servo_9;

int NumSkipped;
int MaxLight;
void setup()
{
  pinMode(A0, INPUT);
  servo_9.attach(9);
  Serial.begin(9600);

}

void loop()
{
    if (analogRead(A0) > 39){
      servo_9.write(45);
      NumSkipped = NumSkipped +1;
      Serial.println("--------------------");
      Serial.print("Number Of Lighting Skipped:");
      Serial.println(NumSkipped);
      Serial.print("Recorded Light:");
      Serial.println(analogRead(A0));
      delay(50);
    }
      if (analogRead(A0)  >=  MaxLight){
      MaxLight = analogRead(A0);
      Serial.print("MaxLight:");
      Serial.println(MaxLight);
      delay(50);
     }

     
      if (analogRead(A0) < 39){
      servo_9.write(90);
      }
}

I am specially interested in those few problematic lines:

      if (analogRead(A0)  >=  MaxLight){
      MaxLight = analogRead(A0);
      Serial.print("MaxLight:");
      Serial.println(MaxLight);
      delay(50);

Here is the result:

18:54:00.689 -> MaxLight:0
18:54:00.723 -> MaxLight:27
18:54:00.793 -> MaxLight:26
18:54:00.862 -> MaxLight:0
18:54:00.896 -> MaxLight:4
18:54:00.964 -> MaxLight:0
18:54:00.997 -> MaxLight:0
18:54:01.065 -> MaxLight:0
18:54:01.099 -> MaxLight:0
18:54:01.168 -> MaxLight:2
18:54:01.202 -> MaxLight:28
18:54:01.271 -> MaxLight:0
18:54:01.305 -> MaxLight:19
18:54:01.372 -> MaxLight:11
18:54:01.407 -> MaxLight:0
18:54:01.475 -> MaxLight:0
18:54:01.510 -> MaxLight:0
18:54:01.579 -> MaxLight:17
18:54:01.613 -> MaxLight:0
18:54:01.682 -> MaxLight:0
18:54:01.716 -> MaxLight:0
18:54:01.784 -> MaxLight:28
18:54:01.818 -> MaxLight:0
18:54:01.886 -> MaxLight:0
18:54:01.920 -> MaxLight:0
18:54:01.988 -> MaxLight:7
18:54:02.022 -> MaxLight:22
18:54:02.090 -> MaxLight:0
18:54:02.124 -> MaxLight:0
18:54:02.191 -> MaxLight:24
18:54:02.225 -> MaxLight:17

How can the ‘‘MaxLight’’ value printed not always be the highest? How come it sometimes prints zero?

You're calling analogRead(A0) twice, the values can be different! Call it once, store the value in an variable, and work with that.

The advice is usually call it twice, and use the 2nd reading. Gives the ADC time to settle better.

Well, not so much settle, more to give the circuit being measured time to charge the ADC sampling cap fully if the source does not have much drive capability.

That's also true. But TS uses one measurement to check if the current value is higher than the previous, and then "remembers" another ; which can of course be less than the previous!

if (analogRead(A0) >=  MaxLight){
   MaxLight = analogRead(A0);

   etc...

This should work as expected:

X = analogRead(A0);
if (X >=  MaxLight){
   MaxLight = X;

   etc...

Thank you guys, it worked, you were right! I needed to call the analogRead only once then store it as follow:

Light = analogRead(A0);
    if (Light >=  MaxLight)
    {
      MaxLight = Light;
      Serial.print("Max Light:");
      Serial.println(Light);
      delay(50);   
    }