Pages: [1]   Go Down
Author Topic: Assigning pin values  (Read 829 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

While looking through sample code examples for assign pin values to a variable I see that people are using integers like:
Code:
int ledPin = 13;                // LED connected to digital pin 13

Is there a reason for not defining the variable as a byte since that is adequate to hold values 0-255?

Scott
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 27434
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I use byte. Others use #define, #const, to keep the software from changing the pin assisgnment accidentally I suppose.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

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

int is the natural unit to think in, but I think it gets cast down to byte for calls to digitalWrite anyway
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.

Offline Offline
Newbie
*
Karma: 0
Posts: 18
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all.  I am coming from the Basic Stamp and out of necessity you did not waste any memory or variables.  Really starting to like the Arduino!
Logged

NJ, USA
Offline Offline
Newbie
*
Karma: 0
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

            • LED_BUILTIN is a defined symbol in pins_arduino.h. It should be used. Don't made a variable for it.
            • (as of 1.00 4/10/12 it is incorrectly defined if using ETHERNET board, it should be 9 )
            • There is a big difference between using #define and int
              • #define
                acts as an alias and is a preprocessor directive. The substitution occurs when the source is compiled. It allocates no storage. The hardware instruction set contain a class of short "immediate" instructions which do not require fetching an operand for small values, like 13. Using define permits the compiler to use them
              • int
                • allocates 2 bytes in precious RAM (even byte takes one).
                • In addition the generated code cannot use an immediate instruction.

            • The value for LED_BUILTIN is not actually the number of a hardware pin, rather is is a software definition used by the library functions
            • Unfortunately digitalWrite  as defined in wiring_digital.c as seen in some examples is hugely  more complicated then simply setting a value on a pin. The code generated for the call is longer with the int
              • with define:
                • 8d e0           ldi r24, 0x0D   ; 13
                • 61 e0           ldi r22, 0x01   ; 1
                • 0e 94 41 01     call    0x282   ;  0x282
              • with int
                • 80 91 00 01     lds r24, 0x0100 ;  LED13
                • 61 e0           ldi r22, 0x01   ; 1
                • 0e 94 61 01     call    0x2c2   ;  0x2c2
            The hardware instruction set for the ATmel AVR micro contoller (as well an many other micro computer systems ) includes other immediate instructions. These include instructions for setting, resetting and testing flags. They handle I/O ports extremely efficiently. There also exist immediate instructions to  add, subtract(with and without carry), and, or and compare as well as skip instruction which test I/O ports directly. They require referencing the specific port and pin.
            Refer to ATmel AVR instruction set document 0856I–AVR–07/10 at
            http://www.atmel.com/Images/doc0856.pdf

            For example  CBI Clears a bit in an I/O register which is specified in the instruction takes only 2 bytes (see page 48)
              ++++++++++++++++++++++++++
            Can the reference pages
            http://arduino.cc/en/Reference/Int and  
            http://arduino.cc/en/Reference/UnsignedInt and
            http://arduino.cc/en/Reference/DigitalWrite
            http://arduino.cc/en/Reference/PinMode
            please be revised to replace unsigned int ledPin = 13;  with something better.


            Please. smiley-roll-sweat[/list][/list][/list][/list][/list][/list]
            « Last Edit: April 10, 2012, 05:54:10 pm by dgerman » Logged

            Offline Offline
            Newbie
            *
            Karma: 0
            Posts: 18
            View Profile
             Bigger Bigger  Smaller Smaller  Reset Reset

            I posted the example of using pin 13 because it is a common reference.  In my case I am using 10 LEDs each triggered by a digital pin.

            Scott
            Logged

            NJ, USA
            Offline Offline
            Newbie
            *
            Karma: 0
            Posts: 48
            View Profile
             Bigger Bigger  Smaller Smaller  Reset Reset

            The discussion regarding #define is still appropriate, perhaps even moreso.

            Logged

            Pages: [1]   Go Up
            Jump to: