IF i only had a clue

So this is my first post. I typically find an answer without asking, but this one is just killing me, and holding me back a bit from moving forward with the rest of the idea.

I have been trying to lean to work with sensors, the incoming data, and how to organize it into useful stuff that can be used reliably. Usually I use “if” in simple ways with simple comparison operators, and outside of typo errors I have never had much issue, or at least nothing I could not solve by searching for an answer. I plugged in a simple photo resistor so I could play with some new ideas from recent readings. And have found myself stumped by a simple “if” issue.

const int sensorPin = A0;
 int minReading = 200;
 int maxReading = 700;
 int newRead = 0;

 int analogValue = 0;

 void setup(){
  Serial.begin(9600);
 }

 void loop(){
  
 if (maxReading < (newRead + 20) || maxReading > (newRead - 20)){//change maxReading if < newRead by 5 or > than by 5 
    maxReading=newRead;// new reading becomes new max reading
   Serial.println("something changed");// allert to a change
   delay(2000);
   Serial.println("new max value = "); print the changed value
   Serial.println(maxReading, DEC);
   delay(2000);
 }
  
  analogValue=analogRead(sensorPin);
  analogValue=constrain(analogValue, minReading, maxReading);
  analogValue=map(analogValue, minReading, maxReading, 1, 3);
  Serial.print("anaolog value = ");
  Serial.println(analogValue, DEC);
   delay(200);
  Serial.print("max value = ");
  Serial.println(maxReading, DEC);
   delay(200);
  Serial.print("min. value = ");
  Serial.println(minReading, DEC);
   delay(3000);
    newRead=analogRead(sensorPin);
  
  
 }

So as you can see, fairly simple stuff. I’m playing with “constrain” and “map” to learn basic analog input control and trying to use Serial comms. as much as I can to make it a normal thing for me to use.
So what is wrong?
It does not seem to be using the constraints,my intention is to have a text line show up “if” the reading of the photo resistor shows a change of + - 20, but it seems to just pound through the “if” loop regardless, it is reporting changes in maxReading of less than -+ 20 as defined in the if(condition). Also the first text in the Serial Monitor is “something changed, new max value = 0” the value of newRead as set when I defined the int newRead at the beginning, not the value of maxReading as also set at the beginning ie; int maxReading = 700, so it is getting to the line "maxReading =newRead the first time through, resetting the value of maxReading from 700 to 0. I have looked up as many examples of operators as I can and I don’t understand what could be wrong with it, but I have a felling this is where I’m going wrong.
-no compiler errors
-minReading does nothing for now,
-all the Serial stuff works as expected

New semester starts 19th, and I have been preparing with a lot of preemptive studying, so I hope this is not a case of fuzzy brain vs. something new to learn.

Seems to me it should pass the first if condition (and then immediately assign maxReading=newRead;, which is 0).

But you should also have a compiler error, so something seems to be missing or changed in the code you posted.

I just happen to be working on a project that also uses a photoresistor to read ambient light levels. I copied your code, and there is one error:

 Serial.println("new max value = "); print the changed value

you left off the comment character at the end of the statement. I’ve modified your code slightly to show what’s going on, at least with my sensor which returns a value of about 140.

const int sensorPin = A0;
int minReading = 200;
int maxReading = 700;
int newRead = 0;

int analogValue = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){

  if (maxReading < (newRead + 20) || maxReading > (newRead - 20)){//change maxReading if < newRead by 5 or > than by 5 
    maxReading=newRead;// new reading becomes new max reading
    Serial.println("something changed");// allert to a change
    delay(2000);
    Serial.print("maxReading value (top) = "); //print the changed value
    Serial.println(maxReading, DEC);
    delay(2000);
  }
  /* 
   analogValue=analogRead(sensorPin);
   analogValue=constrain(analogValue, minReading, maxReading);
   analogValue=map(analogValue, minReading, maxReading, 1, 3);
   Serial.print("anaolog value = ");
   Serial.println(analogValue, DEC);
   delay(200);
   Serial.print("max value = ");
   Serial.println(maxReading, DEC);
   delay(200);
   Serial.print("min. value = ");
   Serial.println(minReading, DEC);
   delay(3000);
   */
  newRead=analogRead(sensorPin);
  Serial.print("newRead value (bottom) = "); //print the changed value
  Serial.println(maxReading, DEC);
  Serial.println("======== bottom ==========");
}

The constraints and mapping don’t affect the if statement since they are never used with maxReading or newRead. If I don’t move the sensor, at the end of the first pass (where there’s a print for “bottom”), newRead and maxRead are both 140. Now look at your if statement

 if (maxReading < (newRead + 20) || maxReading > (newRead - 20)){

which becomes:

 if (140 < (140 + 20) || 140 > (140 - 20)) {

The first expression in the if statement will always be true. Because you never assign analogValue to either maxReading or newReading, the expression will always return true. Also, I’m curious why are you mapping the values to 1 or 3?

Your first if test, maxReading > (newRead - 20) 700 is greater than (0-20) or -20, as int's are signed numbers. So I could see maxReading getting the value of 0 right off the bat.

Ok, got it. Bummer my first post was in fact a brain fart issue…
Econ, and crossroads pointed out that the first condition would always be true, I had done the equation on paper originally to prove it would be correct and then brain farted when I wrote it in, I simply applied the buff amount (±20) to the wrong variable.

if (maxReading < (newRead + 20) || maxReading > (newRead - 20))

should have been

if ((maxReading + 20) < newRead || (maxReading - 20) > newRead))

I made a stupid mistake.

Econ it is not mapped to 1 and 3, that’s 1 through 3 or 1, 2, 3,

I added some more lines to the sketch, and now it gives me all the info I was trying to get. The sketch now deals with the minReading variable adjusting it in relation to changes in the maxReading, though its just doing so through subtraction or addition of the changes to maxReading, from what I can tell it should still be usable for what I’m doing. The initial reading for max now happens first in loop. Now I need to work on changing constrain and map relative to that. So the output of map stays relative, allowing me to use case 1, 2, 3 consistently even with changes in the ambient room lighting.

Thanks guys, appreciate the help,

const int sensorPin = A0;
 int minReading = 200;
 int maxReading = 700;
 int newMax = 0;
 int newMin = 0;

 int analogValue = 0;

 void setup(){
  Serial.begin(9600);
 }

 void loop(){
   newMax=analogRead(sensorPin);
  
 if ((maxReading+20) < newMax || (maxReading-20) > newMax){//change maxReading if < newRead by 5 or > than by 5 
    newMin=maxReading-newMax;
    minReading=minReading-newMin;
    maxReading=newMax;// new reading becomes new max reading
   Serial.println("something changed");// allert to a change
   delay(2000);
   Serial.print("new max value = "); //print the changed value
   Serial.println(maxReading, DEC);
   delay(2000);
 }
  
  analogValue=analogRead(sensorPin);
  Serial.print("raw analog value = ");
  Serial.println(analogValue, DEC);
  analogValue=constrain(analogValue, minReading, maxReading);
  analogValue=map(analogValue, minReading, maxReading, 1, 3);
  Serial.print("mapped value = ");
  Serial.println(analogValue, DEC);
   delay(50);
  Serial.print("max value = ");
  Serial.println(maxReading, DEC);
   delay(50);
  Serial.print("min. value = ");
  Serial.println(minReading, DEC);
  Serial.println("end of data");
  Serial.println(" ");
   delay(50);
   
  
  
 }