Go Down

Topic: Error message... (Read 471 times) previous topic - next topic

t3rror23

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

Code: [Select]
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;

PaulS

Code: [Select]
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.

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:?

PaulS

Quote
can i use an if statement within an if statement? because thats the only way I can think to achieve this:?

Yes, you can use if statements in the body of an if statement.

To achieve what?

MaJiG

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?

Maybe I don't understand...

PeterH


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.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Quote
Maybe I don't understand...

You don't.

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.

Go Up