Pages: 1 [2] 3 4   Go Down
Author Topic: using integer type to define pins, why?  (Read 2715 times)
0 Members and 1 Guest are viewing this topic.
Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Personally I use byte, because pin numbers are indeed unsigned, and don't go over 255.

But does for example analogRead(pin#) function cast it as a int size value inside the function? And as you said a pin number declared as a const anyway doesn't occupy any memory space on it's own, only by the thing that reference it?

Lefty
« Last Edit: February 03, 2013, 12:54:07 am by retrolefty » Logged

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

Actually pin numbers, as used with 'digitalWrite(12, HIGH)', are in fact specified as being of type 'uint8_t' so the compiler will down convert the 'int' to an 'uint8_t' for you.

But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.
« Last Edit: February 03, 2013, 12:57:07 am by lloyddean » Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually pin numbers, as used with 'digitalWrite(12, HIGH)', are in fact specified as being of type 'uint8_t' so the compiler will down convert the 'int' to an 'uint8_t' for you.

But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.

Ah got it, thank you.

Lefty
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.

In other words, "const byte".
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But technically you should be specifying pins as 'const uint8_t pinANALOG = A0' as an example.

In other words, "const byte".

But of course now I've learned that doesn't save any real space over using const int as applied to pin number use.  smiley-grin

Lefty
Logged

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

My problem with 'byte' is it doesn't tell me enough about it.

I believe it is a synonym for 'unsigned char' (because its different on different platforms), but I'm never certain and alway find myself doing the double-check. 

I much prefer 'uint8_t' as it's easier to know what you're getting and is the same everywhere I want to be.
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That's fine, but the core type is "unsigned char" as shown here in stdint.h:

Code:
/** \ingroup avr_stdint
    8-bit unsigned type. */

typedef unsigned char uint8_t;

You just have to remember that "unsigned char" is an 8-bit unsigned type.

Using uint8_t is one typedef away from the core type.

This is obviously a style issue, but for me, I prefer to remember that. It's like saying:

"I want to call a dog a MammalWith2LegsThatBarks because that tells me more about it."

OK, but it's really a dog.

I don't want to start a "style war" here, hey next we'll be arguing about GOTO. smiley-wink
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I much prefer 'uint8_t' as it's easier to know what you're getting and is the same everywhere I want to be.

If they have the typedef I quoted. If not, it's an unsigned char.

Hey, wait, I just shot myself in the foot here. I should be using "unsigned char" and not "byte". Oh well.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That's fine, but the core type is "unsigned char" as shown here in stdint.h:

Code:
/** \ingroup avr_stdint
    8-bit unsigned type. */

typedef unsigned char uint8_t;

You just have to remember that "unsigned char" is an 8-bit unsigned type.

Using uint8_t is one typedef away from the core type.

This is obviously a style issue, but for me, I prefer to remember that. It's like saying:

"I want to call a dog a MammalWith2LegsThatBarks because that tells me more about it."

OK, but it's really a dog.

I don't want to start a "style war" here, hey next we'll be arguing about GOTO. smiley-wink

You own a two legged dog that barks? Post a video or it isn't true.   smiley-grin

Lefty
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I was very emotional when I wrote that. smiley
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

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

Nick - I'm not sure what your getting at or if you're upset or not.

Just saying for me it's ambiguous as to if it's signed or unsigned (unless I look it up) where as 'uint8_t' it's clear that it's unsigned and is unsigned on all platforms where as 'byte' if expressed at all can be either.
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm only upset that I thought a dog had two legs.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17303
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm only upset that I thought a dog had two legs.

Well they do have two legs, plus two more.  smiley-wink

Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 361
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think many people who know better, sometimes just use int instead, when trying to help someone to whom uint8_t or--even byte--would cause additional confusion.  One can't learn everything at once...

Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 506
Posts: 19141
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well they do have two legs, plus two more.  smiley-wink

Two legs, squared?
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: 1 [2] 3 4   Go Up
Jump to: