Pages: [1]   Go Down
Author Topic: Switch Case Usage  (Read 881 times)
0 Members and 1 Guest are viewing this topic.
Madison, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Exploring Electrons. . .
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I'm trying to use the 'switch case' to choose between cases with my accelerometer:

Code:
 switch(axis) {
    case 'X':
      code = 0x06; //Return register for X data
      return code;
      Serial.println("Found X");
      break;
    case 'Y':
      code = 0x07;
      return code;
      Serial.println("Found Y");
      break;
    case 'Z':
      code = 0x08;
      Serial.println("Found Z");
      return code;
  }

the serial println's are there for debugging, I never see any output so it's never getting to any of the cases.  I'm pretty sure it's because it's not correctly comparing the axis being passed in to the character in each case.

Here's the calling code:

Code:
 //Handle Accelerometer

    Serial.print("X Data: ");
    xValue = getAccelData(X);
    Serial.println(xValue);
    
    Serial.print("Y Data: ");
    yValue = getAccelData(Y);
    Serial.println(yValue);
    
    Serial.print("Z Data: ");
    zValue = getAccelData(Z);
    Serial.println(zValue);
    delay(500);

Can you only use integers with switch cases?
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Doesn't the "return code;" line cause the program to exit the subroutine, so it never reaches the println command?  Try putting the println before the return.

Also, you can put a "default" case in your switch statement, just to confirm it's not picking any of the cases.
http://arduino.cc/en/Reference/SwitchCase

Good luck!
Logged

Madison, WI
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Exploring Electrons. . .
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, it does :-)

Can you use characters as case variables though?  I have the code working using integers but i really want to use characters as it makes the code so much clearer.

IE this works:

Code:
 switch(axis) {
    case 1:
      code = 0x06; //Set register for X data
      break;
    case 2:
      code = 0x07;
      break;
    case 3:
      code = 0x08;
      break;
  }
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use characters instead of integers.

This code works.  Hopefully it'll help you see what you need to do.

Code:
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
}

void loop()                       // run over and over again
{
  int value;
  
  value = getAccelData('X');      // Notice that 'X' is in single quotes
  Serial.println(value);
  value = getAccelData('Y');      // Also, case is important
  Serial.println(value);
  value = getAccelData('Z');
  Serial.println(value);
  value = getAccelData('@');
  Serial.println(value);
}

int getAccelData(char axis)        // Notice that axis is defined as type char
{
  int code;
  switch(axis) {
    case 'X':                               // Notice 'X' is in single quotes
      Serial.println("Found X");
      code = 0x06; //Return register for X data
      return code;
      break;
    case 'Y':
      Serial.println("Found Y");
      code = 0x07;
      return code;
      break;
    case 'Z':
      Serial.println("Found Z");
      code = 0x08;
      return code;
      break;
    default:
      Serial.println("default");
      return 0;
  }
}
« Last Edit: December 06, 2009, 12:54:15 am by Kitep » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26489
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Minor point: All those "break;" s are redundant.

Why bother with a switch? Why not use a simple lookup table?
Or even simple arithmetic?
[uncompiled, untested]
Code:
int getAccelData(char axis)      
{
  if (axis >= 'X' && axis <= 'Z')
     return (axis - 'X') + 6;  
  else
     return 0;
}
or, shorter yet:

Code:
int getAccelData (char axis)      
{
     return (axis >= 'X' && axis <= 'Z') ? (axis - 'X') + 6 : 0;  
}

Of course, you may want to define 6 as something meaningful.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

AWOL, I agree your code will work, and it's clever.

However, "clever" code is usually harder to maintain in the future, either for others or yourself in a few months.  It doesn't adapt well to new requirements.  And it can pin the compiler down, even if the compiler could have done it in fewer machine operations on a particular processor.

Write the intent clearly, even if it takes a little more room in the editor window.  Let the C compiler figure out the best way of squeezing the actual logic into machine language.
Logged

Pages: [1]   Go Up
Jump to: