Pages: [1]   Go Down
Author Topic: variable declaration  (Read 590 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello.

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

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 289
Posts: 25697
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Manchester NH
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Like to build typing devices, and heard unicorns
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Better again:

Code:
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".
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As declared in the API headers -

Code:
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:
const uint8_t ledpin = 13;
« Last Edit: April 06, 2013, 04:45:44 pm by lloyddean » Logged

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int ledpin = 13;
is ok.

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

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Better again:

Code:
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.
« Last Edit: April 06, 2013, 05:49:19 pm by hary » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
« Last Edit: April 06, 2013, 06:21:28 pm by lloyddean » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

I only provide help via the forum - please do not contact me for private consultancy.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've always wished for -

Code:
typedef const uint8_t   pin_t;
Logged

France
Offline Offline
Jr. Member
**
Karma: 1
Posts: 85
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've always wished for -

Code:
typedef const uint8_t   pin_t;


What's that ?
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've always wished for -

Code:
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:
pin_t pin13 = 13; //This is a const type, it cannot be changed.

Similar to:

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

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

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

Logged


Pages: [1]   Go Up
Jump to: