Project

General

Profile

switch case statement

Added by WilcoE almost 6 years ago

Hi,

I’m building a wireless doorbell based on a JeeNode.
The doorbell has alot of sounds and it is possible to change this wireless.

I have only a problem with the case statement, is it possible to change this code to less code? i need 130 cases so it’s using alot of bytes.

switch( value ) {
case 0:
player.play_P(song_0, octave);
break;
case 1:
player.play_P(song_1, octave);
break;
case 2:
player.play_P(song_2, octave);
break;
case 3:
player.play_P(song_3, octave);
break;
case 4:
player.play_P(song_4, octave);
break;
case 5:
player.play_P(song_5, octave);
break;
case 6:
player.play_P(song_6, octave);
break;
case 7:
player.play_P(song_7, octave);
break;
case 8:
player.play_P(song_8, octave);
break;
case 9:
player.play_P(song_9, octave);
break;
case 10:
player.play_P(song_10, octave);
break;
case 11:
player.play_P(song_11, octave);
break;
case 12:
player.play_P(song_12, octave);
break;
case 13:
player.play_P(song_13, octave);
break;
case 14:
player.play_P(song_14, octave);
break;
case 15:
player.play_P(song_15, octave);
break;
case 16:
player.play_P(song_16, octave);
break;
case 17:
player.play_P(song_17, octave);
break;
case 18:
player.play_P(song_18, octave);
break;
case 19:
player.play_P(song_19, octave);
break;
case 20:
player.play_P(song_20, octave);
break;
case 21:
player.play_P(song_21, octave);
break;
case 22:
player.play_P(song_22, octave);
break;
case 23:
player.play_P(song_23, octave);
break;
case 30:
player.play_P(song_30, octave);
break;
case 31:
player.play_P(song_31, octave);
break;
case 32:
player.play_P(song_31, octave);
break;
}


Replies (4)

RE: switch case statement - Added by mjl almost 6 years ago

WilcoE wrote:
> Hi,
>
> I’m building a wireless doorbell based on a JeeNode.
> The doorbell has alot of sounds and it is possible to change this wireless.
>
> I have only a problem with the case statement, is it possible to change this code to less code? i need 130 cases so it’s using alot of bytes.
>
> switch( value ) {
> case 0:
> player.play_P(song_0, octave);
> break;
> case 1:
> player.play_P(song_1, octave);
> break;
> case 2:
> player.play_P(song_2, octave);
> break;
> case 3:
> player.play_P(song_3, octave);
> break;
> case 4:
> player.play_P(song_4, octave);
> break;
> case 5:
> player.play_P(song_5, octave);
> break;
> case 6:
> player.play_P(song_6, octave);
> break;
> case 7:
> player.play_P(song_7, octave);
> break;
> case 8:
> player.play_P(song_8, octave);
> break;
> case 9:
> player.play_P(song_9, octave);
> break;
> case 10:
> player.play_P(song_10, octave);
> break;
> case 11:
> player.play_P(song_11, octave);
> break;
> case 12:
> player.play_P(song_12, octave);
> break;
> case 13:
> player.play_P(song_13, octave);
> break;
> case 14:
> player.play_P(song_14, octave);
> break;
> case 15:
> player.play_P(song_15, octave);
> break;
> case 16:
> player.play_P(song_16, octave);
> break;
> case 17:
> player.play_P(song_17, octave);
> break;
> case 18:
> player.play_P(song_18, octave);
> break;
> case 19:
> player.play_P(song_19, octave);
> break;
> case 20:
> player.play_P(song_20, octave);
> break;
> case 21:
> player.play_P(song_21, octave);
> break;
> case 22:
> player.play_P(song_22, octave);
> break;
> case 23:
> player.play_P(song_23, octave);
> break;
> case 30:
> player.play_P(song_30, octave);
> break;
> case 31:
> player.play_P(song_31, octave);
> break;
> case 32:
> player.play_P(song_31, octave);
> break;
> }

You can define an array of song names and index the array by song number:

char *songs[] = {
  "song_1", 
  "song_2",
  [...]
  "song_130"
};

int song_nr;

[...]

player.play_P(songs[song_nr], ocatve)

RE: switch case statement - Added by dabbishaw almost 6 years ago

It would be quite interesting to compare the compiled size between the switch statement and the array method. It might show how good the compiler really is.

RE: switch case statement - Added by padvinder95 almost 6 years ago

I take it the songs are somehow stored in memory and song_X is a pointer to the location. Then you could do something like mjl suggests, but you don’t want strings, but the pointers themselves. Find out what the type of the pointer is, for example a byte pointer, then put all those pointers in 1 array:

byte* songs[] = {song_1, song_2, song_3, ... song_130};

void play(value){
  player.play_P(songs[value]);
}

The cost is one big array with 131 pointers, which will go in RAM; the benefit is a lot less statements in the flash memory. You could even put the pointer array in progmem, I think, which would cause the big array to go in flash: you’d still gain, because all the pointers song_X are in flash in both the switch/case and this option, but for the switch case (see what I did there?) you’d need room for 131 compares—I don’t think the compiler checks whether all compares are consecutive and can thus be changed for an array…

RE: switch case statement - Added by WilcoE almost 6 years ago

Thanks for the answers, i will try things out.
and will post a update of the script on my Github

[[[https://github.com/WilcoE/doorbell]]]

    (1-4/4)