Simple switch question... <code>

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
rzyzzy
 
Posts: 1
Joined: Sat Jan 29, 2011 1:48 pm

Simple switch question... <code>

Post by rzyzzy »

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!

mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am

Re: Simple switch question... <code>

Post by mtbf0 »

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.

User avatar
Franklin97355
 
Posts: 23938
Joined: Mon Apr 21, 2008 2:33 pm

Re: Simple switch question... <code>

Post by Franklin97355 »

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

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

Re: Simple switch question... <code>

Post by zener »

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.

eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm

Re: Simple switch question... <code>

Post by eventhorizon »

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)

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

Re: Simple switch question... <code>

Post by zener »

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

eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm

Re: Simple switch question... <code>

Post by eventhorizon »

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.)

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple switch question... <code>

Post by adafruit_support_bill »

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.

eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm

Re: Simple switch question... <code>

Post by eventhorizon »

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

//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

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple switch question... <code>

Post by adafruit_support_bill »

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.

eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm

Re: Simple switch question... <code>

Post by eventhorizon »

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).

User avatar
adafruit_support_bill
 
Posts: 88141
Joined: Sat Feb 07, 2009 10:11 am

Re: Simple switch question... <code>

Post by adafruit_support_bill »

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

eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm

Re: Simple switch question... <code>

Post by eventhorizon »

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. :)

User avatar
richms
 
Posts: 558
Joined: Tue Jan 20, 2009 3:05 am

Re: Simple switch question... <code>

Post by richms »

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

eventhorizon
 
Posts: 114
Joined: Tue Jan 25, 2011 6:09 pm

Re: Simple switch question... <code>

Post by eventhorizon »

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.)

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”