Monitoring Inputs

Hi Everyone.

I'm new to arduino and have a project that I am attempting but have hit a wall hope someone can help.

I have a Push Button, a Sensor Switch and a Relay.

I am trying to write a program in which when the user Pushes the Button it checks to see if the Sensor Switch is made, if it is the it switches the relay to activate a Solenoid.

This is the code I have for it so far

void loop() {
  // MAIN CODE
  // BLUE BUTTON PRESSED
  int BLUESTATE = digitalRead(BLUEPIN);
  int BDSTATE = digitalRead(BDSWITCH);
  int UNCSTATE = digitalRead(UNCSWITCH);
  digitalWrite(UNCSTATE, LOW);
if ((BLUESTATE == 1) && (BDSTATE == HIGH)) 
  digitalWrite(UNCSOL, HIGH);

The problem I am struggling with is, I would like the program to monitor pin 7 which is connected to another Sensor Switch when that switch is made the relay is deactivated to turn the Solenoid off.

Can someone help me with the code for this or point me in the right direction.

Many Thanks in advance

What's the deal with this "The problem I am struggling with is, I would like the program to monitor pin 7 which is connected to another Sensor Switch when that switch is made the relay is deactivated to turn the Solenoid off." You lost me there...

Can you try describing what you want to do again ? forget the code for The time being - use plain simple English and describe exactly all the gears you have and what is the behavior you want to see

Thank you for your reply.

Ok. I have a push button, 2 reed switches (Open and closed) and a relay connected to a solenoid.

When the user presses the button the program checks the first reed switch (open) if this switch is made then the relay triggers and turns the solenoid on. I have managed this part.
Once the device is closed ( this may take between 30 seconds an 3 minutes ) the second reed switch (closed) is made and the relay turns the solenoid off. The signal from the reed switch closed comes in on pin 7 on my arduino uno.. I can't seem to get this bit to work..

I hope this makes more sense.

Thank you

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

Wow. Wasnt expecting that.
Thank you so much for your comprehesive reply.
Will work through what you suggest and let you know the out come.

Thanks again. :slight_smile:

happy new year :slight_smile: