Hey everyone, yet another noob with a noob question:)...im busy with the following project and when i try to verify it i get the error message
" 'SENSOR_VALUE' cannot appear in a constant-expression".
void loop()
{
SENSOR_VALUE = analogRead(IR_PIN);
switch (SENSOR_VALUE)
{
case (SENSOR_VALUE >= 500) && (SENSOR_VALUE < 600): // <- The arduino compiler points here
READING_1 = digitalRead(LIGHT_1);
if (READING_1 = HIGH)
STATE = LOW;
else
STATE = HIGH;
digitalWrite(FAN_2, STATE);
break;
case (SENSOR_VALUE >= 500) && (SENSOR_VALUE < 600):
Case values have to be constants. This expression is not a constant.
A switch statement is meant to choose one of many options, when the options are constants. It is not a general-purpose replacement for if/else if/else.
It is usual to name variables in lower case or Title Case and constants as UPPER CASE.
It looks to me that the compiler is seeing SENSOR_VALUE as a constant. The supplied code doesn't show the declaration of SENSOR_VALUE. Could the simple addition of an int at the start of the assignment solve the confusion?
t3rror23:
oh if thats the case then can i use an if statement within an if statement? because thats the only way I can think to achieve this:?
If you were thinking in terms of a sequence of switch / case clauses but the case expressions aren't constant, you could achieve essentially the same effect by a sequence of if / else if statements. For a simple life, you would ensure that the various 'if' conditions were mutually exclusive.
The message was not that SENSOR_VALUE was undefined or constant. It was the the case label was not a constant. Aside from the not-following-convention name, there is nothing to indicate that the name is not a declared variable of the correct type.