Offline
Newbie
Karma: 0
Posts: 7
|
 |
« on: January 31, 2013, 03:36:34 pm » |
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
God Member
Karma: 21
Posts: 705
|
 |
« Reply #1 on: January 31, 2013, 03:53:27 pm » |
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; }
|
|
|
|
« Last Edit: January 31, 2013, 03:55:17 pm by lloyddean »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #2 on: January 31, 2013, 04:39:05 pm » |
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
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #3 on: February 02, 2013, 12:20:13 pm » |
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
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9414
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #4 on: February 02, 2013, 12:24:19 pm » |
((SAS) ? 1 : 0)) this is identical to if (SAS) then 1; else 0;
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #5 on: February 02, 2013, 12:52:50 pm » |
Gotcha, thanks!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #6 on: February 02, 2013, 01:11:18 pm » |
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
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: February 02, 2013, 01:13:09 pm » |
You should use bitwise OR | , not logical OR ||
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #8 on: February 02, 2013, 01:17:54 pm » |
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
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9414
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #9 on: February 02, 2013, 01:18:25 pm » |
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
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19067
I don't think you connected the grounds, Dave.
|
 |
« Reply #10 on: February 02, 2013, 01:21:25 pm » |
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.
|
|
|
|
Des Moines, WA - USA
Offline
God Member
Karma: 21
Posts: 705
|
 |
« Reply #11 on: February 02, 2013, 01:37:16 pm » |
'OR' better yet one case state can fall into the next - 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
|
|
|
|
|
Netherlands
Online
Tesla Member
Karma: 90
Posts: 9414
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #12 on: February 02, 2013, 01:47:30 pm » |
agree with lloydDean +1
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 7
|
 |
« Reply #13 on: February 02, 2013, 03:28:04 pm » |
brilliant, it worked! Thanks mate 
|
|
|
|
|
Logged
|
|
|
|
|
|