Hello all! Today I have decided to give the Arduino a go. My previous experience is with the PICAXE & PIC (barely) so this is where my question is being derived from.
My question is, "Can pins only be assigned by their numeric value?" i.e D13 is pin 13 and must be assigned as "13". I would much prefer to refer to pins by their names or by ports rather then having to memorize what A0 is or what D6 is, just calling for D6 should ought to be enough yes? Normally how I call up pins in BASIC is with either a...
symbol LED = B.0
symbol Switch = C.2
or for calling statements or states...
High C.6
Low D.2
and so on and so forth.
Must I always refer to a pin by it's numerical value (in other words the pin #) in sketches?
For the record I have read-up about Ports, which half-way satisfies the question I had. I am wondering if it can go even simpler than that and for, as an example, bit 4 on PORTD is labeled as "D5" and the pin is Pin #5; again, this is all theoretical I do not know if PORTD corresponds to a real port on the ATMega328. So in our sketch we would say pin 5 or xxxx = 5. But if we use the PORT command in Arduino it would be PORTD = xxxxx | B0010000. Can it not just be stated as PORTD = D5,OUTPUT?
One of the first example sketches, blink, shows how you can do that:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Note the "led" is 13, so that is its name. Personally I prefer to make it a constant (and then capitalize it):
Ok I think I'm getting how it all works now. I'll read up on the type of variables to hopefully gather a bit more knowledge. Thanks for your reply.
EDIT:
So to do what I want I can set...
const D4 = 5
to get to...
digital.Write = (D4,HIGH)
I was hoping I could side-step having to store a variable for the pin so that I could directly call the pin. As in, D5 is always D5, where saying D5=5 is redundant. While I am on the thought of it, what are the Analog pins referred to as a port? A tutorial I viewed mentioned PORTD was the Digital pins and PORTB was the Analog pins. Is this true for all devices (I am using the Nano v3.0)?
You can use the ports but I personally wouldn't unless I was desperate for speed. And if you are used to PICAXE basic, you will be accustomed to things running fairly slowly.
Justinmcg67:
I was hoping I could side-step having to store a variable for the pin so that I could directly call the pin. As in, D5 is always D5, where saying D5=5 is redundant. While I am on the thought of it, what are the Analog pins referred to as a port? A tutorial I viewed mentioned PORTD was the Digital pins and PORTB was the Analog pins. Is this true for all devices (I am using the Nano v3.0)?
Note, if you use:
const int D5 = 5;
You are not creating a variable, but instead the C++ compiler will substitute 5 wherever it sees D5. Now, if you said:
int D5 = 5;
Then you create a variable that must be loaded before you use it (since it can be changed).
In terms of the analog pins, depending on which processor you are compiling for, A0..An are defined to be different numbers. For example, on the Uno R3, A0 is 14, while on the Due it is 54.
Justinmcg67:
Ok I think I'm getting how it all works now. I'll read up on the type of variables to hopefully gather a bit more knowledge. Thanks for your reply.
EDIT:
So to do what I want I can set...
const D4 = 5
to get to...
digital.Write = (D4,HIGH)
I was hoping I could side-step having to store a variable for the pin so that I could directly call the pin. As in, D5 is always D5, where saying D5=5 is redundant. While I am on the thought of it, what are the Analog pins referred to as a port? A tutorial I viewed mentioned PORTD was the Digital pins and PORTB was the Analog pins. Is this true for all devices (I am using the Nano v3.0)?
Basically every AVR I/O pin belongs to a certain specific 8 bit port. All I/O pins can be used as digital input or output pin, but more importantly every I/O pin also has specific optional uses such as PWM output capability or as a user interrupt pin or as a analog input pin or timer output functions, etc.
What each I/O pin functions are is best shown in the AVR datasheet for the specific AVR chip being used. The AVR chip pin functions are different from say a avr 328 chip as used in a Uno board from say a avr 2560 chip used in the mega board. The standard arduino I/O functions make these specific pin number assignments transparent for the user by using Arduino 'abstracted' logical pin number names, and must be understood and used if you are going to do any direct low level I/O functions in your sketch.
A tutorial I viewed mentioned PORTD was the Digital pins and PORTB was the Analog pins. Is this true for all devices (I am using the Nano v3.0)?
Not sure I saw an answer to this.
The ports are the same for the same chip no matter the board it is used on.
For for Uno, Duemilanove, Lilypad, Promini, Nano, Fio - all use the Atmega328P
D0-D7 are PORTD bits 0 to 7
D8-13 are PORTB bits 0-5 (bits 6,7 not available to you, these are used as external clock pins)
D14-19 are PORTC bits 0-5 (bit 6 not available to you, used as Reset pin; ; bit 7 is not brought to an IO pin on any '328 device)
D14-D19 may also be used a analog input A0 to A5. (ADC6,7 are available as analog input only on select surface mount boards)