If statement executing even when conditionals are false.

I am controlling a DC motor depending on the current light levels.

The very last if statement is executing as soon as I start my arduino up. I used serial print to display those values to compare to the conditional statement. The values being printed are in fact different than what the conditional is executing.

Printed values:
motorRun == 0
lightlevel == 500ish

Conditional:
if (motorRun > 0 && lightlevel >= 800)

even with this conditional, the code inside the if statement is still being ran.

Any ideas and why this is happening? Here is my code.

const int lightPin = 0;
const int direction1 = 5;
const int direction2 = 6;
const int enabler = 7;
int lightlevel;
int motorStart = 0;

void setup() {
  pinMode(direction1, OUTPUT);
  pinMode(direction2, OUTPUT);
  pinMode(enabler, OUTPUT);
  pinMode(lightPin, INPUT);
  digitalWrite(enabler, HIGH);
  digitalWrite(direction1, LOW);
  digitalWrite(direction2, LOW);
  Serial.begin(9600);
}

void loop() 
{
  lightlevel = analogRead(lightPin);
  Serial.print(lightlevel);
  Serial.print("\n");
  Serial.print(motorStart);
  Serial.print("\t");
  
  if (lightlevel >= 800)
  {
    motorStart = millis();
  }
  if (250 < lightlevel && lightlevel < 800)
  {
    digitalWrite(direction1, LOW);
    digitalWrite(direction2, LOW);
  }
  
  if (lightlevel <= 250)
  {
    digitalWrite(direction1, HIGH);
    digitalWrite(direction2, LOW);
  }
  
  if (motorStart > 0 && lightlevel >= 800);
  {
    digitalWrite(direction1, HIGH);
    digitalWrite(direction2, LOW);
  }
}

if (250 < lightlevel && lightlevel < 800)
change to
if (lightlevel > 250 && lightlevel < 800)

But the ; should not be there.
if (motorStart > 0 && lightlevel >= 800);

larryd:
if (250 < lightlevel && lightlevel < 800)
change to
if (lightlevel > 250 && lightlevel < 800)

But the ; should not be there.
if (motorStart > 0 && lightlevel >= 800);

thank you!!!!!!!!!!

thank you!!!!!!!!!!

You are happy; because, you have got the solutions!

Have you understood --

1. Why the argument of the if() structure should be changed from 250 < lightlevel && lightlevel < 800 to lightlevel > 250 && lightlevel < 800?

2. Why the ; (C termination) should be removed as directed?

Slowly, acquire courage to ask questions. I am sure that if you would ask these questions the Forum Members would certainly answer; they are generous people.

GolamMostafa:
ask questions.

Ok, I'm game; note I'm not OP though. What difference does it make to ask if 250 < lightlevel or lightlevel > 250. Run the code below and see what happens.

//https://forum.arduino.cc/index.php?topic=544754
//1 may 2018

int lightlevel = 300;
void setup()
{
  Serial.begin(9600);
  Serial.println("start");
  if (250 < lightlevel ) Serial.println("250<lightlevel");
  if (lightlevel > 250) Serial.println("lightlevel>250");
  Serial.println("done");
}

void loop()
{

}

Both tests pass.

I think it makes more sense to ask it this way though "lightlevel>250", since that's probably how we would pose the question in speech. But the sketch doesn't seem to care; arithmetically and logically it's the same thing.

+1!

If you don't mind concealing your logic, by all means use 250 <

However, the point is to make your code immediately obvious and self documenting, especially to others.

lightlevel > 250 says: check the 'lightlevel' variable for a lower limit of 250 (and everything less than 250).

By all means, make your code as obscure and cryptic as you want, no one is stopping you.

larryd:
by all means use 250 <

larryd:
change to

The later "by all means" doesn't gel with your earlier imperative "change to", which comes across as if one way is technically different.

Did you actually read what I said?

phantomHelper:
Both tests pass [but] I think it makes more sense to ask it this way though "lightlevel>250"

... where I actually agree with your (later) sentiment.

[aside]Typical of high post count forum members to get all holier-than-thou.[/aside]

The logic of the < > didn't really need to be fixed - no difference.

The semicolon, however, was the problem. In C, an if statement is

if ( )

