Thx
So You need to build a state machine
You have:
- one normally open reed switch, RS1 say connected on pin ReedSwitchNOPin
- one normally closed reed switch, RS2 say connected on pin ReedSwitchNCPin
- one relay, normally open, R say connected on pin relayNOPin
2 states: waitforRS1, waitForRS2
You move from state waitforRS1 to state waitforRS2 when RS1 is triggered and associated action is to turn on relay
You move from state waitforRS2 to state waitforRS1 when RS2 is triggered and associated action is to turn off relay
Your code should declare in the setup() the pins relayNOPin as OUTPUT for controlling the relay and set it to LOW or HIGH Depending on how your relay is turned OFF. ReedSwitchNOPin and ReedSwitchNCPin should be declared as INPUT_PULLUP (to use the built-in current limiting resistors of your arduino) for the reed switches. Last you need to initialize a currentState (a byte type will do) variable to waitforRS1. (States are usually described in an [
enum
](Enumeration declaration - cppreference.com) which lets you declare easy to read keywords)
Your loop() should do
If currentState is waitforRS1 then check status of ReedSwitchNOPin. If that pin is triggered then activate the relay and change currentState To waitforRS2
If currentState is waitforRS2 then check status of ReedSwitchNCPin. If that pin is triggered then deactivate the relay and change currentState To waitforRS1
That's it.
Things to consider:
1/ normally-open reed switch means that when the switch is unaffected by a magnetic field, the switch is open and does not conduct electricity. When a magnet comes close enough for the switch to activate, the contacts close and current can flow. ==> by connecting the switch to a pin as INPUT_PULLUP and ground in the other side, you will get a HIGH when there is no magnetic field. So you want to listen for LOW to detect triggering. On the opposite, the normally closed switch will let the current flow all the time unless magnetic field is present. By connecting it to a INPUT_PULLUP you will read a LOW all the time and a HIGH will indicate that the switch is triggered. ==> logics for detecting triggering are opposite, make sure this is coded the right way
2/ relays have usually a normally open and a normally closed connector, and a common in the center => make sure you wire the right one
3/ as your relay drives a solenoid, ensure it is well protected. This usually involves a bypass diode. Read the Solenoid Tutorial
4/ reed switches are like buttons. They can bounce. In the code above I did not mention any debouncing because you stated that it takes a long time for the second switch to activate so by managing state even if the first switch is bouncing, you won't be testing it again and thus should not be a problem. That's why checking for state you are in before reading the pin status is important to make the right decision. What matters is the state, then you only check for the transition conditions that pertains to that state. If state switching could be super fast, then bouncing should be taken into account for proper decision making.
Don't hesitate to post your code for review