Norway
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« on: February 03, 2013, 07:04:29 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9442
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #1 on: February 03, 2013, 07:16:53 am » |
you can do a digitalRead() to check the status of a pin - so indirect the status of your relay.
|
|
|
|
|
Logged
|
|
|
|
|
Norway
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #2 on: February 03, 2013, 07:24:31 am » |
I have tried digitalRead().. but it still gives my the connected pin number... I need help to code that little part...
digitalRead (Relay_1); Serial.println(Relay_1); delay(200);
|
|
|
|
|
Logged
|
|
|
|
|
Pakistan
Offline
Sr. Member
Karma: 5
Posts: 318
Arduino rocks
|
 |
« Reply #3 on: February 03, 2013, 07:28:27 am » |
do like this... Serial.println(digitalRead (Relay_1));
|
|
|
|
|
Logged
|
|
|
|
|
Norway
Offline
Newbie
Karma: 0
Posts: 16
|
 |
« Reply #4 on: February 03, 2013, 07:31:18 am » |
Thank you so much!! It worked!!! 
|
|
|
|
|
Logged
|
|
|
|
|
Pakistan
Offline
Sr. Member
Karma: 5
Posts: 318
Arduino rocks
|
 |
« Reply #5 on: February 03, 2013, 07:39:49 am » |
Thanks for the Karma 
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9442
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #6 on: February 03, 2013, 07:45:05 am » |
digitalRead (Relay_1); Serial.println(Relay_1); delay(200);
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() - http://arduino.cc/en/Reference/digitalRead - There is a lot to learn there.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35588
Seattle, WA USA
|
 |
« Reply #7 on: February 03, 2013, 09:13:46 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9442
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #8 on: February 03, 2013, 03:59:45 pm » |
Of course, reading the state of the relay is silly, anyway. You should keep track of what you set ti to. But PaulS, the pin itself keeps track of the state of the relay? OK reading it back is sloooww compared to a variable.
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15320
Measurement changes behavior
|
 |
« Reply #9 on: February 03, 2013, 05:55:30 pm » |
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.
Lefty
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 91
Posts: 9442
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #10 on: February 04, 2013, 01:22:10 pm » |
Lefty, and if one contact of the relay does work properly and the other not? I'm sure you never can be sure 
|
|
|
|
|
Logged
|
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15320
Measurement changes behavior
|
 |
« Reply #11 on: February 04, 2013, 01:36:13 pm » |
Lefty, and if one contact of the relay does work properly and the other not? I'm sure you never can be sure  Yes, and a team normally goes through a risk assessment and do consider the probability of the single point failure when making such decisions. Lefty
|
|
|
|
|
Logged
|
|
|
|
|
|