Shop OBEX P1 Docs P2 Docs Learn Events
Press button for certain of time — Parallax Forums

Press button for certain of time

BronsonBronson Posts: 44
edited 2010-10-24 20:37 in BASIC Stamp
Hi,

How can I code: IF button is pressed for 3 sec THEN GOTO procedure1

Can I use BUTTON command? I don't quite understand this BUTTON command.

I am asking this because I am doing a project, but the input pin seems very unstable.
The input can change state by itself even if the button is not pressed at all.
I am using active-low connection for push-button with 10k resistor tied to Vdd.

My testing code is:
=======
start:
DEBUG BIN IN0
GOTO start
=======

The result when the button is not pressed at all:
111111011111111011110111111111011111111111111111

Pls help me....

Regards,
Bronson

Comments

  • LeonLeon Posts: 7,620
    edited 2010-10-24 08:06
    How long are the connections to the push button? The input might be picking up noise.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-24 08:06
    It looks like you have a bad connection to the 10K resistor.

    To test for a button held down for a period of time, you need to keep testing the button in a loop, every 1/10 second or 1/4 second or something like that and keep a count of the number of times it's been tested. If the button goes off, then exit the loop. If the total time (count x delay) is greater than your limit, then you have success. Substitute IN0 or IN1 or whatever for button in the following:
    startOver:
    if button = 0 then goto startOver ' make sure button is released
    count = 0
    waitTime:
    pause 100 ' this sets the time tick.  Here it is 100ms
    if button = 1 then goto startOver ' start over if button released
    count = count + 1
    if count < 30 then goto waitTime ' time limit not yet reached
    ' here we do something if the button is held at least 3 seconds
    
  • BronsonBronson Posts: 44
    edited 2010-10-24 08:24
    Thanks for the quick reply...what a great forum.

    Leon,
    The input will change state by itself every approx 30sec-4min.
    I think you were right...it seems like my input is picking up noise as I am putting my PCB inside a panel loaded with relays, cables, and contactors. But only 1 input is having this problem. The other 7 inputs work perfectly. If it is noise problem, how can I fix it? Can I use shielded cables for the input cable?

    Mike,
    Thanks alot for the code, I have saved it already.

    By the way, while searching this forum, I found a link posted by PJ Allen:
    http://forums.parallax.com/attachmen...chmentid=42013
    Is it relevant to my problem?
  • LeonLeon Posts: 7,620
    edited 2010-10-24 08:49
    Try a capacitor (100nF) to ground at the Stamp input pin.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-10-24 08:56
    You can also use a lower value resistor, something like 2.2K or 1K.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2010-10-24 09:33
    Try this setup is a little different that the one in the WAM book but it work very well for me
    I do all of my inputs or switches this way

    in the past I have use this setup to do two different thing with one switch different cntr = values some time this is not easy to do how ever

    you also try this this works very well


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    '
    '
    chg PIN 1
    cntr VAR Byte
    '
    '
    DO
    '
    cntr = cntr + 1 * Chg ' Input must = 1 for couter to count
    IF (cntr = 5) THEN EXIT ' cntr = 1 to 255 count can be any thing in that range
    '
    'OR
    '
    cntr = cntr + 1 * (1 - Chg) ' Input must = 0 for couter to count
    IF (cntr = 255) THEN EXIT
    '
    LOOP
  • ercoerco Posts: 20,256
    edited 2010-10-24 09:57
    It is possible that your pin got damaged. You could switch this suspect input pin with one of your 7 other pins known to work properly and see where the problem is.
  • BronsonBronson Posts: 44
    edited 2010-10-24 20:37
    Wow...Thanks everyone.

    Erco,
    Why didn't I think of that simple trick? Thx for the advice..Will do as you say.

    Leon,
    Can you please send me pic of the connection using 100nF cap? Just to be sure

    Sam,
    Thx for the code and pic, I will try your idea

    Mike,
    I will change the resistor as you say

    Hope that one of the suggestion will work....
    Regards,
    Bronson
Sign In or Register to comment.