Go Down

Topic: query digitalstate of an OUTPUT pin (Read 233 times) previous topic - next topic

timbuktu

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

PaulS

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.

timbuktu

Thanks PaulS :)

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

tim

retrolefty

#3
Nov 29, 2013, 03:34 pm Last Edit: Nov 29, 2013, 03:37 pm by retrolefty Reason: 1

Thanks PaulS :)

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

Go Up