Pages: [1]   Go Down
Author Topic: Error message...  (Read 438 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Lost Wages
Offline Offline
Full Member
***
Karma: 11
Posts: 103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 653
Posts: 50881
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: