Peculiarity with Switch Case Statement..

Hi there,

Arduino newbie here.. :)

I'm in the process of experimenting with a little vehicle, a couple of motors, 2 wheels, a sensor on the front for obstructions.. the next step up from Hello World I guess :)

I have the two motors working via a SN754410 Quad Half H-Bridge IC, 4 digital outputs driving it.. the distance sensor on the front is hooked up to an Analog Input..

I have the vehicle moving forward and stopping appropriately with the following code:

void loop()                     
{
  float volts = analogRead(IRpin);   
  if (volts > 200)
  { 
    digitalWrite(2, HIGH);
     digitalWrite(3, LOW);
     digitalWrite(4, HIGH);
     digitalWrite(5, LOW);
   }    
   else
  {
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
  }  
}

Seeking to start extending the functionality I moved the control of the vehicle out into a procedure.. one of the parameters of the procedure is the direction I want it to travel, there's a SwitchCase statement in the procedure to process it:

void RobotGo( int direction ) 
{
   switch( direction )
    {
      case 3 :  //robotSTRAIGHT
           { 
           digitalWrite(2, HIGH);
           digitalWrite(3, LOW);
           digitalWrite(4, HIGH);
           digitalWrite(5, LOW);
           }
           
      case 5 :  //robotSTOP          
          digitalWrite(2, LOW);
          digitalWrite(3, LOW);
          digitalWrite(4, LOW);
          digitalWrite(5, LOW);
            
    }  
}


void loop()                     
{
  float volts = analogRead(IRpin);   
  if (volts > 200)
  { 
   RobotGo( 3 );
   }    
  
  else
  {
   digitalWrite(2, LOW);
   digitalWrite(3, LOW);
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
  }  
}

But this doesnt work.. the vehicle doesn't move.

With much experimenting I've found that introducing a delay within the switch statement causes it to work, you can see the change I made below:

void RobotGo( int direction ) 
{
   switch( direction )
    {
      case 3 :  //robotSTRAIGHT
           { 
           digitalWrite(2, HIGH);
           digitalWrite(3, LOW);
           digitalWrite(4, HIGH);
           digitalWrite(5, LOW);
          delay(50); //this pause makes it work!
          }
          
           
      case 5 :  //robotSTOP          
          digitalWrite(2, LOW);
          digitalWrite(3, LOW);
          digitalWrite(4, LOW);
          digitalWrite(5, LOW);
            
    }  
}

Outputting some value to the serial port instead of using the Delay statement also works.. I'm quite puzzled the code works just fine if entirely contained with Void Loop.. but I move it out, and parameterize it and it fails..

Where have I gone wrong?

Duncan.

Duncan,

First of all, you don't need braces under each case. Second, one of the peculiarities of switch/case is that a case will fall through to the next case unless you end it with a break statement. You can use this to your advantage to do an "OR" type case statement:

switch (i)
{
case 1:
case 2:
  j = i;
  break;
case 3:
case 4:
  j = i / 2;
  break;
}

Here is a corrected version of your code:

void RobotGo( int direction )
{
   switch( direction )
    {
      case 3 :  //robotSTRAIGHT
           digitalWrite(2, HIGH);
           digitalWrite(3, LOW);
           digitalWrite(4, HIGH);
           digitalWrite(5, LOW);
           break;
          
      case 5 :  //robotSTOP          
          digitalWrite(2, LOW);
          digitalWrite(3, LOW);
          digitalWrite(4, LOW);
          digitalWrite(5, LOW);
          break;   
    }  
}

Regards,

-Mike

Hi Mike..

Thanks so much for taking the time to respond.. Much appreciated! cant wait to get home and try it after work! :)

Cheers, Duncan.