10 equal functions in switch but with 10 buttons

Each case takes about 400 bytes with each instruction with rotary encoder in coordinate position which lead to roughly 4000 bytes. I want to reduce to just 800 bytes with default if it is possible.

Here's example: case 1 copy to default to do instruction

case 2 copy to default to do same instruction

case 3 copy to the default instruction

default while no any button is pressed so it reads the rotary encoder which read left or right at coordinate position. When it detects 1 of 10 buttons, it copy float size from the default to the previous button I has pressed then go to any of the case.

Is that possible in C ?

Why won't the same 400 bytes deal with all the different cases with one or two variables to distinquish one from the other.

Post your code.

...R

Do you mean something like this? case 1: case 3: DoThis(); break; case 2: DoThat(); break;

Here!

    switch ( P_13_4 ) {
     case 1:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_01_x;    point_xplus();    waypoint_01_x = waypoint_point_x;   }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_01_x;    point_xminus();   waypoint_01_x = waypoint_point_x;   }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_01_y;    point_yplus();    waypoint_01_y = waypoint_point_y;   }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_01_y;    point_yminus();   waypoint_01_y = waypoint_point_y;   }
     break;
     case 2:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_02_x;    point_xplus();    waypoint_02_x = waypoint_point_x;   }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_02_x;    point_xminus();   waypoint_02_x = waypoint_point_x;   }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_02_y;    point_yplus();    waypoint_02_y = waypoint_point_y;   }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_02_y;    point_yminus();   waypoint_02_y = waypoint_point_y;   }
     break;
     case 3:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_03_x;    point_xplus();    waypoint_03_x = waypoint_point_x;   }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_03_x;    point_xminus();   waypoint_03_x = waypoint_point_x;   }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_03_y;    point_yplus();    waypoint_03_y = waypoint_point_y;   }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_03_y;    point_yminus();   waypoint_03_y = waypoint_point_y;   }
     break;
     case 4:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_04_x;    point_xplus();  waypoint_04_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_04_x;    point_xminus(); waypoint_04_x = waypoint_point_x;     }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_04_y;    point_yplus();  waypoint_04_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_04_y;    point_yminus(); waypoint_04_y = waypoint_point_y;     }
     break;
     case 5:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_05_x;    point_xplus();  waypoint_05_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_05_x;    point_xminus(); waypoint_05_x = waypoint_point_x;     }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_05_y;    point_yplus();  waypoint_05_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_05_y;    point_yminus(); waypoint_05_y = waypoint_point_y;     }
     break;
     case 6:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_06_x;    point_xplus();  waypoint_06_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_06_x;    point_xminus(); waypoint_06_x = waypoint_point_x;     } 
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_06_y;    point_yplus();  waypoint_06_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_06_y;    point_yminus(); waypoint_06_y = waypoint_point_y;     }
     break;
     case 7:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_07_x;    point_xplus();  waypoint_07_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_07_x;    point_xminus(); waypoint_07_x = waypoint_point_x;     }    
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_07_y;    point_yplus();  waypoint_07_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_07_y;    point_yminus(); waypoint_07_y = waypoint_point_y;     }
         break;
     case 8:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_08_x;    point_xplus();  waypoint_08_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_08_x;    point_xminus(); waypoint_08_x = waypoint_point_x;     }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_08_y;    point_yplus();  waypoint_08_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_08_y;    point_yminus(); waypoint_08_y = waypoint_point_y;     }
     break;
     case 9:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_09_x;    point_xplus();  waypoint_09_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_09_x;    point_xminus(); waypoint_09_x = waypoint_point_x;     }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_09_y;    point_yplus();  waypoint_09_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_09_y;    point_yminus(); waypoint_09_y = waypoint_point_y;     }
     break;
     case 10:
         if ( P_13_4 == 11 )  {    waypoint_point_x = waypoint_10_x;    point_xplus();  waypoint_10_x = waypoint_point_x;     }
         if ( P_13_4 == 12 )  {    waypoint_point_x = waypoint_10_x;    point_xminus(); waypoint_10_x = waypoint_point_x;     }
         if ( P_13_4 == 13 )  {    waypoint_point_y = waypoint_10_y;    point_yplus();  waypoint_10_y = waypoint_point_y;     }
         if ( P_13_4 == 14 )  {    waypoint_point_y = waypoint_10_y;    point_yminus(); waypoint_10_y = waypoint_point_y;     }
     break;
     }

void point_xplus()  {
 if ( waypoint_point_x >= point_min && waypoint_point_x <= point_max) { ++waypoint_point_x; stepper_5plus16();  } else { portf(); } }
void point_xminus() {
 if ( waypoint_point_x >= point_min && waypoint_point_x <= point_max) { --waypoint_point_x; stepper_5minus16(); } else { portf(); } }
void point_yplus()  {
 if ( waypoint_point_y >= point_min && waypoint_point_y <= point_max) { ++waypoint_point_y; stepper_5plus4();   } else { portf(); } }
void point_yminus() {
 if ( waypoint_point_y >= point_min && waypoint_point_y <= point_max) { --waypoint_point_y; stepper_5minus4();  } else { portf(); } }

As you see, I have done something with case 1, 2, ... 10 that the button is same as a button and it connects to the exact rotary encoders until other button is pressed, then it connects to two specific rotary encoder while the first is connected off automatic, it is for to save pins while it is possible than reading 50 pins in array which takes a lot of code size.

Something not right here,

If P_13_4 is have value of "1", all the if statement in case 1: will be false, it will do nothing in it.

please the SwitchCase Doc

https://www.arduino.cc/en/Tutorial/SwitchCase

First you have to use different variables in the switch() and if(), else the entire switch will never do anything. Second you can pass the x or y values to point_xplus() etc. as a pointer, instead of using a global variable. E.g. void point_xplus(int *waypoint_point_x) {...}

But before all you should explain what you want to do. Your approach looks not very promising :-(

Have you considered turning the logic on its head and doing the if ( P_13_4 == 11 )  { first.

That way you would only have 4 of those rather than 10 sets of 4

...R

Robin2: Have you considered turning the logic on its head and doing the if ( P_13_4 == 11 )  { first.

That way you would only have 4 of those rather than 10 sets of 4

...R

Now I am drawing a sketch how I am thinking.

That means you are not thinking correctly and logically. You have been given all the answers to solve your problem, if you do not understand something then ask about it.

Here’s attachment

Sorry but I can't make sense of your diagram.

Maybe you can describe what you want in English, step by step like this

Button1 pressed start the oven

Light LED when temperature is correct

Button2 pressed record time for start of cooking

Button3 pressed turn off oven regardless of temperature

...R

Hi, What is the application, what is the overall concept of this project?

Tom.... :)

Qwseyvnd: Now I am drawing a sketch how I am thinking.

Qwseyvnd: Here's attachment

Well there is not much coherent thinking going on in that diagram.

The closest it looks like is a switch matrix.

Is this what you are looking for? http://pcbheaven.com/wikipages/How_Key_Matrices_Works/

Isn't it possible to making that the previous button turns off automatic if I turn on the next button something similar to the smoke starter in a car if it runs too hot? It is to save pins.

TomGeorge: Hi, What is the application, what is the overall concept of this project?

Tom.... :)

Home cockpit simulator.

Qwseyvnd: Isn't it possible to making that the previous button turns off automatic

Most button switches turn off as soon as you lift your finger off them. How you use the Arduino to interpret what the buttons mean is up to you.

You can get button switches that latch in the on position and need a second push to switch them to the off position - don't use them.

...R

Robin2: You can get button switches that latch in the on position and need a second push to switch them to the off position - don't use them.

...R

You said that. Why don't use them ? I said that the previous button turns off automatic if I press another button, not this you described.

Why don’t use them ?

  1. There is no need anything you can do with a push on / push off button you can do in software.
  2. Having a switch permanently pressed makes a mess of the scanning matrix and you have to use diodes to prevent ghost key presses.

Qwseyvnd: You said that. Why don't use them ? I said that the previous button turns off automatic if I press another button, not this you described.

This is getting ridiculous ...

The only reason I mentioned the latching switches was so you knew the difference between them and non-latching switches. I was not / am not sure which sort you have.

You did not previously say "I said that the previous button turns off automatic if I press another button"

In Reply #14 you asked "Isn't it possible to making that the previous button turns off automatic if I turn on the next button"

I can only go on what you write - not what you would might have intended to write.

...R