If statement being ignored WhY?

I am using a pre coded example to figure out what I am doing wrong.
I have marked the lines I added to the sketch the sketch works all but the if statement, It is being ignored and I can’t figure out why. thanks for any help offeredl

/*
  AnalogReadSerial

  Reads an analog input on pin 0, prints the result to the Serial Monitor.
  Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
  Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  
  // read the input on analog pin 0:
  for (int i = 0; i <= 3; i++)  {  delay(500);// I added
  int sensorValue = analogRead(A0);
  // print out the value you read:

   
   //I added the if statement,  the 
   //serial println is part of the original program
   if (sensorValue >530 );Serial.println(sensorValue);
  
  delay(1);    }    // delay in between reads for stability
}

if (sensorValue >530 );

OOPS

BTW

In the Arduino IDE, use Ctrl T to format your code.

Well now at least one of us knows what’s wrong with it.

You have a ; at the end of your conditional ‘if()’

We all do it now and then :wink:

I was going to say auto-format is your friend, but in this case at least with IDE 1.8.7 it is no help…

So see if this would have clued you in:

void loop() {

  for (int i = 0; i <= 3; i++)  {

    delay(500);

    int sensorValue = analogRead(A0);


    if (sensorValue > 530 )
        ;

    Serial.println(sensorValue);

    delay(1);
  } 
}

The if statement controls whether or not the empty statement that is the semicolon on the next line executes or not…

and an empty statement does nothing, so no matter your sensor value, nothing happens.

The line you think it was controlling is not part of the if statement at all. It executes regardless of what went on with the ‘if’.

Watch out for it, and yes, we’ve all done that.

Also don’t be misled by indentation, it makes things look pretty but is without meaning to the compiler, the merciless arbiter.

    if (condition) 
        statement1;
        statement2;
        statement3;

is exactly

    if (condition) 
        statement1;

    statement2;
    statement3;

in this latter oops! it is usual that auto-format will make this other frequent error apparent.

a7

Thanks alto777 and LarryD
It’s working now, sometimes I just too stubborn for my own good.

What is auto-format and how does it work? feel free to send me to the documentation.

I see you edit away the question, you are now on track and I hope see that the above will not print values less than or equal 515.

It can be a very hard thing to spot, I myself have stared for a long time at one or another Oops! which has been pointed out without seeing what you all talking about!

Many times the subject will be like

if statement broken

or

 for loop not looping

or

when did they change the while statement

&c.

which is simply not the issue 99.9999999 percent of the time. Like never.

When you (anyone) thinks so, check the documentation you prefer and be careful about every comma, semicolon, brace, bracket, period, parenthesis &c. &c.

The compiler will mercilessly do what you tell it; the little details make a difference and so far no one has written a compiler that knows what you meant…

It’s a bitch, TBH. And as I said, we all been there and still get snagged by things like that from time to time.

auto format, see reply #2 above!

a7

thanks all!

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