Simple Button
TheVig
Posts: 41
Could I get a simple example of when I push a button on a pin the serial terminal displays that button push?
Thanks,
Jim
Thanks,
Jim
Comments
You'll find all the basics explained there! You'll find examples for readin input and there is an addon regarding serial communication with PC/PST.
I'm drawing a blank (probably because I'm not good at the spin syntax yet) on sending a single command when the button is pressed, and then another single command when the button is let off.
Thanks,
Jim
You really need to work through the examples in the Propeller Education Kit Labs tutorials. Start simple, get each simple example working first, before you move on.
Sending a single command when the button is pushed, then another when it's released is a basic programming skill and has little to do with Spin syntax. It's the notion of a program having several states and the program shifting from one state to another when a particular event occurs. In this state, a loop (REPEAT) represents a state. The program moves from one loop (pushbutton down) to another (pushbutton up) when a specific event occurs (pushbutton released and pushbutton pushed respectively).
What you displayed in post #6 will output continuously to the serial terminal when the pushbutton is pushed. When people provide you with code examples, use what they give you first, before modifying it. You'll learn more and it will probably work better.
Any chance there might be a emulator out there so I can test code without an actual board in front of me? That would be rad!
-Jim
It'd be cool to be able to design in a virtual environment (software emulator of the quickstart board) before even beginining to start prototyping a real live board.
Oh..and by the way, here is an emulator: http://sourceforge.net/projects/gear-emu/
But I think it's PASM only
Just out of curiosity I wrote one myself, but discontinued that after a while because of more interesting stuff ;o)
When I push and hold down the button, this is what is output in the serial terminal: When I let off, it stops.
What I need it to do is say "Pushbutton pushed" and then stop while the button is being held down.
When I release, it needs to say "Pushbutton released".
I don't understand why this is being so difficult.
When the button is press the output is a combination of 11110000's
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Which I ran a program on to count, it is either 85 or 86 ones or zeros before transitioning.
I've been playing around with waitpeq functions with the same results of the cloogy repeat algorithm provided by Mike Green
Do you think I need to add timing or is my board defective?
-Jim
Not sure what else to suggest ...
Edit: When I modify my pulse program to flip level every 1/120th of a second (60Hz period) I get similar character counts to what you were reporting. Which suggests some form of noise. Does that make sense?
60Hz certainly sounds like AC pickup....
Try a Single Pole change over switch, (SPDT) between 0V , Vcc and see if the issues in #17 go away.
1. Using a P8X32A QuickStart Board
For pinout and description:
http://www.parallaxsemiconductor.com/products/quickstart
2. The switches are normally open momentary buttons
3. Using P9 (header 10) to monitor status of the pin
4. Using P16 wired to the onboard LED give an onboard visual status of the switch.
5. The propeller should transmit a serial signal to be read by a custom computer program (Right now the PST)
Q1. Should the switch be connected from from Pins P9 to Vss Ground (header 39)? Or something different?
Q2. What is the appropriate _CLKMODE for the P8x32A?
Q3. What is the appropriate _CLKFREQ for this application
Q4. How should the initial state of the P9 be configured. (set as input, set to low, etc...)
Below is code that doesn't work correctly. The Led on P16 turns on as soon as the code runs while the switch is open.
When the switch closes, the LED turns off.
Same thing goes for the PST, while the switch is open, the output alternates between ON and OFF quickly
When the switch is closed, transmission stops.
Note: I've played around with waiting for low vs high first since I am pulling it down with connecting to ground, but nothing seems to help...but perhaps I haven't nailed the right combination yet.
Extra questions I will have down the road once I start using something other than the PST:
Q5. Will the P8x32A transmit the serial information right over the USB connection where I can read the data with a program?
Q6. Do I need to manage the Com Port connection with the propellant dll in the program?
Q2/3 of course you can also run the quickstart with internal propeller clock RCFAST or RCSLOW. Don't know the right settings in this case - never used it.
Q4 using the classical method means that the pin is simply an input, so nothing to do when starting the propeller, as per default all pins are input after reset/switch on.
Q5/Q6 If I understand your questions correctly: yes and yes. You only have to make sure that your own program can free the COM port, so that it nicely works together with propeller tool or propellent. If your program does not close the port again, the propeller tool / propellent won't be able to upload new software.
First try a change-over toggle switch, between 3.3 and Gnd and verify those connections with a meter.
Check nothing else is driving your pin.
#17 and #18 sound like floating connection issues..?
Using the I/O and Timing Basics Lab example found in the Propeller Education Kit Labs book Version 1.2 page 46 I built this circuit:
When I click the button I get:
ONOFFONOFFONOFFONOFFON (sometimes more than others, but it does stop instead of just flowing constantly while the button is held down.)
When I release the button I get exactly what I want:
OFF
I'm so much closer! How do I get rid of all of those extra toggles when I hit the button.
Circuit? or Code?
-Jim
What you see is the button bouncing back and forth for a small amount of time when you push it. And your solution is fine even if I'd expect the bouncing to be more in the area of 100ms or even less. With one second wait-time you might not detect "double clicks". So, it very much depends on what you use the button for whether a more sophisticated debounce-code is needed.
I would like to have double taps, triple taps, so any suggestions on that "sophisticated debounce-code" (Perhaps an example of handling double taps with spin...which I was actually going to handle with the computer, but it might be better to just build them right in ;-)
Do I need a debounce-code or adjust the wait time? I'm wide open to suggestions here!
-Jim
P.S.
I'd like to thank everyone for jumping in and helping me out. I am so glad to finally get my project up and running. I've fiddled around with the QuickStart for the last year make lights blink and Smile...Did build a circuit and compiled code for measuring the resistance of a pot but bailed when I had troubles with Midi transmitting as garbage. My button now sends NoteOn and then NoteOff data. I am so HAPPY!
In human terms when you press the button the switch closes (once) and all works as expected.
In electronic terms that switch bounces several times (as you noticed)
So, when you detect the first closure, do a short waitcnt to let it stop bouncing.
How long? Dunno. Depends on the switch and how fast the processor is running.
Start with a few microseconds and see how that works?
Sommething like?
if ina[button] == 1 (with a pull DOWN, or 0 if using a pull UP)
waitcnt(someUS+cnt)
For the double click, is there a wait max for switch to toggle?
Tracking time between the ON and OFF, then another ON and OFF, the program should be able to tell if a double tap occured.
Does anyone have experience with a program like this, any catches I should be worried about? Especially if I try to ad a triple tap :cool: