Shop OBEX P1 Docs P2 Docs Learn Events
Momentary Push Button - how would it be done — Parallax Forums

Momentary Push Button - how would it be done

computer guycomputer guy Posts: 1,113
edited 2007-06-17 08:52 in Propeller 1
Hi

I need to interface a momentary button to my proto board to set a variable

0 = off
1 = on

as in you push the button and it switches the variable you push it again and it switches back.

how would this be done.
I am willing to use a cog if needed.

Thank you smile.gif
«1

Comments

  • IWriteCodeIWriteCode Posts: 16
    edited 2007-04-27 08:34
    You can probably detect a rising or falling edge... and when the edge is detected, flip the variable.

    Be sure to debounce the switch though... otherwise it'll become a mess...
  • computer guycomputer guy Posts: 1,113
    edited 2007-04-27 09:05
    Please explain, i don't get what you are saying. smile.gif
  • simonlsimonl Posts: 866
    edited 2007-04-27 09:29
    1. Wait for button press (make sure button is 'de-bounced') [noparse]/noparse]Hint: WAITPEQ _may_ be good enough, but will stop further processing [noparse];)[/noparse
    2. Toggle variable [noparse][[/noparse]Hint: Use the !]

    HTH.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheers,

    Simon

    BTW: I type as I'm thinking, so please don't take any offense at my writing style smile.gif

    www.norfolkhelicopterclub.co.uk
    You'll always have as many take-offs as landings, the trick is to be sure you can take-off again ;-)
  • OzStampOzStamp Posts: 377
    edited 2007-04-27 09:49
    Hi PuterGuy

    Follow this link it explains contact bounce·· hop.gif

    http://www.elexp.com/t_bounc.htm

    The code on that link is not related to the Propeller.
    But the pictures tell the story really..

    Cheers

    Ronald Nollet·· Australia
  • computer guycomputer guy Posts: 1,113
    edited 2007-04-27 09:59
    So

    I would detect the button being pushed wait a set amount of time see if the button is still pushed and if it is come to the conclusion that the button was pushed.

    How do you wire a button to the prop and how do you detect if it was pushed?

    Would you recommend using a new cog for the button detection?

    Thank you smile.gif
  • mahjonggmahjongg Posts: 141
    edited 2007-04-27 11:01
    Computer guy said...
    I would detect the button being pushed wait a set amount of time see if the button is still pushed and if it is come to the conclusion that the button was pushed.
    Yes, basically that is it.
    Computer guy said...
    How do you wire a button to the prop and how do you detect if it was pushed?
    Just connect the the button between a I/O pin and ground, then add a 10K resistor between the I/O pin and 3,3Volt. When you press the button you will read a '0', otherwise you will read a '1'.

    Computer guy said...
    Would you recommend using a new cog for the button detection?
    Not necessary, the code for button detection is simple and can be easiliy integrated with the rest of your application, however if you want another process to run while testing for the button it might be easier to test with a second COG.

    Mahjongg
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2007-04-27 12:05
    Tony, are you for real? You were asking these basic switch questions on one of the forums last year. Now, if you are looking for cheap entertainment you could try frying ants with a magnifying glass.

    Dedicating a 20MIP 32-bit processor to sense a switch? Seriously, you are joking, aren't you? That was a good lark, it probably got a few people. Let's see, that means that one propeller chip can handle 8 switches, wow!

    Thank you smile.gif

    *Peter*

    P.S. I'm an aussie and I can prank too!
  • computer guycomputer guy Posts: 1,113
    edited 2007-04-27 12:50
    Who is Tony? and i was just interested in knowing if the prop could handle checking an input every second and doing all the other work it is doing at he same time on one cog.
    Hence
    Computer guy said...

    Would you recommend using a new cog for the button detection?

    Thank you smile.gif
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2007-04-27 13:14
    You're running a sub-domain off Anthony Myatt's website so if you must know Tony somehow so why ask who is Tony? Plus you have built what looks like a robot but this does not seem possible if you do not know how to wire a switch. Isn't that right? http://forums.parallax.com/showthread.php?p=640635

    So either you didn't build it or you are dead straight "having us on". Compatriot Ron also answered you and gave you a link which showed a switch connected to a logic input. As I also mentioned before, you have asked previously about hooking up switches, it's not exactly rocket science.

    Us engineers are sometimes the most helpful and at the same time most gullible people in the world (at least when it comes to technical questions).

    *Peter*
  • rjo_rjo_ Posts: 1,825
    edited 2007-04-27 15:58
    Peter,

    Frying ants with a magnifying glass requires a steady hand; so first, he would have to stop drinking, then he would have to take Propranolol. I would suggest a cell phone, if you can fry an eggs with it... ants should be a "slam dunk" to quote my favorite leader.

    For the non-engineers out there... I'm one of you... and I actually didn't know how to hook up a button... there are lots of examples, you really can't miss them. I think the one of the deeper points of computer guy's question has to do with the spec sheet... and to that I would say to computer guy... "why don't you try writing a better one." You could start to fail by relating voltage to bulk processing load... and fail a little bit more by trying to order specific cog activities.

    In terms of entertainment value... engineers are the best... following closely behind high military humor (which can be deadly) and of course, political sophistry, which is only slightly less benign. I'm even beginning to think that God has a really deep sense of humor... John 7:5.

    AND... it does help when you guys tell everybody that it is a joke... because sometimes we suspect, but we really don't know[noparse]:)[/noparse]

    Rich
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-04-27 17:24
    Peter,

    Alternatively this guy might be a young whipper snapper not aware that Tony is short for Anthony, assuming that "Tony" goes by the name Anthony and is a friend doing the site not the man in question.

    Then it might be that his robot is actually two motors and a plastic box at the moment and it does not require knowledge of microprocessors and how to connect switches.

    My advice is this.

    1. If this is a joke, ha ha
    2. If not ditch the robot for a bit and read a basic book on electronics and start learning to program the propeller then combine the two.

    Graham
  • Tracy AllenTracy Allen Posts: 6,656
    edited 2007-04-27 17:43
    I won't get into frying ants or into George Tenent's new book that tells about the situation room and the infamous "slam dunk". How might that relate to pressing buttons, scary, off topic.

    Taking it seriously, and assuming one knows how to connect the pushbutton to the input with a pullup resistor and perhaps a debounce capacitor and another resistor to protect the input from ESD, there remains the code. Under many circumstances it is best not to do it with a WAITPxx, because the program has to do other tasks in parallel.

    Assuming a main repeat loop that executes "often enough", it would look something like this snippet:

    oldstate := inA[noparse][[/noparse]0]  ' initialize state variable
    
    
    repeat
      newstate := inA[noparse][[/noparse]0]   ' current state of input
      xstate := newstate ^ oldstate & newstate   ' detect change of state, 0->1
      oldstate := newstate   ' push state down
      if xstate := 1
        !myvariable     ' or do whatever needs to be done. xstate=1 only once through the loop
      ' other program action here, then repeat
      ' this has to repeat often enough so as not to miss both the high state and the low state
    



    This does not include debouncing, but sometimes it is not necessary, or else it can easily be accomplished in hardware with a capacitor. The same state machine can detect change in several pins simultaneously, as in
    newstate := inA[noparse][[/noparse]0..7]
    for 8 at once. Then the xstate variable is nonzero only when there is a change, and the decoding of which event occured can follow the IF.

    Debouncing can of course be done in firmware. The most efficient way to do that with state machine logic that can be included in the flow of a program is usually with a vertical counter. Past states of an input are held in a certain bit position of several variables, and all those bits have to agree as 0 or 1 (a full count) before the end state is considered to have reached 0 or 1, and then that determines the xstate. It is not a trivial problem.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Tracy Allen
    www.emesystems.com
  • computer guycomputer guy Posts: 1,113
    edited 2007-04-28 01:35
    Yes i am Anthony, I am building that robot and this is part of it.
    I would rather not be called Tony as that is not my name, some Anthony's may exept Tony as an alternative way of saying Anthony but not me. Thank you smile.gif

    I was not asking how to connect a switch but a momentary switch/button, therefore I was wondering if any special circuitry was needed for the switch to detect the short amount of connectivity.
    For any future references I do not joke on any forum, and don't intend to in the future.
    Joking is a waste of everyones time and knowledge.

    Thank you all, especially Tracy Allen for supplying the example code smile.gif

    Post Edited (computer guy) : 4/28/2007 1:43:26 AM GMT
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2007-04-28 12:52
    Ok, in the interests of helping you out here is a super simple circuit that will work with even the slowest software polling. The idea is that a momentary press of the button will discharge the capacitor and that it will take several hundred milliseconds to charge up again through the 1M resistor. Ok, so it's not really any different from the normal debounced circuit except for the long RC time constant but the 470R resistor is to allow for the I/O pin to become an output and charge the capacitor back up sooner if you need to.

    So in a way it will stretch out the press of the button but your software should still implement a simple state machine by retaining the last state that was read and comparing this with it's current state (and then updating the last state). If it was high last time and it's low now then that can count as a press. Conversely if it was low last time and high now then that could count as a release (if that was required). Low before and low now don't count same as high before and high now as they are steady state and not transitions. Spin code for this is easy but don't ask me, just go and give it a spin.

    Nuff said, if you have any questions and you really want to learn then try to find out for yourself first or else we could be here a long time explaining the minutest detail and getting nowhere. We can but help, the doing part is yours.

    *Peter*

                      VCC
                       +
                       |
                       |
                      .-.
                      | |1M
                      | |
                      '-'
                       |      ___
              .--------o-----|___|----.I/O PIN
              |        |     470R
            | o        |
          |=|>        ---
            | o       ---1uf
              |        |
              |        |
             ===      ===
             GND      GND
    (created by AACircuit v1.28.6 beta 04/19/05 www.tech-chat.de)
    
    
    
  • computer guycomputer guy Posts: 1,113
    edited 2007-04-29 12:01
    Thank you smile.gif

    Will consider using that. Looking at the spec sheet for my button i don't think i will need a bounce/debounce circuit.
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-05-01 19:09
    If you think that you no not need to debounce your push-button, then it probably is an electronic one. And, if it is (three connections; Vdd, Vss, Out) you just connect it without any more components.

    But I doubt that it is electronic - you didn't say anything about three connections - and then you NEED either SW debounce or HW debounce. Trying to toggle a variable with a mechanical switch will otherwise be very frustrating since it will toggle x times each time you press/release that button.

    Post Edited (Skogsgurra) : 5/1/2007 7:28:25 PM GMT
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-05-01 19:11
    Heck! That picture again! I HAD turned it off! Are these pictures sticky?

    Post Edited (Skogsgurra) : 5/1/2007 7:28:04 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-01 19:14
    ROFL, go into Control Panel>Edit Profile then click in the signature box and·hit Ctrl-A then Delete and finally Submit.
    That should permanently take care of it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-05-01 19:16
    TESTING - Do not Roll OFL I it goesn't work!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-05-01 19:17
    doesn't - but it did...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-05-01 19:18
    It worked, but your sig is a post by post thing, you need to edit previous posts to change your sig in those posts.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • computer guycomputer guy Posts: 1,113
    edited 2007-05-01 23:45
    My button is a computer reset switch.

    Thank you smile.gif
  • Shane De CataniaShane De Catania Posts: 67
    edited 2007-05-02 00:44
    Hey computer guy,
    For someone who never jokes on tech forums you've really made me laugh this morning!
    Cheers,
    Shane.
  • computer guycomputer guy Posts: 1,113
    edited 2007-05-02 00:54
    Shane

    What made you laugh.

    Thank you smile.gif
  • SkogsgurraSkogsgurra Posts: 231
    edited 2007-05-02 15:37
    Hello Computer Guy. It may be my mishaps with that picture that made Shane laugh. But it may also be the fact that a reset button doesn't need all these things we have been discussing. The reset is kind of "that's it" thing. Bounce doesn't matter and the toggling is not really an option.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • computer guycomputer guy Posts: 1,113
    edited 2007-05-06 00:15
    Skogsgurra said...

    But it may also be the fact that a reset button doesn't need all these things we have been discussing. The reset is kind of "that's it" thing. Bounce doesn't matter and the toggling is not really an option.

    I didn't think i would have to worry about bounce, but it was good to learn about bounce along the way.
    As long as the prop can detect the short amount of time the button is pushed it should be able to switch the variable, right?

    Thank you smile.gif
  • Graham StablerGraham Stabler Posts: 2,507
    edited 2007-05-07 20:00
    I think Skogsgurra now thinks you are using the switch as a reset when in fact it is just a push button from a computer that was used for reset.

    You may still have to worry about bounce but you will soon find out when you actually implement it (suprised you havn't already).

    The prop will have no time detecting the short time it is connected, that's why you can get problems with bounce.

    Graham
  • computer guycomputer guy Posts: 1,113
    edited 2007-05-08 08:52
    Thank you good to hear.
    I haven't implemented it as of yet as my project is rather big and i was having a brake from it for a while.

    Thank you smile.gif
  • computer guycomputer guy Posts: 1,113
    edited 2007-06-17 05:40
    I implemented the button about a month ago and all is working well.

    Thank you all for your help. smile.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Check out my robot using the propeller robot.tmcp.com.au
    If you offer cheap PCB fabrication, perl programming or any other helpful services please email me at.
    anthonybmyatt@yahoo.com.au
  • deSilvadeSilva Posts: 2,967
    edited 2007-06-17 08:52
    I think debouncing switches and how to use the results with a propeller is a serious matter despite this hilarious thread:

    The Propeller has (at least) two “shortcomings”
    - no interrupts
    - no internal pull-up for inputs

    The second is a pity, as we shall need at least an external pull-up (or – if you prefer – pull-down): 10 k Ohm is appropriate. But that should be all of hardware – no “C”s please if we can avoid it.

    A question rarely answered explicitly is: How long does a switch bounce? Bouncing happens on a mys-scale and it will of course depend on the specific switch, but after at most 10 ms most switches have reached a stable state.

    As there are no “interrupt routines” the basic question is: How should the “main program” be informed that this switch had been pressed?

    This is a question of global program logic! Is the program running in a loop? If yes, is it exactly timed, and with what cycle time? Do we just want to check if the switch is being pressed at the moment, or if it had been pressed during the last quarter of an hour? Or do we desperately wait for a press?

    The best idea is probably to use one COG as a basic “Operating System”, cycling in a – say – 1 ms loop and regularly checking whatever is to check (and putting results into HUB- memory)

    This is useful for all kinds of bookkeeping and though it may start small it will quickly grow with your needs

    The “OS” should set two bits: A and B for each monitored switch:
    A: switch is pressed at the moment
    B: switch is not pressed at the moment but was some time ago
    After reading B the main program should reset B.

    An simple algorithm for such kind of monitoring (Listing 2 and 3) can be found in a(longer) article by Jack Ganssle (http://www.ganssle.com/debouncing.pdf ) with lots of other related useful information
Sign In or Register to comment.