Black Lives Matter - Action and Equality. ... Adafruit joins the Stop Hate for Profit campaign.
0

Simple switch question... <code>
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Simple switch question... <code>

by rzyzzy on Sat Jan 29, 2011 2:03 pm

My project has eight safety switches, after all eight switches are tripped, (but only if they all stay tripped) - I'd like to send a continuous signal to a relay. After the relay has a signal, it doesn't matter if the switches are on or off. I'd like to use a 9th switch (momentary?)to turn off the signal to the relay.

I already purchased the ready-made relay "brick" + tested it with the "blink" tutorial - it's working fine....

Can anyone point me towards some code to get started? This seems incredibly simple, but I'm obviously a novice and I'm not finding anything that will work for me

Thanks much!
rzyzzy
 
Posts: 1
Joined: Sat Jan 29, 2011 1:48 pm

Re: Simple switch question... <code>

by mtbf0 on Sat Jan 29, 2011 2:24 pm

ladyada's arduino tutorial 5 covers using and debouncing switches.

connecting all eight of your safety switches in series will turn them into one big switch that's much easier to handle in code.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: Simple switch question... <code>

by franklin97355 on Sat Jan 29, 2011 4:32 pm

As stated there is no coding needed, it is a pure mechanical operation.

franklin97355
 
Posts: 21498
Joined: Mon Apr 21, 2008 2:33 pm
Location: Lacomb, OR.

Re: Simple switch question... <code>

by zener on Sat Jan 29, 2011 5:13 pm

So, we have here the classic question of hardware/software resource allocation. Technically you do not need any processor, you can do it all with switches, wires and a relay. Or you can do it in whole or in part in software, but you still need switches wires and a relay.

Anyway, your question is about software so you might try posting in the Arduino section.

However, even if you do not want to wire your safety switches in series, you can treat all their inputs to your processor as a big OR function. You don't even really need debounce unless you want a safety switch to have some minimum on time for noise immunity. I think the tutorial does handle the code needed for something like this.

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Simple switch question... <code>

by eventhorizon on Wed Feb 02, 2011 1:11 am

you can treat all their inputs to your processor as a big OR function


You meant a big AND function.

Though If ever you are going to use a uC for that, either use a proper Logic AND Gate (74LS08) for that purpose. Or, you can use a uC to make a safety switch in which you need to switch on the proper switches in sequence (with a reset if you got it wrong) otherwise it won't turn on a relay. (sorry for the tongue twister part..xD)
-----------------------------------------------------------------------------------------------
Out of one hole, and into another.
eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm
Location: Quezon City, Philippines

Re: Simple switch question... <code>

by zener on Wed Feb 02, 2011 4:02 pm

Yes, AND! Although if they are active low then you could use OR...

zener
 
Posts: 4567
Joined: Sat Feb 21, 2009 2:38 am

Re: Simple switch question... <code>

by eventhorizon on Thu Feb 03, 2011 6:42 am

Although if they are active low then you could use OR...


Err.. The way I understand it is that OR is and will always be in parallel, or the operator "+" in math. Even a NOR would only need 1 input changed to vary the state of the output. As for switching, AND, NAND are used especially in multiple switches, opposed to using OR for multiple switches (since just 1 switch is needed to control the flow). So AND is used on multiple switches (specifically safety switches) and OR switch if you want multiple switches that triggers 1 function (I.E. Car alarm when a door is open.)
-----------------------------------------------------------------------------------------------
Out of one hole, and into another.
eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm
Location: Quezon City, Philippines

Re: Simple switch question... <code>

by adafruit_support_bill on Thu Feb 03, 2011 7:12 am

The key phrase here is "active low". This inverts the normal logic. In your case, it would mean that all switches must be OFF (open) to enable operation. To detect any single switch or combination of switches that would disable operation you would use an OR.

There are a variety of circumstances in which active-low logic is useful. In the case Atmega processors, it allows you to use the internal pullup resistors which simplifies wiring and reduces external components.

adafruit_support_bill
 
Posts: 77870
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple switch question... <code>

by eventhorizon on Thu Feb 03, 2011 9:10 am

In your case, it would mean that all switches must be OFF (open) to enable operation. To detect any single switch or combination of switches that would disable operation you would use an OR.


Well the thread started off as:
My project has eight safety switches, after all eight switches are tripped, (but only if they all stay tripped) - I'd like to send a continuous signal to a relay.


So activating 1 (Either grounding of pin, or supplying Vcc in the case of active-low mode) would trip the whole thing in accordance to the code:
Code: Select all | TOGGLE FULL SIZE
//using OR
if SW1 OR SW2 OR SW3.. SW8 //OR would trip with 1 of any of the 8 switches, that's not "safe"
{
    activate relay;
}

