Smoother and faster code for my RGB LED Controller

I have made an RGB controller to control led’s or led strips via a system called “LK-IHC”

when it reads a number on RX, it makes a specific color or fade between colors.

My question is, if some of you could look through my code and give some advise how to make it faster and better :slight_smile:
(Arduino Uno)

The code file is attached :slight_smile:

I’ve have worked a long time on this code, especially on the dimming of the led throughout all the different programs. Hope you like it :slight_smile:

RGB_LED_via_IHC.ino (19.3 KB)

Better?

Use the auto-format tool.

Get rid of the "goto"s

remove anything that looks like this

 if(Data == 601){
      }

Use arrays or arithmetic.

 if(Data == 700){ // LED 0% 
        Light_Procent = 0.00;
        Read_RGB_And_Write();
      }

      if(Data == 701){ // LED 10%
        Light_Procent = 0.10;
        Read_RGB_And_Write();
      }

      if(Data == 702){ // LED 20%
        Light_Procent = 0.20;
        Read_RGB_And_Write();
      }

Cut it down to fewer than 300 lines. 200 would be better

AWOL:

 if(Data == 700){ // LED 0% 

Light_Procent = 0.00;
       Read_RGB_And_Write();
     }

if(Data == 701){ // LED 10%
       Light_Procent = 0.10;
       Read_RGB_And_Write();
     }

if(Data == 702){ // LED 20%
       Light_Procent = 0.20;
       Read_RGB_And_Write();
     }

How about:

       Light_Procent = (Data - 700)/10;
       Read_RGB_And_Write();

instead of all those if statements?

  1. More functions, less goto…

  2. Remove all the repeated if conditions and uses ranges, lookup tables and function calls as suggested before

  3. Remove floating point math

Rough guess is that you could do the same tasks in < 50 lines of code (+ a lookup table),

Thank you all for the response.

I have made this changes to the “light_procent”

"if(Data >= 700 && Data <=710){

Light_Procent = ((Data - 700)/10); 
       Read_RGB_And_Write();
      Data = Old_Data;
    }

I have tried without luck to get rid of the "goto"s, if I need the code to "go back or forth to a curtain place, ?

The same problem with the if conditions ? if I need the following code only to be used if the incoming data i right, then what ?