Shop OBEX P1 Docs P2 Docs Learn Events
Need a way that the BS2 always recognizes a button push to terminate a loop. — Parallax Forums

Need a way that the BS2 always recognizes a button push to terminate a loop.

nellastevnellastev Posts: 10
edited 2007-05-04 03:36 in BASIC Stamp
Hi - I am trying to use a pin being pulled low or high (I am pulling high) to terminate a loop.· What I have found is that his doesn't always work, sometimes I have to push the button numerous times before the loop terminates.· Does anyone know what might be the issue?· Just have a simple divider on a push button switch. Thanks

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-04-30 19:08
    Hello,

    Without a diagram and your code the only thing I can note to you is that you don’t want a voltage divider if that is what you have. The schematics for both active high and active low push button circuits can be found in the Help File or in the BASIC Stamp Manual under the BUTTON command. That said, you can exit the loop using one of the following two routines. Take care.
    DO
      ...
    LOOP WHILE IN0 = 0   ' Active High
    

    [size=2][code]
    DO
      ...
    LOOP WHILE IN0 = 1   ' Active Low
    


    [/code][/size]
    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • metron9metron9 Posts: 1,100
    edited 2007-04-30 19:15
    You did not post your code but it is likley that is the problem.

    If you check to see if a pin is low when a button is pressed but you check the pin just before and just after the button is pressed it doesnt see the button.

    Three solutions. 1. An interrupt, 2.Polling the pin faster than the button can be pressed. 3.Creating a latching circuit.

    Since you are using the BS2 , solutions 1 and 2 wont work.

    Use the circuit below and a HIGH signal will be the button press. After you receive the button press as a high signal, take the pin LOW output and discharge the capacitor, loop the program until the signal stays low (the button must be unpressed) to reset the latch.


    Depending on the speed you need between button presses, other circuits may be a better choice. this will take care of most situations.

                         1 meg ohm
    
    INPUT PIN--------/\/\/\------------GND
                      |
                      |
            220 ohm resistor here
                      |
                       -------Capacitor------GND
                      |
                      |
                      ---------/\/\/\-------___________-------+5V
                                 220 ohm     push button
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Think Inside the box first and if that doesn't work..
    Re-arrange what's inside the box then...
    Think outside the BOX!
  • TechnoRobboTechnoRobbo Posts: 323
    edited 2007-05-01 19:18
    Here's an alternate solution - a modified shcmitt trigger.· circuit holds button press until reset by bringing pin 1 low
    (Reposted BS2 program - had a typo):

    HIGH 1
    INPUT 2
    buttoncount VAR Byte
    waitcount·· VAR Word
    DO
    · DO WHILE IN2=0
    ··· PAUSE 10 'delay for testing - not necessary
    ··· DEBUG HOME,?buttoncount,?waitcount
    ··· waitcount=waitcount+1
    · LOOP
    · buttoncount=buttoncount+1
    · DEBUG HOME,?buttoncount
    · PAUSE 500
    · PULSOUT 1,10 'reset latch
    LOOP

    attachment.php?attachmentid=46961

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Have Fun


    TR

    Post Edited (TechnoRobbo) : 5/1/2007 8:48:01 PM GMT
    655 x 343 - 21K
  • nellastevnellastev Posts: 10
    edited 2007-05-03 20:54
    Hi Here is the code:

    PushBtn········ PIN···· 3·············· ' pushbutton on P0
    Spkr··········· PIN···· 7············· ' output pin for FREQOUT

    Main:
    · OUTH = %00000000
    DIRH = %11111111
    · DO
    · DEBUG "Low E", CR
    · OUTH= %00000110·········· 'E
    ···· PAUSE 3000
    ···· FREQOUT Spkr, 3000, 82
    LOOP WHILE IN3 = 1·· ' Active Low
    DO
    · DEBUG "A", CR
    · OUTH = %00001000········· 'A
    ···· PAUSE 3000
    ···· FREQOUT Spkr, 3000, 110
    LOOP WHILE IN3 = 1·· ' Active Low

    Continues on... for 4 more loops·· - I checked the signal and indeed it is pulled low.· I can hold the button down low for seconds before the BS2 recogizes the pin is low - is this a code issue?· thanks
  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-03 20:58
    The problem you're running into is that the Stamps are single threaded processors. They can only do one thing at a time. When they're executing a PAUSE or FREQOUT statement, they're doing nothing else until that statement is finished. That's why they can't see the change in pin #1 until the PAUSE and FREQOUT are finished.

    For long pauses, you can get around this by using shorter pauses (like 100ms) in a loop that checks for the change in pin #1. You can't really fix this for the FREQOUT statements since there's a few milliseconds before a check of pin #1 can be done and the FREQOUT restarted. That will show up as noise or interruptions in the sound output.
  • hitswarehitsware Posts: 156
    edited 2007-05-03 21:33
    >That will show up as noise or interruptions in the sound output.

    That's the understatement of the year [noparse]:)[/noparse]

    Even if no other instructions are involved (simply updating the frequency)
    produces a big pop.

    This (I think) is a result of the pin going active low at the end of the duration.

    To go back into operation it must jump (dc wise) the 2.5 V necessary to
    center the PWM sinewave.

    No amount of LP filter will bypass that big of glitch
    (without also bypassing the desired signal)..)

    Perhaps if the pin went to input (open)
    after the duration it would help....· ?????????

    Post Edited (hitsware) : 5/4/2007 12:34:32 AM GMT
  • nellastevnellastev Posts: 10
    edited 2007-05-04 03:36
    Thanks everyone this is very helpful. I tried everything but at least now I know it is inherent to the BS2 and I can deal with that!
Sign In or Register to comment.