//using AND
if SW1 AND SW2 AND SW3.. SW8 //all switches must be on, or activated how-ever is what rzyzzy wants.
{
   activate relay;
}


So IMHO, using OR in any form would trip the relay with just 1 switch on.
0|0|0|0|0|0|0|1 = 1, in active-low mode: 1|1|1|1|1|1|1|0 = 1
-----------------------------------------------------------------------------------------------
Out of one hole, and into another.
eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm
Location: Quezon City, Philippines

Re: Simple switch question... <code>

by adafruit_support_bill on Thu Feb 03, 2011 9:24 am

So IMHO, using OR in any form would trip the relay with just 1 switch on.
0|0|0|0|0|0|0|1 = 1, in active-low mode: 1|1|1|1|1|1|1|0 = 1

But in active low, all your logic needs to be inverted.
My project has eight safety switches, after all eight switches are tripped, (but only if they all stay tripped) - I'd like to send a continuous signal to a relay.

In active low, 'tripped' means "0" not one. So:

0|0|0|0|0|0|0|0 = 0 - i.e. all switches are 'tripped' (0) and the result is (0) so the relay is energized (remember, inverted logic!).

If any switch is not tripped (1), the result is (1) and the relay is not energized.

adafruit_support_bill
 
Posts: 77870
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple switch question... <code>

by eventhorizon on Thu Feb 03, 2011 10:44 am

But in active low, all your logic needs to be inverted.


It is inverted. "It" being the inputs. "0|0|0|0|0|0|0|1 = 1" is the normal trip-with-Vcc (Pulled HIGH). In active-low mode: "1|1|1|1|1|1|1|0 = 1" the "0" is when you pull that pin LOW, all else is 1 because of the internal pull-up. Though it's still the same when using active low, say, the switches are pulled down to GND by default (marked as off in a slide switch for example), and you disconnect them from the GND to pull them HIGH (through internal pull ups). Still with that 1 deactivated switch, the relay will turn on(since 1 pin is automatically pulled up when you disconnect that pin from the ground while using active low. And with the code "if (SW1 OR ... SW8)", a single 1 means TRUE for that "if-statement" causing it to execute a Turn_Relay_On).

But I admit I was wrong at the active low part earlier. But my point is that a single 1(HIGH) in the code already signifies an activated switch without pulling up (or disconnecting a pin from GND, when in active low) on any other switch, as compared to the AND operator that all the switch be either ON, or OFF (when in active low).
-----------------------------------------------------------------------------------------------
Out of one hole, and into another.
eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm
Location: Quezon City, Philippines

Re: Simple switch question... <code>

by adafruit_support_bill on Thu Feb 03, 2011 11:14 am

Still with that 1 deactivated switch, the relay will turn on(since 1 pin is automatically pulled up when you disconnect that pin from the ground while using active low. And with the code "if (SW1 OR ... SW8)", a single 1 means TRUE for that "if-statement" causing it to execute a Turn_Relay_On).


The OP has not written any code yet. When we use active-low logic, we write the code accordingly. :D

adafruit_support_bill
 
Posts: 77870
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple switch question... <code>

by eventhorizon on Thu Feb 03, 2011 11:26 am

I suppose, but with the logic of just switching a relay. Either a supply of Vcc to the relay, or pulled down to turn it on; since he's only making a safety switch of some sort that needs a condition "turn all the switch to send a continuous signal to a relay" is already a pseudo code. But it's funny that you want to use a uC for this application, like c'mon it's overkill. Buy switches, wire them up in series and there's a simple safety switch. Or as I mentioned on an earlier post, "you need to switch on the proper switches in sequence" is an added safety feature for it. :)
-----------------------------------------------------------------------------------------------
Out of one hole, and into another.
eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm
Location: Quezon City, Philippines

Re: Simple switch question... <code>

by richms on Fri Feb 04, 2011 11:34 am

Microcontroller isnt overkill if you want it to be able to hold which saftey failing was the reason for the relay being cut. I expect that will be the next thing on the to do list for a project like this

richms
 
Posts: 558
Joined: Tue Jan 20, 2009 3:05 am
Location: New Zealand

Re: Simple switch question... <code>

by eventhorizon on Sat Feb 05, 2011 12:25 pm

Microcontroller isnt overkill if you want it to be able to hold which saftey failing was the reason for the relay being cut.


For the first application, it is. But as you add more and more safety feature a uC is okay (short circuit detection, switch in sequence to trigger, reset, and the list goes on.)
-----------------------------------------------------------------------------------------------
Out of one hole, and into another.
eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm
Location: Quezon City, Philippines

Please be positive and constructive with your questions and comments.