analogRead value does not change within nested IF statement

Hi guys. A real head scratcher here and I am unable to find any help in the forum or online on this.

In place of my PhotoResistor, I am using a potentiometer (voltage divider). My nested IF is looking for 4 readings under 45. HOWEVER... I am altering the potentiometer to >45 during these checks... but the analogRead value never changes like it should within the nested IF re-checks.

After the loop restarts, the new analogRead value (>45) shows up. Anyone know what's going on here? I would really appreciate some direction here. Thanks!

void loop() {
// reads the input on analog pin A0 (value between 0 and 1023)
int PhotoResistor = analogRead(A0);

// Serial.print("Analog reading: ");
// Serial.print(PhotoResistor); // the raw analog reading

// We'll have a few threshholds, qualitatively determined
if (PhotoResistor < 45) {
Serial.print("CONDITION #1 MET: ");
Serial.println(PhotoResistor); // the raw analog reading
Serial.println("WAITING 5 SECONDS");
Serial.println("");
delay(Seconds * 5);
}
if (PhotoResistor < 45) {
Serial.print("CONDITION #2 MET: ");
Serial.println(PhotoResistor); // the raw analog reading
Serial.println("WAITING 5 SECONDS");
Serial.println("");
delay(Seconds * 5);
}
if (PhotoResistor < 45) {
Serial.print("CONDITION #3 MET: ");
Serial.println(PhotoResistor); // the raw analog reading
Serial.println("WAITING 5 SECONDS");
Serial.println("");
delay(Seconds * 5);
}
if (PhotoResistor < 45) {
Serial.print("CONDITION #4 MET: ");
Serial.println(PhotoResistor); // the raw analog reading
Serial.println("DOOR STAYS CLOSED");
Serial.println("");
digitalWrite(relayinit1, HIGH);
digitalWrite(relayinit2, LOW);
digitalWrite(relayinit4, HIGH);
}

SERIAL MONITOR:

CONDITION #1 MET: 30
WAITING 5 SECONDS

CONDITION #2 MET: 30
WAITING 5 SECONDS

CONDITION #3 MET: 30
WAITING 5 SECONDS

CONDITION #4 MET: 30
DOOR STAYS CLOSED

Please read there forum guide in the sticky post, then edit and correct your post because it's breaking forum rules. Code and output from serial monitor should be posted in code tags.

1 Like

I see no nested if's. I see 4 consecutive if's.

And I only see 1 analogRead() at the top of the loop. You could twiddle that pot as much as you like between if's, but since you're not re-reading it, it won't matter.

And yes, as @PaulRB said, not having your code formatted with the handy <CODE/> tool made it that much harder to read.

There is no nested if in your code. There are 4 if statements. They are looking at one reading 4 times. Unsurprisingly, they all execute their code or none of them do.

1 Like

It shouldn't change.
The PhotoResistor value only changes when you run the analogRead() command.
And the PhotoResistor variable only stores the last value.

2 Likes

Please edit youropening post, select all code and click the <CODE/> button and next save your post. This will apply code tags which will make the code easier to read and easier to copy and the forum software will display it correctly.


Your code (in future please post complete code) seems to indicate that you are implementing a finite state machine (look it up). Testing if a condition is met is a state.

Note:
Consider what must happen when all conditions are met and the relays are activated (or de-activated). Does it need to stay like that or does then sequence have to start from the beginning.

Thank you sterretje. I am trying to do this... sorry new to forum.

@b707 - thank you for this. This may help!

From your answer it seems to me that you didn't understand your mistake...

I knew the track you were leading me down b which was the right one. I had to update the PhotoResistor variable after each "check"... which makes perfect sense now in hindsight.

Not a programmer... just need to automate a few things. :wink: This was the final structure that works great!

Thanks a lot guys!

void loop() {
  // reads the input on analog pin A0 (value between 0 and 1023)
  int PhotoResistor = analogRead(A0);
  
  // We'll have a few threshholds, qualitatively determined
    if (PhotoResistor < 45) {
    Serial.print("CONDITION #1 MET: ");
    Serial.println(PhotoResistor);   // the raw analog reading
    Serial.println("WAITING 5 SECONDS");
    Serial.println("");
    delay(Seconds * 5);
    PhotoResistor = analogRead(A0);
    }
     if (PhotoResistor < 45) {
        Serial.print("CONDITION #2 MET: ");
        Serial.println(PhotoResistor);   // the raw analog reading
        Serial.println("WAITING 5 SECONDS");
        Serial.println("");
        delay(Seconds * 5);
        PhotoResistor = analogRead(A0);
     }
        if (PhotoResistor < 45) {
          Serial.print("CONDITION #3 MET: ");
          Serial.println(PhotoResistor);   // the raw analog reading
          Serial.println("WAITING 5 SECONDS");
          Serial.println("");
          delay(Seconds * 5);
          PhotoResistor = analogRead(A0);
        }
          if (PhotoResistor < 45) {
		        Serial.print("CONDITION #4 MET: ");
            Serial.println(PhotoResistor);   // the raw analog reading
            Serial.println("***DOOR STAYS CLOSED***");
            Serial.println("");
		        digitalWrite(relayinit1, HIGH);
		        digitalWrite(relayinit2, LOW);
		        digitalWrite(relayinit4, HIGH);
          }
          else if (PhotoResistor < 1023) {
            Serial.print("DOOR OPEN READING: ");
            Serial.println(PhotoResistor);   // the raw analog reading
            Serial.println("***DOOR STAYS OPEN***");
            digitalWrite(relayinit1, LOW);
            digitalWrite(relayinit2, HIGH);
            digitalWrite(relayinit3, HIGH);
        }

Don't you think that updating it BEFORE "check" makes more sense?

int PhotoResistor = analogRead(A0);
  if (PhotoResistor < 45) {
    Serial.print("CONDITION #1 MET: ");
    Serial.println(PhotoResistor);   // the raw analog reading
    Serial.println("WAITING 5 SECONDS");
    Serial.println("");
    delay(Seconds * 5);
    }
     
   PhotoResistor = analogRead(A0);
   if (PhotoResistor < 45) {
        Serial.print("CONDITION #2 MET: ");
        Serial.println(PhotoResistor);   // the raw analog reading
        Serial.println("WAITING 5 SECONDS");
        Serial.println("");
        delay(Seconds * 5);
     }
   
    PhotoResistor = analogRead(A0);
    if (PhotoResistor < 45) {

They both update the variable immediately after each IF delay.... so same results stored to the variable at the same time. But do I agree with your format above mine. Thanks for the pointer b.

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