0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« on: December 22, 2010, 09:10:40 am » |
I'm trying to control a single RGB LED using a switch case, but I keep getting this error: error: case label does not reduce to an integer constant My code looks like this: void ledColor(char color) { switch (color) { case "clear": digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); break; case "red": digitalWrite(redPin, 255); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); break; case "green": digitalWrite(redPin, 0); digitalWrite(greenPin, 255); digitalWrite(bluePin, 0); break; case "blue": digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 255); break; } } and it is called like this: ledColor("blue"); I'm pretty new at this. I tried the "char" type because "string" doesn't seem to be present in the Arduino syntax. What am I missing or doing wrong?
|
|
|
|
« Last Edit: December 22, 2010, 11:17:33 am by mrbug »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #1 on: December 22, 2010, 09:14:45 am » |
The message pretty much tells you what the problem is. "error: case label does not reduce to an integer constant" http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htmRules for switch statements
[glow]values for 'case' must be integer or character constants[/glow] the order of the 'case' statements is unimportant the default clause may occur first (convention places it last) you cannot use expressions or ranges
Your are not. Strings (null terminated arrays of characters) are not characters. You can not use a switch statement in the way you are trying to. This is also a problem: void ledColor(char color) { ledColor("blue"); "blue" is not a character. It is an array of characters.
|
|
|
|
« Last Edit: December 22, 2010, 09:16:58 am by PaulS »
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #2 on: December 22, 2010, 09:44:45 am » |
"blue" is not a character. It is an array of characters. Yeah, that's why I was wondering what I could do to make the strings work. Should I just change it to r/g/b/c instead or is there something else entirely that I should do? EDIT: Okay, I changed it to just r/g/b/c. However, I'm still getting error: case label does not reduce to an integer constant and error: invalid conversion from 'const char*' to 'char' error: initializing argument 1 of 'void ledColor(char)' EDIT 2: If I put "char color = 'c';" in the variable section of my code, it doesn't complain as much. However, I still get this error: error: variable or field 'ledColor' declared void error: 'color' was not declared in this scope error: invalid conversion from 'const char*' to 'char' Is color a reserved word that I don't know about?
|
|
|
|
« Last Edit: December 22, 2010, 09:58:20 am by mrbug »
|
Logged
|
|
|
|
|
UK
Offline
Faraday Member
Karma: 15
Posts: 2852
Gorm deficient
|
 |
« Reply #3 on: December 22, 2010, 09:57:52 am » |
Psst, we can't see your code.
|
|
|
|
|
Logged
|
Per Arduino ad Astra
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #4 on: December 22, 2010, 10:02:06 am » |
Ah, good point. Here you go: // physical mailbox checker (PMC) // ------------------------ // 1. photoresistor detects "box open" state. blue activity LED is enabled. // 2. if pressure sensor detects mail, green "mail present" LED is enabled. // 3. if pressure is REMOVED, red "mail retrieved" LED is enabled. // 4. LED is disabled 5 minutes after mail has been removed if // no more mail is placed into box (accounts for outgoing mail).
const int photoPin = 11; // use pin 11 for photoresistor const int pressurePin = 12; // use pin 12 for pressure sensor // rgb led requires 1 pin for each color const int redPin = 7; // use higher resistor on red! const int greenPin = 8; //use regular resistor on green const int bluePin = 9; // use regular resistor on blue int photoState = 0; // variable for photoresistor state int pressureState = 0; // variable for pressure sensor state int mailPresent = 0; // variable for mail presence char color = "c"; // variable for color code for rgb led
void setup() { // pin setup (hardware) pinMode(photoPin, INPUT); // photoresistor is input pinMode(pressurePin, INPUT); // pressure sensor is input pinMode(redPin, OUTPUT); // led pins are all output pinMode(greenPin, OUTPUT); pinMode(bluePin, OUTPUT); Serial.begin(9600); // enable serial output for debugging }
void ledColor(color) { switch (color) { case "c": digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); break; case "r": digitalWrite(redPin, 255); digitalWrite(greenPin, 0); digitalWrite(bluePin, 0); break; case "g": digitalWrite(redPin, 0); digitalWrite(greenPin, 255); digitalWrite(bluePin, 0); break; case "b": digitalWrite(redPin, 0); digitalWrite(greenPin, 0); digitalWrite(bluePin, 255); break; } }
void loop() { photoState = digitalRead(photoPin); // box open? if (photoState > 0) { // box is open Serial.println("Mailbox has been opened"); ledColor("b"); delay(30000); // wait 30 seconds for mail pressureState = digitalRead(pressurePin); if (pressureState > 0) { // mail is present ledColor("g"); mailPresent = 1; delay(30000); // wait 30 seconds for box to be closed } else { ledColor("r"); delay(300000); // stay red for 5 minutes ledColor("c"); } photoState = 0; } else if (mailPresent = 0) { ledColor("c"); } } Again, I'm very new at this. I'm much more comfortable with Python and BASH than C/Wiring. EDIT: Oh, and be sure to read "color" as "colour" =-)
|
|
|
|
« Last Edit: December 22, 2010, 10:03:35 am by mrbug »
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9454
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #5 on: December 22, 2010, 10:17:51 am » |
A switch statement can use an ENUM, OK it are no strings but an integer in disguise  You could do something like: enum Color { UNDEF, RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE };
void setup() { Color clr = RED; switch (clr) { case UNDEF: break; case RED: clr = BLUE; break; case BLUE: clr = YELLOW; break; // etc default: break; } }
void loop() { }
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #6 on: December 22, 2010, 10:40:11 am » |
I just tried that, but it doesn't seem to fit what I'm trying to do -- but I could just be reading your example incorrectly.
I want to send output to the pins to change the color of the rgb led. I figured that a function would be the best way to avoid duplicating code (thus keeping the sketch size low).
Is the setup() function the correct place for what I'm trying to do?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: December 22, 2010, 10:55:59 am » |
case "g": You can't have a string as a case selector. See reply #1. case 'g': You can have a character.
|
|
|
|
« Last Edit: December 22, 2010, 10:57:20 am by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #8 on: December 22, 2010, 11:17:21 am » |
Got it!! Switching all of the double quotes to single quotes and then modifying the first line of my function fixed it! I now have char ledColor(char color) { instead of void ledColor(char color) { Thanks, everyone! EDIT: Oops, I typed "var" instead of "void" in the second code section.
|
|
|
|
« Last Edit: December 22, 2010, 02:12:38 pm by mrbug »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #9 on: December 22, 2010, 01:27:12 pm » |
I now have Code: char ledColor(char color) {
What does ledColor return?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #10 on: December 22, 2010, 02:08:31 pm » |
Nothing, the function (in theory) acts like this:
>> ledColor('b'); << case 'b': << digitalWrite(redPin, 0); << digitalWrite(greenPin, 0); << digitalWrite(bluePin, 255);
(where each pin controls one leg of the rgb led)
It didn't like when I used "void" even though it doesn't return anything.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9454
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #11 on: December 22, 2010, 02:22:31 pm » |
char ledColor(char color) { idea : you could return the previous value of ledColor.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #12 on: December 22, 2010, 02:30:02 pm » |
Ah yeah, I could do that for logging and logic purposes. Something like current state: blue previous state: red which would show that it did not have mail before, so it's likely to have it now. That or someone is just checking it to see if there is any mail even though the led says otherwise =-)
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #13 on: December 22, 2010, 02:59:11 pm » |
It didn't like when I used "void" even though it doesn't return anything. Why? There is no reason not to declare the return type as void if it doesn't return anything. In fact, if warnings were not turned off, you would see the compiler complain that the function does not return anything if its type is not void.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 19
Arduino rocks
|
 |
« Reply #14 on: December 23, 2010, 04:03:57 pm » |
That's what was puzzling about it! Even though it did not return anything, the compiler would complain if I used void. It works with char, though, so I'm just going to leave it as it is.
|
|
|
|
|
Logged
|
|
|
|
|
|