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

Thanks,

-Raff

Comment viewing options

Yeah, I too think that Chris if right although it if not necessary that you use #define to declare a global scope variable which is pre processed by the compiler (global scope variables exist throughout the life of the function ie until the batteries are pulled). You can follow my way our that of Chris. Both are correct.

Why are you using so many analog inputs for digital? And, what NilsB is saying is something one learns in programming courses. Make your variable names as explicit as possible. For example if you write a program to calculate sales tax on a dinner bill, you will have the sale, the tax, the subtotal, and the total at a minimum. Calling them n1, n2, n3, n4 will not help you down the road when you come back to look at the code.

I believe what he is saying is turn your binary constants/variables into named variables/constants, so, you can look at the code and not have to worry about wth does 0b00000100 mean again?

well ok here is the main connections to the duino board (I use digital I/O pins 2-13 for the 12 servo connections, Im leaving digital I/O 0 and 1 for the rx/tx).. now I am left with using AO-A5(or digital I/O 13-19) for connection to an RX2 chip, those chips found on cheap R/C toys.. they have 5 decoded outputs there, but I am using only 4 at the moment.. A0=forward A1=reverse A2=left A3=right, A4=unused... so this means I will have an extra I/O here (A5) to use as OUTPUT... now im utilizing the A0-A5 as digital INPUTS, not analog.. I have tried using if -else-if but my problem is if I press the forward, and press the left, the code will still do the "forward" function.. that is why I use the switch case..I am still learning using this though...

about that binary constants into variables, can you show me how this can be done? I hope I made myself clear in my posts above.. sorry If I cant explain much regarding code..

The RX2 is paired with a TX2 for Radio Control. They are found in a number of inexpensive RC vehicles. Most times the remote only has left/right, forward/reverse. The turbo is not always brought out.

You talk about pins. A pin is a very low level concept.

Could it be that your problem decomposition uses terms like distance, direction and speed ? If so then you could reflect that in your code. The more your domain specific terms are used in the code the more the code speaks to you.

Finding a switch with 0b00001111 as cases seems very odd (as it is shown in the example). Will you understand its meaning one week after writing? Having a switch statement that spans over several screen pages can overstrain our intellectual abilities. Will you understand this after a while?

There is nothing wrong using a binary bitmap but it can be hidden using variables with domain specific names.

Hey Nils,

pardon my coding, still a n00bie...just needed a quick solution to my "slight" problem.

The code linked below, works OK since all the Analog inputs(A0-A7) are used as inputs. at the moment, Im just using A0-A3. there is a slight problem though, I can't use A5 as output(for use on piezo buzzer)..just a quick solution to a problem :P oh heck, need more learning.

unsigned char motion=0;

void setup()
{
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);

}

void loop()
{

switch(motion)
{
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();
}
}

First of all, all pins are in Input mode at start up. You only need to declare them Output if needed. Secondly, you can use any analog pins as output for anything, even if you use the other pins for input or for analog read. So yeah, you can declare A4 as output and use a piezo speaker on it. Remember, all the pins are I/O pins, some of them have additional functions that need to be declared to be used. Arduino IDE declares them when needed, except the Output mode that you have to do it yourself and the pull up resistors for the input mode.

That is fine. But what is the meaning of A0 for you? What "sense" is it? Why is the combination of A0 and A2 lead to crab-left?

You intensional hide information here and this can lead to code-horror since you loose the connections between the dots. The concept is hidden.

To stay explicit and intension revealing is always good. Especially if you are new to programming.

So show the concepts wherever you can. Your mind will thank you for that ;-)

After searching for more, found this..