Shop OBEX P1 Docs P2 Docs Learn Events
Has anyone written a program that scans a set of switches to see which ones are on? — Parallax Forums

Has anyone written a program that scans a set of switches to see which ones are on?

HalloweenBobHalloweenBob Posts: 2
edited 2013-12-04 20:56 in BASIC Stamp
I want to make a program that will scan 12 switches continuously, and determine how many and which ones are on at any given time.

The switches are wired individually, but could be wired in a matrix if that would help.

Then after gathering that information, I need it to output by closing a matching set of switches in rapid succession.

It should work like this:

1. It polls all the inputs and determines which switch or switches are closed at any given time.

2. The output of the circuit is a rotating series of quick switch closures representing each switch that is closed at it's input. I need 12 outputs from this circuit. each output is a set of normally open contacts which become closed (0 ohms) in order to 'short out' the contacts of a button on a transmitter.

So, as described earlier, this circuit would poll the input switches. As an example, I am going to say that inputs 3, 4, 7 and 8 are all closed at the time of the polling.

The output of this circuit needs to behave as follows: It needs to create a zero ohm connection between the two output wires for outputs 3,4,7 and 8 in the following manner:

Output 3 is closed for 200ms
Output 3 is then opened and there is a 50ms delay
Output 4 is closed for 200ms
Output 4 is then opened and there is a 50ms delay
Output 7 is closed for 200ms
Output 7 is then opened and there is a 50ms delay
Output 8 is closed for 200ms
Output 8 is then opened and there is a 50ms delay
REPEAT this pattern for as long as the input state remains the same.


I am just guessing at the 200ms and the 50ms times. I would like to have the ability to adjust those times so that I can discover what the optimal times are. I am assuming that there might be a problem if there is no delay time in-between releasing one button and pushing another. With no delay at all, my transmitter may get confused, and not be able to react fast enough to open the first connection before the second one is made locking it out.

To explain about the transmitter....
I have a transmitter with 12 buttons on it. I have removed the buttons and attached bare wires where they were. Connecting any of those wires to GND on the transmitter completes the circuit and for all purposes, closes the switch. When a switch is closed, the associated relay in the receiver is energized as you would expect. Not complicated. Push button 3 and relay 3 becomes energized wirelessly.

The problem is this transmitter can only send one signal at a time. The switches are all momentary switches, so if I press and hold switch one, Relay one closes, but while I'm holding down switch one, I can not energize any other relays. Pushing additional buttons at the same time does nothing.

I want to 'pulse' the buttons so that each button that I want to hold down at the same time, gets pressed quickly, then the next, then the next and then repeats with short pauses between them.

Hopefully I have explained this well enough.

I am completely new to the basic stamp world, but understand programming of this sort well enough to take something that exists and modify it to suit me. I am fairly sure I am not the first person to need something like this, so if you have already invented this wheel, please share if you will to give me someplace to start.

Thank you!

Comments

  • Prophead100Prophead100 Posts: 192
    edited 2013-12-04 19:05
    Search for code on reading a keypad and driving a set of relays. It should be doable on the STAMP although it would be a breeze on the Propeller with objects already written such as (keyboard) http://obex.parallax.com/object/144 and (digital I/O) http://obex.parallax.com/object/442
  • SapphireSapphire Posts: 496
    edited 2013-12-04 20:28
    Here is an example you can work from. Let's assume 8 switches and 8 relays. That way you can test it on a BS2 without any extra hardware. If you have a BS2p40 then you have 32 I/O pins and you could do 16 switches and 16 relays without extra hardware.

    This assumes you do not have a matrix of switches. If you have that, then you could read more switches with less inputs, but the method of determining which switch is on would be different.

    This also assumes a switch that is on generates a high input, and you turn an output on (high) to drive the relay closed.

    The relays are connected to P0-P7 as outputs in order (i.e. P0 = relay 1, P1 = relay 2)
    The switches are connected to P8-P15 as inputs in order (i.e. P8 = switch 1, P9 = switch 2)
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    CloseTime CON 200              ' close time (200ms)
    DelayTime CON 50               ' delay time (50ms)
    
    switches VAR Byte              ' variable for holding polled switch status
    i VAR Nib                      ' loop variable
    
    DIRS = $0F                     ' P0-P7 are outputs, P8-P15 are inputs
    
    DO                             ' forever
      switches = INH               ' read all 8 switches at once
      DEBUG BIN8 ? switches        ' show switch status (for testing only)
    
      FOR i = 0 TO 7               ' 8 switches/relays
        IF switches & (1<<i) THEN  ' switch i is on
          HIGH i                   ' turn relay i on
          PAUSE CloseTime          ' wait for CloseTime
          LOW i                    ' turn relay i off
          PAUSE DelayTime          ' wait for DelayTime
        ENDIF                      '
      NEXT                         ' next switch/relay
    LOOP                           '
    
    END                            '
     
    
  • HalloweenBobHalloweenBob Posts: 2
    edited 2013-12-04 20:56
    Wow! Thanks so much for the replies and the program example!

    Prophead mentioned the propeller.

    Would this be what you are talking about? http://parallax.com/product/32150

    What else would I need if I bought that propeller board, or am I looking at the wrong one?

    And as for the program posted by Sapphire, Thank you! I would need 12 inputs and outputs for sure.

    What other components would I need to achieve that? If I can do it equally well with the propeller or the BS, I might as well choose the option with the fewest parts and lowest cost. Which would be the way to go out of these options. Or is there yet another option that I haven't even thought of yet?

    Thanks for the fast helpful responses!
Sign In or Register to comment.