Momentary Push Button - how would it be done
computer guy
Posts: 1,113
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
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
Comments
Be sure to debounce the switch though... otherwise it'll become a mess...
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
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 ;-)
Follow this link it explains contact bounce··
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
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
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'.
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
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
*Peter*
P.S. I'm an aussie and I can prank too!
Hence
Thank you
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*
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
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
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:
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
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
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
Post Edited (computer guy) : 4/28/2007 1:43:26 AM GMT
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*
Will consider using that. Looking at the spec sheet for my button i don't think i will need a bounce/debounce circuit.
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
Post Edited (Skogsgurra) : 5/1/2007 7:28:04 PM GMT
That should permanently take care of it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Paul Baker
Propeller Applications Engineer
Parallax, Inc.
Thank you
For someone who never jokes on tech forums you've really made me laugh this morning!
Cheers,
Shane.
What made you laugh.
Thank you
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
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
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
Thank you all for your help.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
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
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