Push button help
Dr. Evil
Posts: 4
I want to connect a Push button to my stamp to toggle between values 0 through 2. (·0,1,2) So if i push the PB·a stored value will change·from 0 to 1. Then if i push the same PB again the value will change from 1 to 2. and then a third push will cause it to go back to 0.
Then i can write
IF (Stored value) = 0 then
IF·(Stored value) = 1 then
IF (Stored value ) = 2 then
you get the idea.
Then i can write
IF (Stored value) = 0 then
IF·(Stored value) = 1 then
IF (Stored value ) = 2 then
you get the idea.
Comments
DO
IF PB0 = 1 THEN PBValue = PBValue + 1 // 3 ' if pressed, add 1, modulus 3 (0-2)
Pause 100 ' time for debouncing
DEBUG ? PBValue ' Display
DO WHILE PB0 = 1 ' wait while pressed
LOOP
' Add your conditionals
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Electronic Systems Technologies
Southern Illinois University Carbondale
Personal Links - ·Lot of BASIC Stamp info
and
SelmaWare Solutions
StampPlot Pro Version 3 Release 4
Graphical Data Acquisition for your Micro and Imagination!
Now allows additional controls to be added, or developed.
·
Each·DEBUG command can be replaced with one or more of your own custom commands.
Post Edited (Andy Lindsay (Parallax)) : 2/14/2005 4:05:51 AM GMT
Assuming the PB is connected to P0, active-high, maybe something like this:
PBValue VAR Nib
DO
IF IN0 = 1 THEN PBValue = PBValue + 1 // 3 ' if pressed, add 1, modulus 3 (0-2)
Pause 100 ' time for debouncing
DEBUG ? PBValue ' Display
DO WHILE IN0 = 1 ' wait while pressed
LOOP
' Add your conditionals
LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Electronic Systems Technologies
Southern Illinois University Carbondale
Personal Links - ·Lot of BASIC Stamp info
and
SelmaWare Solutions
StampPlot Pro Version 3 Release 4
Graphical Data Acquisition for your Micro and Imagination!
Now allows additional controls to be added, or developed.
·
IF sequence = 3 THEN sequence = 0
Without the sequence = sequence + 1 command, the program wouldn't have gotten past zero.
just make sure you put delay after the button was pressed as a debouncer,
then loop until the button is released... see Martin's example.
just remove the loop if you want to continously increase the stored value while holding the button for a long time... see Andy's example
I prefer using the first technique. but really depends on your application.
Jim
[noparse][[/noparse]edit] BTW, as a general rule, it is better to see if a variable is greater than a limit instead of equal to a limit. That way is something doinks the variable contents that you weren't expecting, you'll still test true for greater than your limit, instead of infinitely running on past the limit. This is especially critical with process automation stuff, like robots.. [noparse][[/noparse]/edit]
Post Edited (Jim McCorison) : 2/14/2005 4:38:25 PM GMT
· sequence = sequence + 1 // 3
the value of sequence will always be 0 to 2.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA
Agreed
sequence = sequence + 1 // 3
...is a more elegant solution.
Dr. Evil,
The reason for your double push at 2 is because of the position in code where you are checking for 'sequence' vs where you are increasing the
value to 'sequence'. By the time you are checking 'sequence', the variable has already iterated through your program with a value of 3.
If you add...
CASE 3
DEBUG "state 3", CR
...you should see this more clearly. Otherwise if you change the line...
IF sequence = 3 THEN sequence = 0 ELSE sequence = sequence + 1
...to look like...
IF sequence = 2 THEN sequence = 0 ELSE sequence = sequence + 1
...your problem should be resolved.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe - Mask Designer III
National Semiconductor Corporation
(Communication Interface Division)
500 Pinnacle Court, Suite 525
Mail Stop GA1
Norcross,GA 30071