Shop OBEX P1 Docs P2 Docs Learn Events
Switches and SX — Parallax Forums

Switches and SX

Brian CarpenterBrian Carpenter Posts: 728
edited 2006-06-22 02:25 in General Discussion
So I have 16 switches that i want to monitor with the SX48.· The switches are magnetic reed switches.· when a magnet rotates and passes near the switch it closes the switch (to ground).· I want to have 16 different Variables, one for each switch.··Each Variable will be set at 15 for instance at the beginning of the program. Each time the switch is closed (to ground) I want the associated variable to decrement by one, then send the new variable value to the PINK module where it can be seen via a web page.· My questions are
1.· Do i create a loop that just checks that status of each pin that switches are connected to?· If a change is seen, Decriment.

2. With other things that need to happen in the chip also do i just nest this loop into the rest of the program?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


It's Only A Stupid Question If You Have Not Googled It First!!

Comments

  • NateNate Posts: 154
    edited 2006-06-05 10:56
    The key information that you have left out is:

    a) What is the expected time interval (frequency)·of the switches being turned on?

    b) How long do you expect the switches to be in either state (ie, how long is the high period, how long is the low period)?

    c) What else is being done while monitoring the switches, just sending info to the Pink module?

    Nate
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-05 13:56
    The Switch will be pulled high for most of its existance. What i mean is that the it may go low for 1/2 a second every few hours. so my question is that i am looking for that 1/2 second low over a few hour period. Also the processor will be saving from a Human interface Device that works in and interupt.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-05 23:44
    was this a dumb question?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • NateNate Posts: 154
    edited 2006-06-06 00:02
    Your programming solution depended on the shortest time interval of your input switch (high or low).·

    With a 1/2 second "changed state" of your input (eternity to a micro controller), you can do this pretty much any way you want.· You can place a loop somewhere in your main code, as long as you are sure that the inputs are tested at intervals less than 1/2 second.

    A more elegant, but more complicated-to-program solution, would be to wire the input switches as a matrix (google "keypad matrix") attached to rb (interrupt capable port).· When the interrupt is triggered, you would need to decode the port interrupt register status to determine which input was triggered.· This way you would only spend the resources looking at the input switches when there was a input event.· (But if you are using the interrupt for other timing-dependant routines, this may not be possible).


    Nate
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-06 00:30
    i will be using the interupts with the HID. can i still use them for the Matrix. I need 2 for the HID

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • Clock LoopClock Loop Posts: 2,069
    edited 2006-06-06 01:03
    With that many inputs, you might want to think about using a few Parallel in - Synchronous Serial out chip... AKA the 74hc597, This method, you can latch all inputs at once, and shift them all into your microcontroller using only 3 pins on your microcontroller.'

    Just another way to go about your idea...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Meh. Nothing here, move along.
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-06 01:17
    I am not familiar with the 74hc597. If a switch goes high and then low, will it hold the high until the uCOntroller reads it?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • NateNate Posts: 154
    edited 2006-06-06 01:58
    If going the button matrix route, 4 I/O's can handle 16 inputs, 5 I/O's handle 32, 6 I/O's handle 64.....etc.·
    ·
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-06 02:18
    Nate, I am looking at the SX/B example matrix and for a 4x4 matrix it uses 8 IO's. am i looking at this wrong?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-06 03:24
    Based on the fact that i need 16 switches. i used the SX/B example for the 4x4 matrix, wired all the switches the same but in a configuration that i will ned for my machine (7 switches go in one location while the other 9 go to a different location)· The port pins will actually be the two different multi-pin connectors to my board.·See atachement for Diagram.



    So the vision is to use an older style vending machine and add electronics to it.· This machine uses a coin opperated rotary mechanism that you put your required coins into and turn the dial.· i will be installing magnetic reed switches in this mechanism with a neodym magnet attached to the rotaing part.· as the coins are validated and the mech is allowed to turn, the magnet will pass the reed switch and close the contact.· My thought is to have a variable for each switch (ie.. sw0-sw15) The variable called sw0 will be set to ,say 15 (the ammount of goods in that row) It the SX48 sees that the reed switch has made a connection then it will decrement the value to 14 and so on.· Eventually, i would like for the SX to update these variables to the PINK module and i will be able to log in and see current inventory.· Another good thought is to have the Pink send me an email if quantities drop below 3.· This may not seem like any huge project to some, but for me i work crazy long hours and am trying to learn SX/B and develop this in my wee remaining hours.· Any suggestions would be appreciated.· thanks


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • NateNate Posts: 154
    edited 2006-06-06 10:37
    Sounds like an excellent project to me.

    The keypad matrix that I was thinking about was one that used an·encoder (or perhaps 2 of the '74148) to turn your switch input into a binary representation.·

    Since the actual switch time that you are sensing is so long (1/2 sec. or so) it would be fine to use the canned SX/B keypad matrix and just make sure that you run the "check input status" routine at a frequency of greater than 2 Hz.

    Nate
  • BeanBean Posts: 8,129
    edited 2006-06-06 11:26
    Altitudeap,
    I think your SW6 should connect to RC.5 instead of RC.4 ?
    Because the picture shows SW6 & SW7 both connected from RC.2 to RC.4

    Other than that, looks like your on the right track.

    I would not worry about using interrupts for a signal that lasts 500 milliseconds.
    Have you thought about what should happen if two people try to vend different items at the same time ? I guess it could happen...


    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "I reject your reality, and substitute my own." Mythbusters
    ·
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-21 03:57
    can anyone point me in the right direction with a small bit of SX/B code to use the matrix switches as above. Each Switch needs its own variable. My guess is to use a 2 bytes and make each position of the byte a switch "status" ie.. if we have sw0, sw1, sw2, sw3, sw4, sw5, sw6, sw7 and sw4 was closed, then the Byte would be 00001000 am i on the right track. I am using the entire RC 'bank' (not the right term im sure) so can anyone give me a staring point? Thanks

    P.S. I am looking for a sort of latching effect. ie. switch closes for a few miroseconds, but i need the bit in the byte to stay 1 until other code processes the data and sets the bit low again.... even though the pin might have already gone low. Does this make sense?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • Brian CarpenterBrian Carpenter Posts: 728
    edited 2006-06-22 01:11
    BEAN?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    It's Only A Stupid Question If You Have Not Googled It First!!
  • BeanBean Posts: 8,129
    edited 2006-06-22 02:25
    Okay 4 of the pins will be the columns and 4 will be the rows.
    Setup one set (either the columns or the rows) to "drive", and the other set to "sense".
    Enable pull-ups on the sense pins.
    Loop through each of the drive pins making them LOW (others should be INPUTS).
    As you make each drive pin low, check if any of the sense pins are low. If it is, the switch must be closed.

    If you just want to know if ANY switch is closed, just make ALL the drive pins LOW. Then if ANY of the sense pins go low, you know a switch is closed. Then use the routine above to figure out which switch is close.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "I reject your reality, and substitute my own." Mythbusters
    ·
Sign In or Register to comment.