Go Down

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


Please post the code, though. I'm curious why the compiler is complaining about a void type.
The art of getting good answers lies in asking good questions.


The new/completed code:

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

char ledColor(char color) {
 switch (color) {
   case 'c':
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 0);
     digitalWrite(bluePin, 0);
   case 'r':
     digitalWrite(redPin, 255);
     digitalWrite(greenPin, 0);
     digitalWrite(bluePin, 0);
   case 'g':
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 255);
     digitalWrite(bluePin, 0);
   case 'b':
     digitalWrite(redPin, 0);
     digitalWrite(greenPin, 0);
     digitalWrite(bluePin, 255);

void loop() {
 photoState = digitalRead(photoPin); // box open?
 if (photoState > 0) { // box is open
   Serial.println("Mailbox has been opened");
   delay(30000); // wait 30 seconds for mail
   pressureState = digitalRead(pressurePin);
   if (pressureState > 0) { // mail is present
     mailPresent = 1;
     delay(30000); // wait 30 seconds for box to be closed
   else {
     delay(300000); // stay red for 5 minutes
   photoState = 0;
 else if (mailPresent = 0) {
   ledColor('c'); // clear the led

As you can see, the ledColor function does not return anything. Maybe your compiler won't complain?


I pasted the code into 0017, and changed char ledColor() to void ledColor. it compiled just fine.

Then I did the same in 0021. Same successful compile.
The art of getting good answers lies in asking good questions.


That's very strange... I'm currently running on OS X. Which OS are you using? Maybe it'll work without complaint if I paste the code into a new sketch. I'll try that now and post the results when I get the chance.


I'm on Windows XP.

What is the error message that you get?
The art of getting good answers lies in asking good questions.


Dec 24, 2010, 02:52 am Last Edit: Dec 24, 2010, 02:53 am by mrbug Reason: 1
Very strange... I pasted into a new sketch (using void instead of char) and it worked perfectly. Must have been a non-printable character that found its way into the code or something. I've seen that happen before with line termination characters when writing shell scripts in Windows and then running them in Linux.

I was getting the error
Code: [Select]
error: variable or field 'ledColor' declared void

The confusing thing about it is that I was declaring it as a void function properly, but I guess that it saw it as a variable.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131