Go Down

Topic: variable declaration (Read 747 times) previous topic - next topic

hary

Hello.

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

I usually see and use :
Code: [Select]
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 ?
Code: [Select]
byte ledpin=13;

AWOL

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.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Paul Beaudet

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.

Nick Gammon

Better again:

Code: [Select]

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".
http://www.gammon.com.au/electronics

lloyddean

#4
Apr 06, 2013, 11:43 pm Last Edit: Apr 06, 2013, 11:45 pm by lloyddean Reason: 1
As declared in the API headers -

Code: [Select]

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

Code: [Select]

const uint8_t ledpin = 13;


hary

I see, I see.

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

So,
Code: [Select]
int ledpin = 13; is ok.

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

hary

#6
Apr 07, 2013, 12:05 am Last Edit: Apr 07, 2013, 12:49 am by hary Reason: 1

Better again:

Code: [Select]

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".


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.

lloyddean


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.

hary

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 ?

lloyddean

#9
Apr 07, 2013, 01:07 am Last Edit: Apr 07, 2013, 01:21 am by lloyddean Reason: 1
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

Code: [Select]

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.

PeterH

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.
I only provide help via the forum - please do not contact me for private consultancy.

lloyddean

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.

lloyddean

I've always wished for -

Code: [Select]

typedef const uint8_t   pin_t;


hary


I've always wished for -

Code: [Select]

typedef const uint8_t   pin_t;




What's that ?

pYro_65



I've always wished for -

Code: [Select]

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:

Code: [Select]
pin_t pin13 = 13; //This is a const type, it cannot be changed.

Similar to:

Code: [Select]
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:
Code: [Select]
typedef unsigned char uint8_t;

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

Code: [Select]
enum{ pin13 = 13 };
//...
digitalWrite( pin13, HIGH );



Go Up