Switch case statement for multiple digitalRead inputs?

Code sample

This is the only thing I can think if which will help-

#define Forward 1 or #define Forward 0b00000001

Do this with all your binaries ie Reverse gets defined as 2 and so on. Then in the switch statement do this-

case Forward: forward(); break;

and rewrite all cases in a similar manner. I hope that this should work. Compiler replaces the value of Forward by 1 where ever it sees it while the program is compiled. I’ve never tried it but theoretically, it is correct.

If it doesn’t work, then add notes next to the binaries like-

case 0b00000001: forward(); break; //this makes the crab go forward

realize

that #define makes a global constant whereas const int Forward = 1; can be placed inside a function.

Yeah you are right

I know that when I declare it in global scope, it runs throughout the execution of the program but it also helps him as if he has used binary anywhere else, he can replace that too. const int is good if the execution memory of the program is low and chances are that he will run out of memory but I don’t think that is going to be the case

Hi Raffx, the problem you

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.

Ohk, I give in bird

Here are the changes

 

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;
  const int Forward= 0b00000001;        //or const int Forward=1; 

  const int Backward=0b00000010;      //or const int Backward=2;

  const int Rotate_left=0b00000100;    //or const int Rotate_left=4;

  const int Rotate_right=0b00001000;  //or const int Rotate_right=8;

  const int move_left=0b00000101;      //or const int move_left=3;

  const int move_right=0b00001001;    //or const int move_right=9;

  switch(motion)

  {
    case Forward:  forward();        break;
    case Backward:  backward();       break;
    case Rotate_left:  rotate_left();    break;

    case Rotate_right:  rotate_right();   break;
    case move_left:  crab_left();      break;
    case move_right:  crab_right();     break;
    
    default:  servoENABLE();  
  }
}

 

As an aside,

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.

okhkie… will try that one

okhkie… will try that one out later… after I upload the video… :slight_smile:

hey

this is what I tested JUST NOW

#define FWD 1     //decimal of 0b00000001
#define BCK 2      //decimal of 0b00000010
#define LFT 4       //decimal of 0b00000100
#define RYT 8      //decimal of 0b00001000

//…and so on and so forth…

void setup()
{
 pinMode(A0,INPUT);    //DO I  STILL have to declare this pins as INPUT? btw @ startup, is this ANALOG IN or DIGITAL IN?
 pinMode(A1,INPUT);
 pinMode(A2,INPUT);
 pinMode(A3,INPUT);
 pinMode(A4,INPUT);
 pinMode(19,OUTPUT);  //buzzer  <— here I declared it to be an output
 }

 

void loop()
{
  //motion = digitalRead(A0) + digitalRead(A1)*2 + digitalRead(A2)*4 + digitalRead(A3)*8 + digitalRead(A4)*16;
 
  switch(PINC)
  {
    case FWD:  forward();        break;
    case BCK:  backward();       break;
    case LFT:  rotate_left();    break;
    case RYT:  rotate_right();   break;
    //case 0b00000101:  crab_left();      break;
    //case 0b00001001:  crab_right();     break;
   
    default:  servoENABLE(); 
  }
}

I rem’ed out the crabwalks (just to isolate it)… so IM REALLY DOUBLE SURE the code DID update … lol… since at startup, all pins are set to INPUT, one should declare a pin as output to use it as output… I dunno if the above code still has bugs… but hey, it works :slight_smile:

Congratulations :smiley:
Well the above code does look fine. Try experimenting with the crab left and right enabled. I don’t know about PINC though. Try experimenting with that too. The more you experiment, the more all of us learn :slight_smile: And don’t forget to upload a vid soon. Will be waiting for that. :wink:

AND

THIS even works! golly :slight_smile:

 

void setup()
{
 pinMode(19,OUTPUT);  //buzzer
 }

 

void loop()
{
  switch(PINC)
  {
   
    case 1:  forward();        break;
    case 2:  backward();       break;
    case 4:  rotate_left();    break;
    case 8:  rotate_right();   break;
    case 5:  crab_left();      break;
    case 9:  crab_right();     break;

   
    default:  servoENABLE(); 
  }
}

 

MUCH MUCH simpler…

I think im going to stick

I think im going to stick with PINC at the moment… check out my latest post, its ALL(moves) in there :slight_smile: but Im still thinking of other “fun” moves as well… sheesshhhhh its taking AGES just to upload a 3Mb video file… what theeeeeeeeeee… :stuck_out_tongue: