Why use int instead of byte for pin numbers?

callmebob:
Thanks krupski, for this informative answer.

WOW

5 years later

Oops, reviving a dead thread... sorry was so thrilled with the answer, I didn't read any dates.

After UKHeliBob's informative reply I noticed the dates. Thanks to the wonderful feature of this board, I had to wait 5 minutes before editing or deleting it.

Just a thought... it might be 5 years to you all, but it was only 15 minutes old to me :slight_smile:

Thanks to the wonderful feature of this board, I had to wait 5 minutes before editing or deleting it.

You are halfway to having the 5 minutes between posts restriction lifted (100 posts needed I believe)

Arduino is not alone on this. There is an ongoing trend among computer programmers, and that is that you don't think that much about anything. One of those things you don't think about is data types. AT ALL.

Remember that "incident" (oh the horror!) on Youtube where the play count for Gangman Style rolled over?

They were using signed integers to store that. It would have taken a semi competent programmer of any seniority, let alone architect, to realize that it takes the same space and processing power to just use an unsigned integer, but that's not "standard", that's not "modern", modern is to use whatever holds numbers.

The base integer type of the language/platform gets used for anything numeric that doesn't need decimal places. Period.

One of those things you don't think about is data types. AT ALL.

There are programming languages that don't actually have data types. Whilst this provides great freedom it does teach you to be very careful. Debugging can be fun.

They were using signed integers to store that. It would have taken a semi competent programmer of any seniority, let alone architect, to realize that it takes the same space and processing power to just use an unsigned integer,

Java mentality.

When's the last time you ever saw a perfect product that pleased everybody?

majenko:
yea, I am getting close to the edge of ram in one of my project, changed all my const X's to #defines and freed up almost 2 dozen bytes of ram

Surprising that, since const variables are stored in progmem, not in RAM...

Once it has gone through the compiler there is no difference between

const byte X = 4;

and

#define X 4

Nope, they are different, as has been proven by experiment with the actual compiler and
the actual compiler optimization settings used in the environment. There is definitely no
requirement to store const variables in read only memory, its just a possible optimization.

krupski:
Pin numbers: How stupid is that? What's wrong with "PORTB, BIT 7"?

Well, to somebody new to Arduino, here's what's wrong: I can understand pin numbers. I don't understand "PORTB, BIT 7"!

MarkT:
Nope, they are different, as has been proven by experiment with the actual compiler and
the actual compiler optimization settings used in the environment. There is definitely no
requirement to store const variables in read only memory, its just a possible optimization.

I have found this to be true. I would have thought that the compiler would treat a constant variable like a constant instead of a variable. I like having the type, and have historically used const byte ledPin = 3; sort of statements for easy reading. However, I found (incidentally, when I was debugging something unrelated and noticed it in the generated assembly code) that in some instances it generated larger code than #define statements. So I switched. I prefer to have more efficient execution code than prettier source code. I realize this is a value judgement, and there are many people who feel that maintainability and speed of coding are more important than execution speed. I prefer to have a better end product.

I believe that this is guaranteed to be a compile time constant.static constexpr byte x = 4;

Jimmus:
However, I found (incidentally, when I was debugging something unrelated and noticed it in the generated assembly code) that in some instances it generated larger code than #define statements.

Example please.