Assigning pins to variables

Does declaring a variable as a pin actually use memory? or is it just used internally by the compiler.

What I mean is, will this use 3 bytes of RAM?

byte led1 = 6;
byte led2 = 8;
byte led3 = 3;

I'm just wondering, because when using PICAXE BASIC, the command "symbol" was just used internally to name variables, pins and constants. For example:

#picaxe 20X2

symbol lcdpin = D.4   'Pin
symbol lcdbaud = N2400_64   'Constant
symbol temperature = b1   'Byte Variable

Yes that will use 3 bytes. The reason is you are not declaring a "pin", what you are doing is declaring a byte of memory which a number which the arduino core then converts into an actual pin register and bit while the program is running.

On the other hand:

const byte led1 = 6;

will use 0 bytes of memory because the compiler will optimise it to a literal value which is stored as part of assember instructions in the program space.

You can save space by using pin and port registers directly for example:

bitSet(PORTB,1);

Would use no ram, less program space, and is much faster.

You can store these as friendly names such as:

volatile uint8_t* portRegister = &PORTB;
const byte someBit = 1;

...
bitSet(*portRegister,someBit);

will use 0 bytes of memory because the compiler will optimise it to a literal value which is stored as part of assember instructions in the program space.

I don’t think that is right.

To use no memory at all use the #define, that performs a text substitution in the pre compiler
so
#define pin3 3
or better yet a meaningful name like:-
#define redLED 3
note there is no ; at the end of these lines.

I was under the impression that the purpose of declaring something as const was to allow the compiler to know that it will never change and thus treat it as a literal value?

So for example this code:

void setup()
{
  // This code will only run once, after each powerup or reset of board
  
}
const byte led1 = 6;
void loop()
{
  // This code loops consecutively 
  digitalWrite(led1,HIGH); 
}

Compiles to this:

...
  76:	86 e0       	ldi	r24, 0x06	; 6
  78:	61 e0       	ldi	r22, 0x01	; 1
  7a:	01 d0       	rcall	.+2      	; 0x7e <digitalWrite>
...

Which in this case is just loading a literal value.
The LDI instruction takes this form: (where KKKKKKKK is an 8 bit value, and dddd is the upper register to use)
dddd KKKK 1110 KKKK, thus the 6 is stored in the assembler instruction not the ram.

Whereas this:

void setup()
{
  // This code will only run once, after each powerup or reset of board
  
}
byte led1 = 6;
void loop()
{
  // This code loops consecutively 
  digitalWrite(led1,HIGH); 
}

Compiles to this:

...
  76:	80 91 60 00 	lds	r24, 0x0060
  7a:	61 e0       	ldi	r22, 0x01	; 1
  7c:	01 d0       	rcall	.+2      	; 0x80 <digitalWrite>
...

Thus loading from the SRAM and using 1 byte of memory.

Declaring a const and a #define results (usually) in the same code.

The advantage of using a const over a #define is that it both avoids name symbol confusion, and imposes strict types on the values.

#define foo 4
cont byte bar = 4;
[code]

both result in the same code.  But:

[code]
foo = 9;
[code]

will throw a cryptic error message, because you have ended up with:

[code]
4 = 9;

which makes no sense.

However,

bar = 9;

will fail with a more descriptive error saying you cannot assign to a constant value.

Also,

char f = foo;

will compile perfectly fine, but may not give the desired results if foo is >127. But:

char f = bar;

will warn (or should do anyway) that the two parts differ in signedness and you should investigate.

Yes, #defines have their place, but their place isn't really for representing numerical constants.[/code][/code][/code][/code]