PDA

View Full Version : Push button help



Dr. Evil
02-14-2005, 10:09 AM
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.

Martin Hebel
02-14-2005, 10:46 AM
Assuming the PB is connected to P0, active-high, maybe something like this:

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 (http://imsinet.casa.siu.edu/hebel/links_personal.htm)
and
SelmaWare Solutions
StampPlot Pro Version 3 Release 4 (http://www.stampplot.com)
Graphical Data Acquisition for your Micro and Imagination!
Now allows additional controls to be added, or developed.
·

Andy Lindsay (Parallax)
02-14-2005, 10:46 AM
Try this program with the pushbutton circuit (http://forums.parallax.com/attachment.php?attachmentid=37093) on Page 76 of What's a Microcontroller v2.2. It's available for free download from www.parallax.com -> Downloads -> Stamps in Class Tutorials (http://www.parallax.com/html_pages/downloads/siccurriculum/documentation_sic_curriculum.asp).



' {$STAMP BS2}
' {$PBASIC 2.5}

sequence VAR Nib

DO

DO: LOOP UNTIL IN3 = 1
DO: LOOP UNTIL IN3 = 0

IF sequence = 3 THEN sequence = 0 ELSE sequence = sequence + 1

SELECT sequence
CASE 0
DEBUG "state 0", CR
CASE 1
DEBUG "state 1", CR
CASE 2
DEBUG "state 2", CR
ENDSELECT

PAUSE 100

LOOP


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

Martin Hebel
02-14-2005, 10:50 AM
Ack, correction to mine:

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 (http://imsinet.casa.siu.edu/hebel/links_personal.htm)
and
SelmaWare Solutions
StampPlot Pro Version 3 Release 4 (http://www.stampplot.com)
Graphical Data Acquisition for your Micro and Imagination!
Now allows additional controls to be added, or developed.
·

Andy Lindsay (Parallax)
02-14-2005, 10:54 AM
Ack, correction to mine too. It is now corrected on the earlier post. The first draft of the IF...THEN statement incorrectly read:

IF sequence = 3 THEN sequence = 0

Without the sequence = sequence + 1 command, the program wouldn't have gotten past zero.

Pinoy NYC
02-14-2005, 10:54 AM
Dr. Evil,

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.

Dr. Evil
02-14-2005, 02:16 PM
When going from 2 to 0 I have to push the PB two times. 0 to 1 toggles on one push, 1 to 2 toggles on one push, but 2 back to 0 requirees two pushes?



' {$STAMP BS2}
' {$PBASIC 2.5}

sequence VAR Nib

DO

DO: LOOP UNTIL IN3 = 1
DO: LOOP UNTIL IN3 = 0

IF sequence = 3 THEN sequence = 0 ELSE sequence = sequence + 1

SELECT sequence
CASE 0
DEBUG "state 0", CR
CASE 1
DEBUG "state 1", CR
CASE 2
DEBUG "state 2", CR
ENDSELECT

PAUSE 100

LOOP

Jim McCorison
02-15-2005, 12:35 AM
When I was first learning to program and things didn't work right, I was told to grab a pencil and paper and play computer. Step through the actual program writing down the values of each variable as they change. What you'll find is a bit of a logic error in the IF statement. When you loop through with a sequence value of 2, it fails the 'IF sequence = 3' test, so it adds 1 to sequence. Sequence now equals 3 which isn't a valid CASE in the SELECT statement. The program loops around again, and now sequence = 3 so it is reset to 0. Thus it takes two pushes to get from 2 to 0. Instead try:




sequence = sequence + 1
IF sequence > 2 THEN sequence = 0




Jim

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..

Post Edited (Jim McCorison) : 2/14/2005 4:38:25 PM GMT

Jon Williams
02-15-2005, 12:39 AM
Marty offered the most elegant solution, using the modulus operator. By doing this:

· sequence = sequence + 1 // 3

the value of sequence will always be 0 to 2.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas, TX· USA

Beau Schwabe
02-15-2005, 01:31 AM
Jon,

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
-------------------------------------------------------------------------------------