Reducing code space

Hi,

I’m poor in english and poor in Arduino !

But, i programmed my first one to control a model sub,

my arduino is about to be full of code space, so i try to reduce code,

this is part of my program, please, do you have idea to reduce it ?

My question is specially for this part of code

Thanks !

for(int x=1; x<=3; x++)
 {
   value[x]=pulseIn(5, LOW);
   if (x==1) 
   {
    voie1=value[x];
    lcd.print("voie1:");
    lcd.print(voie1);
   }
   else if (x==2)
   { 
    voie2=value[x];
    lcd.print("voie2:");
    lcd.print(voie2);

   }
   else if (x==3)
   {  
    voie3=value[x];
    lcd.print("voie3:");
    lcd.print(voie3);

   }

You need to post your code again, use the # icon on the editor's toolbar to give you a proper code box, then we will be able to read it properly.

See if you can post your entire code since the problem was the code is too large for arduino. We need to see where things can be done differently to save space.
Use the code button to post code, the "#" symbol in red square:

Meantime if you want some reading, I have some general remarks on how to optimize code to reduce bloat:

obel38:
this is part of my program, please, do you have idea to reduce it ?

You will save space if you change voie to an array - you won’t have to repeat this code so many times, and it becomes short like this:

int voie[3]; 

 for(int x=1; x<=3; x++)
 {
   value[ x ]=pulseIn(5, LOW);
   voie[ x-1 ] = value[ x ]; 
   lcd.print("voie");
   lcd.print(x);
   lcd.print(":");
   lcd.print(voie[ x-1 ]);
 }

I’m assuming voie is an int (no code to check), but making it an array of three items makes your code shorter. The only problem: x goes from 1-3, while voie goes from 0-2, so you have two choices:

  • use “int voie[4]”, which runs from 0-3, and use the same indexes, ignoring voie[0]

  • always subtract one when going from x to voie, and add one when going from voie to x - this is what I have done.

Why mention the first way (voie[4])? Because you mentioned you were low on space - doing it this way takes up two bytes for a unnecessary int, but removes all the code where you have to +1 or -1 - on large code, this can add up.

Without seeing the rest of your code, I can’t say where else you can save space - but consider changing other values like this to arrays and use loops instead.

You will need to show all your code. The lines you posted won't take up much of any type of memory in arduino.

obel38:
my arduino is about to be full of code space

Can you explain what makes you believe you are running out of space?

I think OP meant the compiled side is near maximal size available on FLASH. Wouldn't hurt to clarify this one, OP.

I really like the way an OP starts a thread from an undisclosed timezone and then totally disappears, while the discussion carries on …

It's top secret code. He can't share the whole thing. Sorta like secret government projects, subcontract various parts to different contractors, so no one knows what the final project will be. This isn't spam, this is just me smiling at someone who is scared to show his code for fear of ridicule. And besides...is he ever coming back?

Might have just gone to bed, or to work, or... who knows.

Or solved the problem on his own.