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.
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 :) And don't forget to upload a vid soon. Will be waiting for that. ;)

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

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 :)

THIS even works! golly :-)

 

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

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

 

okhkie... will try that one out later... after I upload the video... :)

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

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

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

If the only place the binary numbers appear in your code is the set of case statements in your switch, add a comment before the switch detailing what each bit is connected to. Or, maybe a remark next to each case statement as to which pins are being called.

As to why you had to set your pin to output before you could use it, arduino defaults all pins as input. You must specifically call for pins to be set as outputs (I learned that through some of my reading to find the PINC command).