Shop OBEX P1 Docs P2 Docs Learn Events
Getting "BUTTON" to work — Parallax Forums

Getting "BUTTON" to work

ghost13ghost13 Posts: 133
edited 2007-06-14 14:30 in BASIC Stamp
My BS1 system should beep once at startup and then wait until a button is pressed. Then, it should continuously beep.

Right now, with the below code, the piezo just turns on (the switch is off - it should only turn on when the switch if first turned on):
Main:
  HIGH 3
  PAUSE 250
  LOW 3
  goin = 0
  GOTO Try


Try:
  FOR reps = 1 TO 0
    HIGH CS                              ' Deactivate the ADC to begin.
    GOSUB Convert
    grnd = AD*1
    GOSUB Convert                        ' Get data from ADC.
    prev= AD
    BUTTON Btn, 4, 200, 20, btnWrk, 1, Again
  NEXT




Again:
  HIGH 3





I also tried changing this line:
BUTTON Btn, 4, 200, 20, btnWrk, 1, Again


to this line:
BUTTON Btn, 4, 200, 20, btnWrk, 0, Again



The result is the same [noparse]:([/noparse]

Help would be appreciated!!! Thanks!!

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-12 13:04
    ghost,

    FOR reps = 1 TO 0? Where is your Convert subroutine? It seems there is code missing...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ghost13ghost13 Posts: 133
    edited 2007-06-12 15:17
    FOR reps 1 TO 0 is supposed to be an infinite loop.

    Is this wrong?

    Should I just call GOTO Try instead?

    Thanks!
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-06-12 15:44
    ghost13 -

    It may be in infinte loop in some variants of BASIC, but not in PBASIC. Take a look at the LOOP command, or use the GOTO as you suggested.

    Regards,

    Bruce Bates
  • ghost13ghost13 Posts: 133
    edited 2007-06-12 18:47
    Okay. I changed that code. The new version is below. Now, when I test it, it beeps at startup (good), remains silent (good), and when the switch is pressed, goes on for about half a second and then turns off (bad). Then, every time the button is pressed again, it goes back on for half a second (bad).

    I want it to only activate one time when the button is first pressed, and then not concern itself with the button again.

    Here's the code:
    SYMBOL Btn = 0
    SYMBOL btnWrk = B4
    
    
    'the main part of the program
    Main:
      HIGH 3
      PAUSE 250
      LOW 3
      GOTO Try
    
    
    Try:
      HIGH CS                              ' Deactivate the ADC to begin.
      GOSUB Convert
      grnd = AD*1
      GOSUB Convert                        ' Get data from ADC.
      prev= AD
      BUTTON Btn, 4, 200, 20, btnWrk, 1, Again
      GOTO Try
    
    
    
    
    Again:
      HIGH 3
    




    What's wrong?

    Thanks!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-12 19:38
    Ghost,

    Until you can post the completed code I doubt you’re going to get any better answers. I don’t even see how that code could compile since your Convert subroutine is missing and the code seems to END at the HIGH 3 command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • ghost13ghost13 Posts: 133
    edited 2007-06-12 21:12
    Sorry. here's the full code (attached).

    So... is my BUTTON code correct? I want it to execute Again, which calls itself forever (until apogee is reached). Once button executes once, it should be ignored.

    Thanks!

    Post Edited (ghost13) : 6/13/2007 6:06:08 AM GMT
  • ghost13ghost13 Posts: 133
    edited 2007-06-14 02:09
    Is there anything else you need posted? Or is this all the info you need?

    Thanks!
  • ghost13ghost13 Posts: 133
    edited 2007-06-14 06:50
    Ok... I completely reworked the button code. It uses "Input" I still does not work, however. When +5V goes into PIN4 (the first this happens only, though), I want to go to this code:
    HIGH CS                              ' Deactivate the ADC to begin.
      GOSUB Convert
      grnd = AD
      prev= grnd
      lastRead = prev
    



    Here is the full code:
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    
    'Vars for ADC input
    SYMBOL CS = 0                ' Chip select; 0 = active.
    SYMBOL CLK = 1               ' Clock to ADC; out on rising, in on falling edge.
    SYMBOL DIO_n = 2             ' Pin _number_ of data input/output.
    SYMBOL DIO_p = PIN2          ' Variable_name_ of data input/output.
    SYMBOL ADbits = B1           ' Counter variable for serial bit reception.
    SYMBOL AD = W1               ' 12-bit ADC conversion result.
    SYMBOL sglDif = 1            ' Single-ended, two-channel mode.
    SYMBOL msbf = 1              ' Output 0s after data transfer is complete.
    SYMBOL oddSign = BIT0        ' Program writes channel # to this bit.
    
    'other vars:
    SYMBOL  reps    = B2         ' counter for infinite loop
    SYMBOL  lastRead= W2          ' for check if after apogee
    SYMBOL  grnd= W3         ' base reading everything is based off of
    SYMBOL  prev= W4         ' base reading everything is based off of
    
    
    'for beeping:
    SYMBOL i = B3
    SYMBOL ioByte = B2
    
    SYMBOL Btn = 0
    SYMBOL btnWrk = B4
    SYMBOL pos = B4
    
    SYMBOL G_SWITCH = PIN4
    
    
    'the main part of the program
    LOW 7
    HIGH 3
    PAUSE 500
    LOW 3
    INPUT G_SWITCH
    
    Check:
      IF G_SWITCH=0 THEN Check
    
      HIGH CS                              ' Deactivate the ADC to begin.
      GOSUB Convert
      grnd = AD
      prev= grnd
      lastRead = prev
    
    Again:
        HIGH 3                                  ' infinite loop
        GOSUB Convert
        lastRead = AD
        prev = prev+5
        IF lastRead>prev THEN Pyro ' display if after apogee
        prev = lastRead                                                ' Change input channels.
      GOTO Again                                            ' Endless loop.
    
    
    Pyro:
      HIGH 3
      PAUSE 2000
      LOW 3
      HIGH 7
      PAUSE 3000
      LOW 7
      lastRead=grnd-lastRead
      lastRead=3346-lastRead
    
    Disp:
      GOSUB Long
      GOSUB Medium
      ioByte = lastRead/1000 'get 1000s
      GOSUB Play_Number
      GOSUB Medium
      ioByte = lastRead//1000/100 'get 100s
      GOSUB Play_Number
      GOSUB Medium
      ioByte = lastRead//100/10 'get 10s
      GOSUB Play_Number
      GOSUB Medium
      ioByte = lastRead//10 'get 1s
      GOSUB Play_Number
      GOSUB Medium
      GOTO Disp
    
    Play_Number:
       IF ioByte = 0 THEN Change
       GOSUB PN
       RETURN
    
    PN:
       FOR i = 1 TO ioByte                            'one "beep" per count
          GOSUB Short
       NEXT
       PAUSE 250
       RETURN
    
    
    Change:
      ioByte = 10
      GOSUB PN
      ioByte = 0
      RETURN
    
    
    Short:
      HIGH 3
      PAUSE 300
      LOW 3
      PAUSE 150
      RETURN
    
    
    Medium:
      PAUSE 500
      RETURN
    
    
    Long:
      HIGH 3
      PAUSE 1250
      LOW 3
      PAUSE 500
      RETURN
    
    
    'the conversion subroutine:
    Convert:
      LOW CLK                    ' Low clock--output on rising edge.
      HIGH DIO_n                 ' Switch DIO to output high (start bit).
      LOW CS                     ' Activate the 1298.
      PULSOUT CLK,5              ' Send start bit.
      LET DIO_p = sglDif         ' First setup bit.
      PULSOUT CLK,5              ' Send bit.
      LET DIO_p = oddSign        ' Second setup bit.
      PULSOUT CLK,5              ' Send bit.
      LET DIO_p = msbf           ' Final setup bit.
      PULSOUT CLK,5              ' Send bit.
      INPUT DIO_n                ' Get ready for input from DIO.
      LET AD = 0                 ' Clear old ADC result.
      FOR ADbits = 1 TO 13        ' Get null bit + 12 data bits.
      LET AD = AD*2+DIO_p         ' Shift AD left, add new data bit.
      PULSOUT CLK,5               ' Clock next data bit in.
      NEXT                        ' Get next data bit.
      HIGH CS                     ' Turn off the ADC
      RETURN                      ' Return to program.
    
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-06-14 14:30
    I would recommend breaking this down into something simpler. You should write a test program that sits in a loop until you press the button then goes to your new routine. Use DEBUG and verify that it works. Then you may be able to see how this is working. In your current code the switch is only checked once…If it’s not LOW on entry into the Check routine it’s missed…The loop following it (Again) never checks it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.