Go Down

Topic: Assigning pin values (Read 1 time) previous topic - next topic


While looking through sample code examples for assign pin values to a variable I see that people are using integers like:
Code: [Select]
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?



I use byte. Others use #define, #const, to keep the software from changing the pin assisgnment accidentally I suppose.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.


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


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!


Mar 30, 2012, 07:54 pm Last Edit: Apr 11, 2012, 12:54 am by dgerman Reason: 1

            • 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

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
please be revised to replace unsigned int ledPin = 13;  with something better.

Please. :smiley-roll-sweat:[/list][/list][/list][/list][/list][/list]


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.



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

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131