Let's Make Robots!

switch case statement for multiple digitalRead inputs?

Hey guys,

I have a question.

in arduino C, how would you go about having digital pin 14,15,16,17 as input? the control logic coming from a TX2 chip Forward/Rev/Left/Right outputs..










Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

posts are getting Looooong already :)

Guys, MY 1st post was that I was unsure as how I would make a switch case of multiple digitalinput/s (pin14-pin19 OR A0-A5)... in short, this would result to forward(); when a HIGH is detected on A0(pin14). or backward(); when a HIGH is read at A1(pin15) or rotate_left(); with a HIGH on A2(pin16) or a crabwalk_left(); with both a HIGH on A0 & A2... now my 2nd problem was my piezo would not play a tune.. but its all fixxed now.. I added


inside void setup() ... and NOW it is sounding off.. I thought that calling tone(pin,freq); would just do the trick, but it did not..

the RX2 chip is a decoder so no need to decode signals coming from the transmitter. can be forward/backward/forward+left/forward+right/bacward+left/backward+right/turbo+left/turbo+right. every function. there is a certain pin that would output HIGH for forward, another pin for reverse, etc etc...

so what I would "get" from the RX2 chip is a "set" of lows and HIGHs...meaning RX2 has 5 pins for those "outputs"...Forward/Reverse/Left/Right/Turbo(seldom used).... for example, I get a 1000 for forward, 1(for A0), 0(for A1), 0(for A2), 0(for A3).... for the reverse, will be 0100, 0(for A0), 1(for A1), 0(for A2), 0(for A3).. .and for that crabwalk_left(), I should get a 1010 , 1(for A0), 0(for A1), 1(for A2) , 0(for A3).... whew :) its an or/and function...

my problem is how I would write those digitalinputs for switch case? because samples I see are usually from serial data, or from just a single pin input (with 1 or 0 only)...

once again, my hardware is perferctly working with the above code/link I have posted/found on the net...maybe tomorrow I will post a video of it doing all those movements...

but 1 thing still remains, HOW do I code it RIGHT? :D

I learn faster through actual code samples... goodnyt all! :)


You have already done that with the statement motion= digitalRead(A0)+ digitalRead(A1)*2+ digitalRead(A2)*4+ digitalRead(A3)*8+ digitalRead(A4)*16; The problem is, as Chris says, that all the outputs are not mapped. When you press say A1 with A2, motion stores the value 6 which in binary is 0b00000110 which as I see from your above code is a case that has not been defined. For a successful coding, you should define all possible combinations no matter how unlikely they are to occur. You have defined outputs for the values 1 ,2,4,5,8,9 but not for values 3,6,7,10,11,12 etc. (convert to binary yourself). If you define all possible outcomes, your code won't be bugged.

I found it on the net, copied, AND IT WORKED.. thats what mattered the most.. :D I *never* did that .. :D

anways, the code works like a charm, my bot is up and "running" already... forward/reverse/rotate left & right, and crabwalk left & right with a combination of forward/left or forward/right.... will post a video soon

ok i have abetter question.how would you go about getting the data from all the 4 digitalpins  and making it into one?

    case 0b00000001:  forward();        break;
    case 0b00000010:  backward();       break;
    case 0b00000100:  rotate_left();    break;
    case 0b00001000:  rotate_right();   break;
    case 0b00000101:  crab_left();      break;
    case 0b00001001:  crab_right();     break;
    default:  servoENABLE();  

You might need to declare a variable and initialize it to PINC, but, it should work otherwise.

Realize that I am not fluent in arduino, nor do I have an arduino to play with.

The code you have given and the code that he is using is no different. Changing the name of the variable doesn't have any change. The compiler doesn't care if you use PINC our motion (unless PINC is a KEYWORD) as your variable. What it cares about is the value stored in that variable (this should hold true if arduino is really based on C as it holds true in C). Define more outputs. And post the complete code. There might be some problem with the functions that you are using and not with the switch case.

Our there can also be some problem in the way you have wired your motors and servos

is indeed a keyword. It is meant to read the status/bits of PortC, which happens to be the 6(8) analog pins including the pins he is currently planning on reading.

I will agree that there doesn't seem to be a reason to read things differently.

He asked. I answered.

The while loop method that I suggested above is a complete waste as I see that the loop() already does that.

What I said above is only true based on what I am guessing is in your motor routine functions. For all I know, you could have something funny in there that is keeping your code locked up. If those routines are what I think they are --I.e., set the motor direction and return, then what I said above is true.

Binary into constant... The easist way would be to simply use actual numbers instead of binary.

case 1:
case 2:
case 3:

Expanding on that, you could define these constants with names up at the top of your program:

#define want_to_go_forward 1
#define want_to_go_right 2

This would make it easier to read and understand your code


Now, in terms of watching for different buttons that are being hit. The problem is not because you are using IF's or Case, it is the fact that you have not told the robot what to do when 2 buttons are being pushed. You are wicked close though. First off, you have assigned different values to each of your inputs so when they are added, you can figure out (with math) what has been hit. However, you don't have a case for say, 17 --which is forward and crab right mixed together. Same with 5 --forward and rotate left mixed together. 

That's really it. Just add some more cases so you have all the combinations of buttons covered.