Pages: [1]   Go Down
Author Topic: Case statement using array, help with syntax? #  (Read 372 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something along the lines of -

Code:
#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;
}
« Last Edit: January 31, 2013, 03:55:17 pm by lloyddean » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
((SAS) ? 1 : 0))

this is identical to

if (SAS) then 1;
else 0;
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Gotcha, thanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

Rob Tillaart

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26332
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

"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.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
case 0b00010000:
case 0b00001000:
    break;

cases statements need not be in numerical order.
« Last Edit: February 02, 2013, 01:39:06 pm by lloyddean » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13739
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

agree with lloydDean
+1
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

brilliant, it worked!

Thanks mate  smiley-grin
Logged

Pages: [1]   Go Up
Jump to: