How to reduce program space

The program below produces a value that I can use on my servo'.The last digit must be either a 0 or 5.
Compiling wiht method2 only produces less programm-space * then method1 compiled.

#include <Streaming.h>  /* http://arduiniana.org/libraries/streaming/ */
#define myround(X)   round(X * 2.0) / 2.0

int   servo1Value = 1230;
int   servo2Value = 1230;
float myRound = 0.0;
/*********************************************************************************************
  The total amount to be paid ending in .01 € or .02 € is rounded to the lower .00 €.
  The total amount to be paid ending in .03 € or .04 € is rounded up to the higher .05 €.
  The total amount to be paid ending in .06 € or .07 € is rounded down to the lower .05 €.
  The total amount to be paid that ends in. 08 € or. 09 € is rounded up to the higher, 00 €.
 *********************************************************************************************/
void setup() {
  int rangestart = servo1Value;
  int rangeend = rangestart + 10;
  Serial.begin(9600);
  Serial << "Servo" << "\t" << "method1" << "\t" << "method2" << endl;
  for (rangestart; rangestart <= rangeend; rangestart++) {
    // Method one gives correct answer */
    myRound = float(rangestart);
    servo1Value = myround((myRound) / 10) * 10;
    // Method2 also produces the correct answer
    servo2Value = myround(float(rangestart) / 10) * 10;
    Serial << rangestart << "\t" << servo1Value << "\t" << servo2Value << endl;
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}```

Can either method1 or 2 be written more effecient so it uses even less program-space??

The lines with the three backslash-single-quotes should on a separate line. You can still fix that.

First of all, it is not something to worry about. You already use the Serial library and the "Streaming.h" code and probably also the Servo library. A few bytes more or less with the same code written in a different way will not make the difference.

I can make a guess what is going on. The compiler optimizes in a way that is almost uncomprehensive. Changing the source code in a very small way can result in the compiler choosing a different optimization method.
When you change something in other parts of the code, then the compiler can decide to optimize this part in a different way. So it also depends on every single thing in the rest of the code.

Do you know that your macro is dangerous ? Since X can be anything.
This is better:

#define myround(X)   round((X) * 2.0) / 2.0

The Arduino round() is a teeth grinding, hair splitting, toe-curling, bug:

Can you make a function for the rounding ?

If you are trying to save program space it makes no sense to round to the nearest 5 by dividing by 10, rounding to the nearest 0.5, and then multiplying by 10. Just round to the nearest 5. You can also pass the integer directly to the macro since it gets converted to a float when divided by 5.0:

#define myround(X)   (round(X / 5.0) * 5.0)
// int myround(float X) { return round(X / 5.0) * 5.0;}

    // Method one gives correct answer
    servo1Value = myround(rangestart);

It doesn't seem to matter for program size if you use a macro or a function:
As a macro: 3332 bytes of program memory.
As a function: 3332 bytes of program memory.

If you don't have to deal with rounding negative numbers toward zero you can do everything in integers and save over 1100 bytes of program memory:

int myround(int X) { return ((X+2) / 5) * 5;}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.