Go Down

Topic: Analog input as digital input for button (Read 20258 times) previous topic - next topic


This is more of a confirmation that i read it right, but i can use my analog inputs as digital inputs right?  i would just use pin 14-19?

Code: [Select]

digitalRead(14); //?


Yes, you can use the analog pins as digital pins, by numbering them 14 - 19.

That's the proper way to read the pin. Well, except that the function returns a value that must be important, or you wouldn't have called the function, so toy probably want to store the returned value somewhere.


does this also works and for the arduino mega

is this corect?

Code: [Select]
//********** i/o scan modes on analog inputs **********
int scan1 = (8+54) ;
int scan2 = (9+54);
int scan3 = (10+54);
int scan4 = (11+54);
int scan5 = (12+54);
int scan6 = (13+54);
int scan7 = (14+54);

//******************** inputs as i/o for the scan mode ********************
int simplescan = (0+54) ; // i/o switch for simple scann
int sensorenable = (1+54) ; // i/o switch for voice scan
int lightenable= (2+54) ; // i/o switch for light scan
int motionenable = (3+54) ; // i/o switch for motion enable scan


You aren't reading the pin.

You would want:

Code: [Select]

int scan1 = digitalRead(8+54) ;

this will give you a 1/0 in scan1 and let you know if anything is connected to port (8+54=)66.

I've never used a mega so you may want to check that whole 8+54 business.

Jeff McD

Quick question:
Is the below correct if i want to read a push button on analog pin (0)?
its a rough version i know, but i just want to check the important parts...

pinmode(14,input);                       // set the pin to input
int BUTTON = digitalread(14);        // read analog pin 0 as digital and set it to a variable

if (BUTTON == HIGH)                   // it shows pressed
else if (BUTTON == LOW)             // it shows not pressed

hardware layout:
5v___button____10k ohm resistor___ground
                   |___analog pin 0

layout done as shown in:

Thanks :)



Slower reply, no.
If BUTTON is not HIGH, there is no need to check to see if it is LOW.

Jeff McD

yea i will prob use an else, but wanted to explain what both scenarios  represented.

btw wouldnt it still be yes, just with unnecessary code  :P

Go Up