Go Down

Topic: [SOLVED] switch case with strings (Read 14 times) previous topic - next topic

mrbug

Dec 22, 2010, 03:10 pm Last Edit: Dec 22, 2010, 05:17 pm by mrbug Reason: 1
I'm trying to control a single RGB LED using a switch case, but I keep getting this error:
Code: [Select]
error: case label does not reduce to an integer constant

My code looks like this:
Code: [Select]

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

PaulS

#1
Dec 22, 2010, 03:14 pm Last Edit: Dec 22, 2010, 03:16 pm by PaulS Reason: 1
The message pretty much tells you what the problem is.
Quote
"error: case label does not reduce to an integer constant"


http://gd.tuwien.ac.at/languages/c/programming-bbrown/c_028.htm
Quote
Rules 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:
Code: [Select]
void ledColor(char color) {

Code: [Select]
ledColor("blue");
"blue" is not a character. It is an array of characters.

mrbug

#2
Dec 22, 2010, 03:44 pm Last Edit: Dec 22, 2010, 03:58 pm by mrbug Reason: 1
Quote
"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
Code: [Select]
error: case label does not reduce to an integer constant
and
Code: [Select]
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:
Code: [Select]
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?

Groove

Psst, we can't see your code.
Per Arduino ad Astra

mrbug

#4
Dec 22, 2010, 04:02 pm Last Edit: Dec 22, 2010, 04:03 pm by mrbug Reason: 1
Ah, good point. Here you go:

Code: [Select]
// 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" =-)

Go Up