trying tolearn case

wrote this with "else if" statements and it worked. I'm trying to learn the case statement so I started to re-write. It compiled for 'a' but when I added 'b' I got these errors.

temp_rf_case.cpp: In function 'void loop()':
temp_rf_case:39: error: duplicate case value
temp_rf_case:33: error: previously used here

I tried to use insert code here, but got a mess of "color" statements and code was unreadable, sorry.

#include <SHT1x.h>

#define dataPin 10 // DATA
#define clockPin 11 // SCK
#define dataPin2 10 // DATA
#define clockPin2 11 // SCK
int heater = 9;
int fan = 8;
int val = 0;
float temp_c;
float temp_f;
float humidity;
SHT1x sht1x(dataPin, clockPin);
SHT1x sht1x2(dataPin2, clockPin2);

void setup()
{
Serial.begin(9600);
}

void loop()
{
temp_c = sht1x.readTemperatureC();
temp_f = sht1x.readTemperatureF();
humidity = sht1x.readHumidity();

val = Serial.read(); // See what command is
switch (val)
{
case -1:
break;
case 'A' || 'a' :
// Read values from the sensor
// Print the values to the serial port
Serial.print("Temperature: ");
Serial.print(temp_f, DEC);
Serial.println("F");
case 'B' || 'b' :
{
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
}
/* else if ('C' == val || 'c' == val)
{
digitalWrite (heater, HIGH);
}
else if ('D' == val || 'b' == val)
{
digitalWrite (heater, LOW);
}
else if ('E' == val || 'e' == val)
{
digitalWrite (fan, HIGH);
}
else if ('F' == val || 'f' == val)
{
digitalWrite (fan, LOW);
} */
}
}

Remember to use 'break' at the end of each 'case' to keep from falling thru to following 'case'

    val = Serial.read(); // See what command is
    switch ( val )
    {
        case -1:
            break;

        case 'a': 
        case 'A': 
            // Read values from the sensor
            // Print the values to the serial port
            Serial.print("Temperature: ");
            Serial.print(temp_f, DEC);
            Serial.println("F");

            break;
    
        case 'b': 
        case 'B': 
            Serial.print("Humidity: ");
            Serial.print(humidity);
            Serial.println("%");

            break;

        case 'C':
            break;

        case 'D':
            break;

        case 'E':
            break;

        case 'F':
            break;

        default:
            break;
    }
case 'A' || 'a' :

This is evaluating 'A' OR'ed with 'a', not defining a case for 'A' or 'a'. lloyddean shows the proper way to do that.

The second case ORs 'B' and 'b', which produces the same result as ORing 'A' and 'a', so the case is not unique.

Thanks guys, I got everything working with "else if" but I thought it would be clearer with "switch-case". Looking at your sample I can see that this is definitely true.

Jim

        case 'a': 
        case 'A':

You could save a bit of effort by doing a case change.

switch (tolower (val) )  // test lower-case versions
    {
        case -1:
            break;

        case 'a': 

...

PaulS,
I think he had the correct operator:

Boolean Operators
&& (and)
|| (or)
! (not)

Not sure using them in a case statement is valid tho.
Letting 'a' fall thru to 'A' and same for 'b' to 'B' is pretty simple.

Paul was correct.

'A' || 'a'

... will always evaluate to 1.

Whilst the compiler might accept it, this is hardly what you are wanting to test for.

Just to elaborate a bit ...

  case 'A' || 'a' :

Is the same as saying:

  case 1:

And then ...

 case 'B' || 'b' :

Is also the same as saying:

  case 1:

Hence the error message about:

temp_rf_case:39: error: duplicate case value

Since you have two cases of 1.