# help with switch/case statment

New to this, and making progress. Have everything working, but can't get my first switch/case statement. I am having to decode a 4 digit number into a single digit, then swap the decimal to hexadecimal, and then display it on my LCD.

I try to keep my codes in sections as I am new and it helps my thought process. The following is a section of the code that isnt working.

if (timer==0){ // DISPLAY NEW NUMBER, if timer = 0 then a button has jsut been pressed digit1=(count/1000); // pulls 1's from count digit2=((count/100)-(digit1*10)); // pulls 10's from count digit3=((count/10)-((digit2*10)+(digit1*100))); // pulls 100's from count digit4=((count)-((digit3*10)+(digit2*100)+(digit1*1000))); // pulls 1000's from count decode(digit1); // decode singles to hexadecimal decode(digit2); // decode tens to hexadecimal decode(digit3); // decode hundreds to hexadecimal decode(digit4); // decode thousands to hexadecimal write_lcd_numbers(digit4, digit3, digit2, digit1); } // display count checkLEFTbutton; checkRIGHTbutton; checkPAIRbutton; ++timer; }

This is the code for displaying the LCD: // -----------------------| DISPLAY LCD NUMBERS |------------------- void write_lcd_numbers(int digit1, int digit2, int digit3, int digit4){ spi_transfer(digit1); // Thousands Digit spi_transfer(digit2); // Hundreds Digit spi_transfer(digit3); // Tens Digit spi_transfer(digit4); // Ones Digit digitalWrite(SLAVESELECT, HIGH); // start slave device listening delay (10); // wait just because I saw it in lots of others code digitalWrite(SLAVESELECT, LOW); // release chip, signal end transfer }// ------------------------------------------------------------

Here is the coed where I try the case/switch // ---------------------| DECODE FROM DEC. TO HEX |-------------------- void decode(int val){ switch (val){ case 0: val=0x3F; break; case 1: val=0x06; break; case 2: val=0x5B; break; case 3: val=0x4F; break; case 4: val=0x66; break; case 5: val=0x6D; break; case 6: val=0x7D; break; case 7: val=0x27; break; case 8: val=0x7F; break; case 9: val=0x67; break; } }// --------------------------------------------------------------------

Also, I can type digit1=0x74 and it displays as it supposed to. The count is a variable defined elsewhere in the code. For right now I simply have it set in the setup as 2345.

I was thinking about it think i may have found it but not sure. Can I use this with multiple variables? Can I decode(digit1) and decode(digit2)... or do I have to create more cases?

A lookup table would be simpler

could you provide a link, example, or a brief explanation?

``````void decode(int val){
switch (val){
case 0:
val=0x3F;
break;
``````

Your function doesn't return anything. Try:

``````int decode(int val){
switch (val){
case 0:
return 0x3F;
``````

or, as AWOL said, a lookup table would be shorter:

``````const byte sevenSegLookup []= { 0x3F, 0x06, 0x5B ....};
``````

When posting code, can you please use the "#" (Code) button on the post editor's toolbar?

I looked at it again. To work like this:

``````  digit1=(count/1000);                                          // pulls 1's from count
digit2=((count/100)-(digit1*10));                         // pulls 10's from count
digit3=((count/10)-((digit2*10)+(digit1*100)));             // pulls 100's from count
digit4=((count)-((digit3*10)+(digit2*100)+(digit1*1000))); // pulls 1000's from count
decode(digit1);                                            // decode singles to hexadecimal
decode(digit2);                                             // decode tens to hexadecimal
decode(digit3);                                                 // decode hundreds to hexadecimal
decode(digit4);
``````

you'd have to use references:

``````void decode(int[glow]&[/glow] val){
switch (val){
case 0:
val=0x3F;
break;
``````

with that I get
decode was not declared in this scope.

Add a `void decode(int &val);` at the top of your sketch

now all sorts of stuff. I will try posting the complete code in a second.

It compiles for me.

Have you got:`void decode(int& var);` at the top, and

``````void decode(int& var)
``````

later?

no, I only have it once. I am missing what you are trying to say. I dont understand what you mean once at the top and then later at the bottom. Is this of the function or setup?

The code I posted doesnt have the “&” in it. I will repost the second half with the “&” in it.

Is this of the function or setup?

Neither, it's a prototype.

Simply put `void decode(int& var);` as your very first line of code.

Thanks, it compiled. I didnt know that it had to go to the very first. Is there a reason it has to go at the top before other declarations? Now to go try it out.

THANK YOU! It worked perfect. I guess I need to spend more time reading about C, but I so much more enjoy making stuff. It now works.

The reason it has to go at the top is that in C++, every function has to have either a full definition, or a prototype before it is referenced. Normally, the Arduino hides this from you by generating prototypes invisibly for you, so the compiler doesn't complain.

However, the technique used (the "int&", called a reference) for some reason doesn't yet work, so you have to put the prototype in yourself.

You could also have got around this by putting the whole of "decode" anywhere before "loop".

(I still think a lookup table would be simpler ;) , but glad you got it going - welcome! )

thanks, I will look up "prototype" this afternoon then. I am slowly getting there and appreciate your patience.