Use byte instead of int to store pin references ? Memory consumption.

Hello guys (and lads) !

I bought my Uno some days ago and have been playing with it since then (Great product by the way!).
I followed a couple of guides (hardware) etc, everything is great!

I was wondering why all the examples and guides (that I've read) were not using the byte type to store pin references instead of int ?
Since memory efficiency is something important when programming in a limited ram environment (as the Uno is), and that byte goes 0 to 255, shouldn't it be preferred over int ? And shouldn't that be taught to beginners(myself included) so that they start with good practices/efficiency in mind?

Edit:

void setup()
{
  Serial.begin(9600);
  Serial.println(sizeof(byte));
  Serial.println(sizeof(int));
}

void loop(){}

Int uses 2 bytes,(16bits) while a byte uses.... well, 1 byte(8bits). :blush:
Of course, this is not a big improvement as It will only (at most, on an official Uno with 16 pins) saves you 128 bits. (( 16 * 8 * 2 ) - ( 16 * 8 )),
but still, there must be some micro-controllers with less ram and more pins out there.

Anyway, thanks!

shouldn't it be preferred over int ?

Yes, but int is easier to type. All the examples are very small, so they tend not to teach good habits.

What's a pin reference? A pin number?

In that case const is preferred as that takes no memory at all.

eg.

const byte LED_PIN = 13;

PaulS:

shouldn't it be preferred over int ?

Yes, but int is easier to type. All the examples are very small, so they tend not to teach good habits.

That make sense, tho I think a 'warning' might be useful, anyway, thank you.

What's a pin reference? A pin number?

Yup, I should have said a reference to a pin. (Prob not the correct term, but in French it makes sense.)

+1 for the const buddy, if the sketch does not need to change it at runtime.

In C reference is a technical term, being basically the address of a variable.

Yes, if you are tight on memory you would use byte or char (or int8_t or uint8_t)
instead of int where you could, and various other means to reduce memory
footprint, but that's not the first aim of a piece of example code.

Using #define's or const int's to name pin numbers shouldn't have storage overheads.

MarkT:
In C reference is a technical term, being basically the address of a variable.

I know that my friend, my bad to have used reference to refer to something (a HW address in that case) ]:slight_smile:

but that's not the first aim of a piece of example code.

Yup, got the point. I was just wondering why I haven't seen a tip on that anywhere.

benjaminf:
Yup, got the point. I was just wondering why I haven't seen a tip on that anywhere.

I've done some tips, one mentions that:

Unfortunately I can't force people to read them. :slight_smile: