Shop OBEX P1 Docs P2 Docs Learn Events
Momentary switch — Parallax Forums

Momentary switch

philipad23philipad23 Posts: 44
edited 2006-02-08 15:38 in BASIC Stamp
Hi all...
I want to know how I can work with a switch that is presses and release immediately. I have the following code that I am working on:

INPUT 12
j VAR Word

Main:
IF IN12=0 THEN increase
...
..
.
GOTO main

Increase:
j=j+1
RETURN

Increment of j works only when the switch is pressed all the time..
Anyone can help me?
Thanks

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-08 04:43
    It's not clear to me what you're trying to do.· Are you trying to make the count keep increasing after you release the switch?· If so just jump to that routine.· Also, you're using a RETURN where there is no GOSUB.· This is probably restarting your program.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Philip GamblinPhilip Gamblin Posts: 202
    edited 2006-02-08 05:20
    Hi all...
    You need to get your INPUT statement inside your loop. Next you need to initialize your variable j.· As Chris said RETURN is for use with GOSUB. Try the following. It's an endless loop that does INPUT 12 really fast. Oh yeah don't forget to DEBUG j so you· can see the result. Depending on your switch, you may have some contact bounce, so your results may vary from what you expect.




    j VAR Word

    j=0

    Main:

    input 12
    IF IN12=0 THEN j=j+1
    PAUSE 200


    GOTO main
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-02-08 06:39
    Here's a cleaner version:

    Btn···· PIN··· 12

    total·· VAR··· Word

    Reset:
    · total = 0

    Main:
    · DO
    ··· total = total + ~Btn
    ··· DEBUG HOME, DEC total
    · LOOP
    · END

    In truth, the total = 0 is not actually required unless you want to clear it after it as been modified, because the Stamp clears all variables to 0 on reset.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-02-08 15:38
    Philip Gamblin said...(trimmed)
    Hi all...
    You need to get your INPUT statement inside your loop. Next you need to initialize your variable j.· As Chris said RETURN is for use with GOSUB. Try the following. It's an endless loop that does INPUT 12 really fast. Oh yeah don't forget to DEBUG j so you· can see the result. Depending on your switch, you may have some contact bounce, so your results may vary from what you expect.
    Philip,

    ·· Actually putting INPUT 12 inside the loop won't do anything in this code.· All the line does is make P12 an input, but since P12 is never changing direction repeatedly using this statement has no additional effect.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
Sign In or Register to comment.