onisuk
March 22, 2015, 3:53pm
#1
hi! i've got a simple project that turns digital pins on and off to control a relay board.
am i correct in thinking that i cannon use digitalRead to find out the current state of the pins? as they are set to outputs to drive the relays....
id like to know the pin state so i can output an "ON" or "OFF" to my i2c display
cheers onis
MarkT
March 22, 2015, 3:54pm
#2
digitalRead () works anytime on any pin.
onisuk
March 22, 2015, 4:02pm
#3
ah I see, does it return a value like 1 for on and 0 for off ?
MarkT:
digitalRead () works anytime on any pin.
But if you’re the one doing the digitalWrite()s so you already know the state; you’re in charge of it.
onisuk
March 22, 2015, 4:09pm
#5
how would i code it then ? do i need to change a stored variable each time i use digitalWrite() ??
then later in the sketch when im writing to the the lcd i can read that variable ?
do i need to change a stored variable each time i use digitalWrite()
A better way is to always use the state variable in the digital write
boolean state = true;
digitalWrite(pin, state);
onisuk
March 22, 2015, 7:56pm
#8
ah so what is that doing ? when the digital pin is written high the boolean called "state" is then updated to true ?
and the opposite happens when its low ?
onisuk
March 22, 2015, 8:26pm
#9
basically ive got an RTC clock that reads the time and turns on a relay to put on a light …
//Light control
if (hour() > 7 && hour() < 20)
{
digitalWrite(lightPin, HIGH);
}
else
{
digitalWrite(lightPin, LOW);
}
this is my code so how do i read the state of “lightPin” so i can output to my display ?
all this help is very much appreciated
when the digital pin is written high the boolean called “state” is then updated to true ?
No nothing is updated. By simply writing the variable and then manipulating your variable you keep the two in sync.
so how do i read the state of “lightPin” so i can output to my display ?
You do not read it. You use the state variable to keep track of it.
if (hour() > 7 && hour() < 20)
{
state = HIGH;
digitalWrite(lightPin, state);
}
{
state = LOW;
digitalWrite(lightPin, state);
}
The variable state is the state of the pin who’s value is given by the variable lightPin.