Pages: [1]   Go Down
Author Topic: query digitalstate of an OUTPUT pin  (Read 160 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi, this is my very first topic, so in advance I apologise for any form errors^^

I want to do something like this:

int led = 13;
pinMode(led, OUTPUT);
digitalWrite(led, HIGH);
if(led == HIGH){
...
}

The thing that doesnt work is the "if(led==HIGH)"-bit. Of course it wont work, because led==13. Is there a "if(digitalstate(led)==HIGH)" or something?

cheers
Logged

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

Quote
Is there a "if(digitalstate(led)==HIGH)" or something?
Yes. You can digitalRead() the state of a pin. Why you'd need to do that is a mystery, though, since you wrote to the pin. Remember what you wrote.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS smiley

It works. I thought digitalRead was for pinMode(..., INPUT) only...

tim
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17259
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PaulS smiley

It works. I thought digitalRead was for pinMode(..., INPUT) only...

tim

Yes it works. Also one can perform a digitalWrite(pin#, HIGH) to a digital pin that is in the INPUT mode, that will turn on the internal pull-up resistor for that pin. A digitalWrite(pin#, LOW) to a input pin will turn off it's internal pull-up. This is useful when reading simple button switches as it saves needing to use an external pull-up resistor for each switch input you use in a project.

 The point paulS was making is that there should never be a need to read a digital output to determine it's state as it only gets turned on or off by statements in your sketch code and therefore it's last changed state value is something your program can save in a variable that you can use to determine it's present state using simple if statements.

Lefty
« Last Edit: November 29, 2013, 09:37:42 am by retrolefty » Logged

Pages: [1]   Go Up
Jump to: