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.
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.
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.
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.
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.
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.
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 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 .