Case statement using array, help with syntax? #

I'm making a line following program using an 8-sensor array, but I'm having trouble with this particular part of the program.

SAS is the sensor array status, and each value is either 1 or 0, corresponding to each sensor. I know this code isn't really close to being correct, but i think it shows what I'm trying to do.

Is there a way to do this without comparing each element of the array individually and using a ton of &&s?

switch (SAS[0,1,2,3,4,5,6,7]) { case 10000000: leftServo.write(45); rightServo.write(180); break; case 01000000: leftServo.write(90); rightServo.write(180); break; case 00100000: leftServo.write(135); rightServo.write(180); break; case 00010000: leftServo.write(180); rightServo.write(180); break; case 00001000: leftServo.write(180); rightServo.write(180); break; case 00000100: leftServo.write(180); rightServo.write(180); break; case 00000010: leftServo.write(180); rightServo.write(90); break; case 00000001: leftServo.write(180); rightServo.write(45); break; }

Something along the lines of -

#define ENTRIES(ARRAY)  (sizeof(ARRAY)/ sizeof(ARRAY[0]))

int bits = 0;

for ( int i = ENTRIES(SAS); i--; )
    bits = ((bits << 1) | ((SAS[i]) ? 1 : 0));

switch ( bits )
    case 0b10000000: leftServo.write( 45); rightServo.write(180); break;
    case 0b01000000: leftServo.write( 90); rightServo.write(180); break;
    case 0b00100000: leftServo.write(135); rightServo.write(180); break;
    case 0b00010000: leftServo.write(180); rightServo.write(180); break;
    case 0b00001000: leftServo.write(180); rightServo.write(180); break;
    case 0b00000100: leftServo.write(180); rightServo.write(180); break;
    case 0b00000010: leftServo.write(180); rightServo.write( 90); break;
    case 0b00000001: leftServo.write(180); rightServo.write( 45); break;

Looks good, I'm kind of new to C++ so it might take me a while to figure out exactly how that works, but i think it'll work. Thanks for the help!

Sorry for the late reply, i understand all your code except for the line:

((SAS*) ? 1 : 0))* I conceptually understand what it does but i don't understand how the "?1:0" works. could you please explain it?

((SAS) ? 1 : 0))

this is identical to

if (SAS) then 1; else 0;

Gotcha, thanks!

Also, is it possible to have an ‘or’ within the case statement?
such as :

case 0b00010000 || 0b00001000:

i don’t think this is correct though, because when i try it i get a duplicate case value error.
It apparently equals this one:

case 0b00000001:

You should use bitwise OR | , not logical OR ||

But that combine the values into 0b00011000, so now both 0b00010000 ans 0b00001000 are no longer defined if those happen to be the values.

case 0b00010000 || 0b00001000: // logical OR

is allowed

case 0b00010000 | 0b00001000: // bitwise OR


as long as it can be calculated compile time (it should be a constant-expression)

But the logical OR is the same case as 0b00001 (aka true), hence the identical case error.

'OR' better yet one case state can fall into the next -

case 0b00010000:
case 0b00001000:

cases statements need not be in numerical order.

agree with lloydDean +1

brilliant, it worked!

Thanks mate :D