Go Down

Topic: Case statement using array, help with syntax? # (Read 428 times) previous topic - next topic

kdade14

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;
  }

lloyddean

#1
Jan 31, 2013, 09:53 pm Last Edit: Jan 31, 2013, 09:55 pm by lloyddean Reason: 1
Something along the lines of -

Code: [Select]

#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;
}

kdade14

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!

kdade14

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?

robtillaart

Quote
((SAS) ? 1 : 0))


this is identical to

if (SAS) then 1;
else 0;
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


kdade14

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:

AWOL

You should use bitwise OR | , not logical OR ||
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

kdade14

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

robtillaart

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

is allowed

case 0b00010000 | 0b00001000:  // bitwise OR

too

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

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

AWOL

But the logical OR is the same case as 0b00001 (aka true), hence the identical case error.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

lloyddean

#11
Feb 02, 2013, 07:37 pm Last Edit: Feb 02, 2013, 07:39 pm by lloyddean Reason: 1
'OR' better yet one case state can fall into the next -

Code: [Select]

case 0b00010000:
case 0b00001000:
   break;


cases statements need not be in numerical order.

robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

kdade14


Go Up