Go Down

Topic: int. varible or input? (Read 685 times) previous topic - next topic

neepie

Jun 01, 2011, 04:09 pm Last Edit: Jun 01, 2011, 04:11 pm by neepie Reason: 1
When defining varibles, you type int variable = 0; right? so its defined now and by default its 0.

What about when defining digital inputs? int inputpin = 0; right?

Analog pins have the letter A but what about digital? How does arduino know is it variable or input?



edit: nicely typoed subject.

AWOL

#1
Jun 01, 2011, 04:16 pm Last Edit: Jun 01, 2011, 04:41 pm by AWOL Reason: 1
Quote
What about when defining digital inputs? int inputpin = 0; right?

Wrong. All you have done there is defined an "int" variable called "inputpin".

If you were to use "inputpin" as an argument to a call to "digitalRead" (e.g. "val = digitalRead (inputpin) ;" ), then you might be said to be defining it as an input, but only if you had made sure with a call to pinMode that "inputpin" was an input.
"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.

johnwasser

When are creating named constants to pass to pinMode() and digitalRead() you should probably use 'const int':

Code: [Select]

const int inputpin = 3;  //  Input from our sensor


The 'const' keyword tells the compiler that the value of that variable will not be changed.  It allows the compiler to optimize the code better AND it allows the compiler to let you know if you do something stupid like "inputpin = HIGH;".
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

PaulS

Quote
Analog pins have the letter A but what about digital?

Wrong. Analog pins being used as digital pins have an alias defined, where the alias starts with the letter A. Analog pins as analog pins do not have the letter A; they are just numbers, like the digital pins.

AWOL

(But you can still use the A prefix if you want to use it as an analogue pin)
"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.

neepie

Umm..
Quote
Analog pins being used as digital pins have an alias defined


What do you mean by using as digital pins? Digital and analog pins are physically different pins(?)

AWOL

Yes, they are physically different pins, but analogue pins can be used as digital I/O pins.
Analogue pin 0 is digital pin 14.
"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.

johnwasser


Yes, they are physically different pins, but analogue pins can be used as digital I/O pins.
Analogue pin 0 is digital pin 14.


Which is why "A0" is defined as "14" and why analogRead() will subtract 14 from the pin number if you give it a number >=14.

(For Arduino Mega the 'analog input' pins start at 54 instead of 14.)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Go Up