Pages: [1]   Go Down
Author Topic: Is there an alternative to 38 Switch-case statements?  (Read 710 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all,

I'm using a rotary encoder and assigning values to each encoder position up to 38. Each case assigns two variable values, one int and one string.

Are there a more memory friendly method? This one takes around 5K.

Thanks

Code:
switch(encoderPos){
      case 0:
        trigval = 0;
        trigdisp = ("0");       
      case 1:
        trigval = 250;
        trigdisp = ("1/4000");
        break;
      case 2:
        trigval = 313;
        trigdisp = ("1/3200");
        break;
...
      case 37:
        trigval = 10000000;
        trigdisp = ("10");
        break;
      case 38:
        trigval = 30000000;
        trigdisp = ("30");
        break;
Logged

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

Lookup table(s)?
(though that's probably what the compiler makes the switch statement into)
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.

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6141
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I know this isn't related to your question, but did you mean to miss a "break;" on case 0?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 64
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good eye James.

Thanks AWOL.
Logged

New Jersey
Online Online
Faraday Member
**
Karma: 65
Posts: 3639
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Would this help any, or does use of sprintf make it worse?
Code:
const unsigned long times[] = {4000,3200,1600,1000,500,250,125,60,30,15,8,4,2,1,10,30};
char trigdisp[8];

if(encodepos==0)
  {
  trigval=0;
  sprintf(trigdisp,"%d",0);
  }
else if(encoderpos<13)
  {
  trigval=1000000UL/times[encoderpos-1];
  sprintf(trigdisp,"1/%d",times[encoderpos-1]);
  }
else
  {
  trigval=1000000UL*times[encoderpos-1];
  sprintf(trigdisp,"%d",times[encoderpos-1]);
  }       
I have fewer shutter speeds (assuming that's what they are) than you of course.
Not tested, not even compiled.
Logged

Offline Offline
Edison Member
*
Karma: 43
Posts: 1559
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use an array of structures to hold the data which are then easily accessed directly through an array reference. It gets rid of the switch or if statements entirely.
Code:
struct triggers {
long trigval;
char *trigdisp;
} trigger_tab[5] = {
0, "0",
250,"1/4000",
313,"1/3200",
// ...
10000000L,"10",
30000000L,"30"
};

void setup(void)
{
  Serial.begin(9600);
  for(int i=0;i<5;i++) {
    Serial.print(trigger_tab[i].trigval);
    Serial.print(" ");
    Serial.println(trigger_tab[i].trigdisp);
  }
 
}

void loop(void)
{
}
Just fill in all the other values and change [5] to [39]
This particular code is 2476 bytes.
Pete
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Sr. Member
****
Karma: 1
Posts: 322
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you looked at PROGMEM?
http://www.arduino.cc/playground/Main/PROGMEM

A simple table lookup with the table in flash could be your best bet.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6141
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Have you looked at PROGMEM?
A simple table lookup with the table in flash could be your best bet.
Code is stored in progmem...

The select-case statements are code for a type of look-up table...

So isn't a lookup table in flash what the original code already accomplishes?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Pages: [1]   Go Up
Jump to: