variable declaration

Hello.

I was studying Arduino reference to go deeper through and I have a question about variable declaration.

I usually see and use :
int ledpin=13;

I wonder, as 13 would fit on 8bit or in 1 byte, why we're using int (integer) wich use 2 octets ?

Wouldn't it be more rational tu use byte instead ?

byte ledpin=13;

Since you're almost certainly going to use the variable in a pinMode or a digitalWrite/digitalRead, then "byte" is the correct type to use. Check the function prototypes.

I store them as bytes..

In the tuts the explaining data types is cumbersome right off the bat, so they just use ints.
That way noobs don't get confused by overflow errors when they start repeating the process.

There might be a better reason though.

Better again:

const byte ledpin = 13;

It won't change, right? So now it takes no memory at all, as the compiler will turn references to it to "load literal".

As declared in the API headers -

void pinMode(uint8_t pin, uint8_t mode);
void digitalWrite(uint8_t pin, uint8_t val);
int digitalRead(uint8_t pin);

'uint8_t' is an 8 bit unsigned integer type

And as Nick said it best be declared as

const uint8_t ledpin = 13;

I see, I see.

But when we don't know about it, the different way we can find in coding can be confusing.

So, int ledpin = 13; is ok.

better is :byte ledpin = 13;
even better is const byte ledpin = 13;
and even better would be cont uint8_t ledpin = 13;
The last solution would make the code more portable, isn't it ?

Hi Nick, would you explain :
"as the compiler will turn references to it to "load literal"." If not to difficult and useful for beginner.
Thanks.

hary:
The last solution would make the code more portable, isn't it ?

Depends upon what you mean by more portable.

To me I understand 'uint8_t' more readily than 'byte' as I continually have to lookup whether 'byte' is signed or unsigned.

It's right that 'uint8_t' is easier to read when we know about it.

How do I change "int" to the same way. Is it "sint16-t" or "int16_t"
And "unsiigned long" as "uint32_t", is that right ?

As 'int' is assumed to be signed by default and, at least on most Arduino's 16 bits, it's 'int16_t'.

If you don't like that you might be able to use

typedef int int_t;

Although I'm not sure how the various version of the Arduino IDE will "interfere" with compilations of said user defined type. You can always try it and find out.

What would have been best of all would have been for the Arduino designers to provide a type to represent a pin number, so people could get into the habit of using the correct data type without having to think about what type was most appropriate. Unfortunately Arduino isn't into type safety.

Someone out there is probably prototyping the Arduino controlled Time Machine as we speak, and someone else is likely working on updating the Arduino API to version 2.x.

We'll see who what happens after that.

I've always wished for -

typedef const uint8_t   pin_t;

lloyddean:
I've always wished for -

typedef const uint8_t   pin_t;

What's that ?

hary:

lloyddean:
I've always wished for -

typedef const uint8_t   pin_t;

What's that ?

typedef creates an alias to a decorated type.
This one says, alias pin_t with a type 'constant uint8_t'

So later on you can use the type in your code:

pin_t pin13 = 13; //This is a const type, it cannot be changed.

Similar to:

const unsigned char pin13 = 13;
//or
const byte pin13 = 13;
//or
const uint8_t pin13 = 13;

Actually, uint8_t is a typedef provided by the standard:

typedef unsigned char uint8_t;

You can also use a type which is guaranteed to be constant:

enum{ pin13 = 13 };
//...
digitalWrite( pin13, HIGH );