Arduino Input pin Mode

I would like to know, if a pin needs to be used as an input, is it necessary to use

digitalWrite (pin,HIGH)??

Is it better to define the pin to be used as input as an integer type or using #define ??

that is,

#define switchpin

or

int switchpin =9

or

const int switchpin =9

The reason why I’m asking is, I need to send a message whenever, pin 9 become high (or low) , but the problem, I’m facing is that the message is sent as soon as the program is uploaded to the board.

Pls help!!!

So far as I can tell these are equivalent and use the same amount of flash memory:

#define switchpin 9

const int switchpin = 9;

I assume you’re doing the below to enable the internal pull-up ?

digitalWrite (switchpin , HIGH);

You should do that in the setup() function prior to running your messaging code. You can also try this command when setting the pinmode:

pinMode(switchpin , INPUT_PULLUP);

http://arduino.cc/en/Reference/PinMode

But be aware that the pin will now be a HIGH unless it’s pulled LOW. So if you want to send a message, you should base the message on the pin being pulled LOW. If you need it to be based on when the pin is pulled HIGH by some external device, the don’t enable the internal pull-up (just declare the pin as an input) and use an external resistor (5k - 20k) connected from the pin to ground (aka an external pull-down resistor).

Mee_n_Mac:
So far as I can tell these are equivalent and use the same amount of flash memory:

#define switchpin 9

const int switchpin = 9;

I assume you’re doing the below to enable the internal pull-up ?

digitalWrite (switchpin , HIGH);

You should do that in the setup() function prior to running your messaging code. You can also try this command when setting the pinmode:

pinMode(switchpin , INPUT_PULLUP);

http://arduino.cc/en/Reference/PinMode

But be aware that the pin will now be a HIGH unless it’s pulled LOW. So if you want to send a message, you should base the message on the pin being pulled LOW. If you need it to be based on when the pin is pulled HIGH by some external device, the don’t enable the internal pull-up (just declare the pin as an input) and use an external resistor (5k - 20k) connected from the pin to ground (aka an external pull-down resistor).

Thank you sir.

I think its not necessary to use pinMode (switchPin, HIGH), as you said, because I’m using it to control an external device (GSM module)

And, I had another doubt.

Is it better to use if (digitalRead(9) == 1) to check the status of the pin, or

define an const int variable like switchPin and initialize it to number 9 (pin number) , and then use the statement if (switchPin==HIGH) ??

Actually, I’m using a pin from another arduino to activate this switchPin by using the statement

digitalWrite(red, HIGH), where red is initialized as an output pin , by using

int red = 8 and

pinMode (red,OUTPUT)

on the other arduino.

Pls reply back!!

You must use: if (digitalRead(switchPin)==HIGH)

The reason to define switchPin as 9 and then use this name in your code is that

it makes your code easier to understand tomorrow when you have forgotten which

pin function has which number. Its also easier when you must move the input

to nother pin, then you only change the define and its all taken care of.

Otherwise you must find all places where pin 9 is used and change it, and according

to Murphy you will miss some, making a big mess. It also gives you a nice list of all pin numbers

and their meaning at the top of your sketch.

Regards

Magnus

bobxcool:
I think its not necessary to use pinMode (switchPin, HIGH), as you said, because I’m using it to control an external device (GSM module)

OK, now you've confused me. Are you *reading* this pin to find out what some external device is sending to the Arduino or are you using it to *send* a control signal to some external device ? That above indicates the latter.

Yet in your prior post you said:

if a pin needs to be used as an input, …

Is it better to define the pin to be used as input as an integer type or using #define ??

that is,…

So is the pin to be used as an input or as an output. It can’t be both at the same time.

Mee_n_Mac:

bobxcool:
I think its not necessary to use pinMode (switchPin, HIGH), as you said, because I’m using it to control an external device (GSM module)

OK, now you've confused me. Are you *reading* this pin to find out what some external device is sending to the Arduino or are you using it to *send* a control signal to some external device ? That above indicates the latter.

Yet in your prior post you said:

if a pin needs to be used as an input, …

Is it better to define the pin to be used as input as an integer type or using #define ??

that is,…

So is the pin to be used as an input or as an output. It can’t be both at the same time.

Sir, I am actually reading this pin for a HIGH signal from another arduino.

As, soon as it receives this HIGH signal, a GSM module is activated to send a message.

bobxcool:
Sir, I am actually reading this pin for a HIGH signal from another arduino.

As, soon as it receives this HIGH signal, a GSM module is activated to send a message.

OK, I've got it. So declare the pin as an input using pinmode(). It's a good idea to use a mnemonic like "switchpin" or "inPin" or whatever instead of using the pin number all over the place for all the reasons Magnus pointed out.