the statement can be an expression with a trailing semicolon, the "empty" statement (a semicolon on its own), or some braces surrounding a { }, or a few other things. Your 'if' was correct, but the condition was controlling the execution of a single empty statement.

larryd:
if (250 < lightlevel && lightlevel < 800)
change to
if (lightlevel > 250 && lightlevel < 800)

Respectfully disagree.

The original clearly show that lightlevel test is BETWEEN 250 and 800
Machine "reads " same as most humans - from left to right.

if (250 < lightlevel && lightlevel < 800)
{
digitalWrite(direction1, LOW);
digitalWrite(direction2, LOW);
}

As far as the "typo" - on my IDE when I type if( the closing parenthesis gets automatically added and when I type {
and " enter " closing } is added
so I end up with

if()
{
}

then all I have to do is "fill in the blanks". It behave similar in most constructs.

I suppose most people write code piece by piece thus typos can result.

Happy coding.

The original clearly show that lightlevel test is BETWEEN 250 and 800

Respectfully disagree.

if (250 < lightlevel && lightlevel < 800)

Can you read that in English and honestly say that it makes immediate sense ?

When I read it, as soon as I get to if (250 < lightLevel I stop and translate it to if (lightLevel > 250, so why not write it that way in the first place ?

Equally, if you are so wedded to thinking backwards why not do it to the second part of the test as well ? && 800 > lightLevel)

The program is testing the value of lightLevel so it makes sense for that to be the first thing in the test.

Programs need to be read and understood. That is easiest if they are written is as natural language as possible, not in some back to front way that brings little or nothing to the party.

Machine "reads " same as most humans - from left to right.

Apart from those very many humans who don't, because their language is written in other ways and directions.

UKHeliBob:
Respectfully disagree.

if (250 < lightlevel && lightlevel < 800)

Can you read that in English and honestly say that it makes immediate sense ?

I personally prefer to write range statements that way because it closely resembles the way it would be written as a math expression:

250 < lightlevel < 800

250 < lightlevel < 800

This is semantically correct; is it syntactically correct from programming point of view?

lightlevel is a variable, and 250 is a constant value. The variable should be compared, and not the constant.

if (250 < lightlevel && lightlevel < 800)

It is clearly a programming language instruction. Therefore, it should be judged from both semantic and syntactic points of views. It is semantically correct as there is no ambiguity in the meaning; but, it is certainly syntactically wrong as we are comparing a constant against a variable. The variable lightlevel should be greater than 250 AND should be less than 800 for the next course of action to have taken place.

GolamMostafa:
This is semantically correct; is it syntactically correct from programming point of view?

You got that exactly wrong @GolemMostafa.
If it were syntactically incorrect, a C compiler would complain.
But, because the syntax is correct, it won't.

However, the semantics are wrong, because of the language's operator precedence, and left-to-right evaluation.
if (250 < lightlevel < 800) is evaluated as "is 250 less than lightlevel? (answer: true or false)"
Because true is equivalent to 1, and false to zero, the evaluation proceeds
"is 0 (or 1) less than 800? (anwer: true or false)" which is, of course, always true.

If it were syntactically incorrect, a C compiler would complain.

The banana is flying like an arrow.

Is there any kind of error in this sentence? Any language analysis machine, whatever is its intellectual level, will not be able to find any error in this English Language sentence.

But, a school boy will immediately say that there is a semantic error in this sentence.

Compiler is not a divine element; it is a product of human being. Compilation reports (errors and warnings) are always judged by the human operator from realistic/logical point of view. The point is that a variable should be compared against a constant and not the other way.

The banana is flying like an arrow.

The gag is "Time flies like an arrow, but fruit flies like a banana".

Semantically, there is nothing wrong "The banana is flying like an arrow".
In English, to fly like an arrow means to fly straight. A thrown banana can easily fly like an arrow.

Compiler is not a divine element

Burn the heretic!

The point is that a variable should be compared against a constant and not the other way.

Actually, no.
if (10 == var) looks a little odd, but it guarantees that if you forget that a comparison is "==", and you accidentally write if (10 = var) you will get an error, something that cannot be said if you had written if (var = 10)

I get the impression that a lot of what you write is defensive knee-jerk bluster .