I cant quite get my code... I have searched the web to find out how to serial.print og lcd.print the status of my 4 relays... they are managed by my sensors... but when I want to check the status of an relay I just get Output pins they are connected to and not 1 or 0... (On or off).
#define RELAY_ON 0
#define RELAY_OFF 1
#define Relay_1 2
#define Relay_2 3
#define Relay_3 4
#define Relay_4 5
//-------( Initialize Pins so relays are inactive at reset)----
digitalWrite(Relay_1, RELAY_OFF);
digitalWrite(Relay_2, RELAY_OFF);
digitalWrite(Relay_3, RELAY_OFF);
digitalWrite(Relay_4, RELAY_OFF);
//---( THEN set pins as outputs )----
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
pinMode(Relay_3, OUTPUT);
pinMode(Relay_4, OUTPUT);
delay(200); //Check that all relays are inactive at Reset
This way the output of digitalRead() is thrown away.
And you just print the value of the variable Relay_1.
int relayState = digitalRead(Relay_1);
if (relayState == LOW)
{
Serial.println("LOW");
} else {
Serial.println("HIGH");
}
Please read the reference and tutorial section e.g. about how to use digitalRead() - digitalRead() - Arduino Reference -
There is a lot to learn there.
If you used variable names that make sense, the code would be easier to write (and follow).
One can not look at a variable named Relay_1 and have any clue what it does. But, names like RelayPin1 and relayState1 are quite clear about what they contain/should be used for.
It then becomes obvious that digitalRead(relayState1) is wrong, since the function takes a pin number, and that digitalRead(relayPin1) is the correct way to call the function.
Of course, looking at the reference page, and seeing that each function does is a good idea. You would see that a function like digitalRead() returns a value that you should not be throwing away.
Of course, reading the state of the relay is silly, anyway. You should keep track of what you set ti to.
Well if one want's to get anal about it the best and most positive method to determine the state of a relay output is to use relays with extra contact pairs and have one set of contacts be read back with a digitalRead(). That is the only way one can be positive the relay indeed engaged or not in case of say external DC relay power failed or the relay coil opened up, or the relay coil driver switching transistor failed, or a broken wire or even a damaged output pin, or etc.
At our refinery all our Safety Shutdown Systems required positive external hardware feedback sensing on all controller controlled outputs and typically we used triple inputs signals where it took >= two out of three signals in agreement/voting as to what input value to except.