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);
}
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.
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.
It shouldn't change.
The PhotoResistor value only changes when you run the analogRead() command.
And the PhotoResistor variable only stores the last value.
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.
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. 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);
}
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.