Pages: [1]   Go Down
Author Topic: int. varible or input?  (Read 578 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 3
Posts: 111
Arduino rocks?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 01, 2011, 09:11:03 am by neepie » Logged

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

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.
« Last Edit: June 01, 2011, 09:41:53 am by AWOL » 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.

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8720
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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;".
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 601
Posts: 48566
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

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

0
Offline Offline
Full Member
***
Karma: 3
Posts: 111
Arduino rocks?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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(?)
Logged

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

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 203
Posts: 8720
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: