It makes a lot more sense to "combine" the two value mathematically. For instance, 1 and 0 could become 10 by multiplying the first value by 10 and adding the second value (1,0 -> 10; 1,1 -> 11; 0,1 -> 1; 0,0 -> 0).
Binary expressions can also be used for comparison:
byte total = (sensorValue1 << 1) | (sensorValue2);
switch(total){
case 0b00:
// code here
break;
case 0b01:
// code here
break;
case 0b10:
// code here
break;
case 0b11:
// code here
break;
}
CrossRoads:
break; is required to end each case, yes?
break jumps to the end of the switch. (It breaks out of the switch.) If you leave out the break, execution continues into the next case.
Sometimes this is what you want, but in those cases, you should definitely leave a comment to indicate that it is on purpose.
Just as an illustration:
switch (c)
{
case 10:
case 13:
if (n)
{
line[n] = 0;
process(line);
n = 0;
}
break;
case 32:
if (!n)
break;
// else no break because reasons
case 'a' ... 'z':
line[n++] = c;
break;
}
So here, case 10 has no break, it falls through into case 13. Case 13 always ends. Case 32 sometimes breaks and sometimes falls through.
something as simple as this may read a bit easier to review after a few months!
byte valueA = digitalRead(pinA);
byte valueB = digitalRead(pinB) ? 10 : 0;
valueA += valueB;
switch (valueA){
case 0: //neither active
//
break;
case 1: // A is active
//
break;
case 10: // B is active
//
break;
case 11: // Both are active
//
break;
default:
//
break;
}
Thanks, I'm new to this and am simply trying what seems to make sense (like converting the values into text & concatenating them for comparison). I'll try the two (far better) solutions above.
yes, I'm up to speed on the "break", was just illustrating the point.
I have to disagree with you here BulldogLowell. Bitshifting is readable just fine, it's faster and this way you can use 8 switches in one byte (or 16 in unsigned int) instead of 3 (or 5 in uint). Okay, to make it more readable you can spread it across lines and use binary notation to make it clear. (0b11 is more clear to be switch 1 and switch 2 then just plain 3)
septillion:
I have to disagree with you here BulldogLowell. Bitshifting is readable just fine, it's faster and this way you can use 8 switches in one byte (or 16 in unsigned int) instead of 3 (or 5 in uint). Okay, to make it more readable you can spread it across lines and use binary notation to make it clear. (0b11 is more clear to be switch 1 and switch 2 then just plain 3)
No problem, there are a lot of ways to Do it.
I only threw it out there as an option, that better matched what the OP set out to do.
Personally, I wouldn't use either approach, just the simple if, else sequence.