Sketch size and different Datatypes makes no difference? (for ATTiny)

I'm playing around with different Attinys and I try to adapt my sketch and observe if I can shrink down the size of my source code.

int led1 = 4;

void setup() {

  pinMode(led1, OUTPUT);

}

void loop() {
  analogWrite(led1, 15);
}

This Source code uses 1392 bytes, nevertheless if I set the led1 to int, long, byte or what else?

I thought in coding with MCU it is critical which data types I'm using?

Thanks for your help

Perhaps the compiler can see that the variable led1 is never updated, so it optimises it as a constant requiring only 1 byte. It would do this no matter what type byte/int/long you make it.

Try

volatile int led1 = 4;

and then change the int to byte or long. The volatile keyword tells the compiler not to make assumptions/optimisations about the variable, but the compiler might still not be fooled by that, I don't know.

2 Likes

No way.

Why not? Do I mix here something up?

Without analogWrite and a few more LEDs the Sketch uses 912 bytes?

You're confusing source and object.

The optimizer really does it's best for you, but to see effects you will need a more expanded program than what you are doing now. Even if the compiler doesn't see it as a constant, it will know where it is used, and if it is used as an argument for a function that takes a 16 or 8 bit value, the variable will get truncated to that unless it is modified somewhere.
Reducing variable sizes can make a huge difference, for instance if you use millis() but can make do with a 16-bit value, do so !
Avoid floating point variables since using them requires the use of special functions that will need to be compiled.
If you define something like

#define ECHO 14
#define VALUE 23 * ECHO + 12

you will find that when using VALUE somewhere, you can reduce flash by adding braces

#define (VALUE 23 * ECHO + 12)

Forcing the compiler to do the calculation instead of the MCU from within the sketch.

1 Like

Please post the code you are trying to use (within code tags </> no screenshot !) and the board you are compiling for (and the core you are using)
So i can re-create and show you how it does work.

1 Like

Thank you very much for your explanation. Makes sense!

I have no specific problem to discus. I just want to understand the principial how to shrink the source code size.

Now I have better understanding.
Have a nice day!

Fixed.

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