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

 

Thanks,

-Raff

 

 

 

 

 

 

Comment viewing options

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

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

Hi Raffx, the problem you have raised above seems a trivial one to me. I am new to electronics so I am assuming that the RX 2 chip you mentioned above is used by the bot to read the input from an infrared controller. If so, then the only problem with the coding can be that you are not reading the chip sufficient number of times. What I understand is that your bot responds to the first command and none after that. The sole reason is that during coding, you have not asked it to read the input from the chip again. As a result, your bot only performs your first order until you pull out the batteries. If this is indeed the problem, then the solution is to use a while loop before you perform your first read. This will make sure that the bot keeps on reading the chip and performing the orders until you pull out the batteries. I can't help you with the exact coding as despite my being old to C++, I'm new to wiring and my codes will only be flawed and incorrect. As to what Nils says is that he asks you to name you variables and not use binary figures. This is easy. Right at the start of your function name the variable like here in LMR, each one of us has a unique user id but we still refer to each other using nicknames. In the background admin knows that if my name is mentioned, it refers to my specific user id but we still use names. Do this right at the begining of your function- declare long forward =0000001 or what ever you are trying to assign. The syntax is- <const> <variable type> <variable name>=<value of variable>. Doing this helps people other than you understand what you are trying to do and helps in debugging the code. Hope this helps.

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()
{
  motion = digitalRead(A0) + digitalRead(A1)*2 + digitalRead(A2)*4 + digitalRead(A3)*8 + digitalRead(A4)*16;
 
  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..

http://www.linksprite.com/product/showproduct.php?lang=en&id=184