Shop OBEX P1 Docs P2 Docs Learn Events
Stamp 1 Button Loop Code — Parallax Forums

Stamp 1 Button Loop Code


This is from Stamp Editor Help/Language/By Category.
' {$STAMP BS1}
' {$PBASIC 1.0}

SYMBOL  Btn             = 0

SYMBOL  btnWrk          = B2


Main:
  ' Try changing the Delay value (255) in BUTTON to see the effect of
  ' its modes: 0 = no delay; 1-254 = varying delays before auto-repeat;
  ' 255 = no auto-repeat (only one action per button press)
  '
  ' The BUTTON instruction will cause the program to branch to
  ' No_Press unless P0 = 0

  PAUSE 5
  BUTTON Btn, 0, 200, 20, btnWrk, 0, No_Press
  DEBUG "*"

No_Press:
  GOTO Main  

Comments


  • Doing some prototyping.

    Anybody know some improvements we can do to this?

    Give us a little time to get it running first.
  • Please stop posting only information from manuals or application notes. It's one thing to post an example or description when that explains or otherwise informs a discussion of something you are actually doing, but what you are doing here is not a reasonable use of the forum's resources.

  • 'something you are actually doing'

    This is what we are doing.Thank you.
    The program to control the hardware would use the following steps:
    1. Wait for SW1 to close.
    2. Turn on LED1.
    3. Wait for SW1 to open.
    4. Turn off LED1.
    5. Repeat.

    For one thing it is program design for bigger and better things.

    For another it is to test a new board with.A Parallax product if that matters.It should.

  • 'not a reasonable use of the forum's resources.'

    If you are an undercover moderator you should make yourself known.

    Southern Californian's really, really really do not like attempts to deceive us.

  • Here's the program in Stamp Editor.



    PDB%20Button%20Press.png
    1366 x 768 - 184K

  • Found some problems with it.

    It does not follow the program design we want.

    Will think about changing it or scrapping it and using SX.
  • 'not a reasonable use of the forum's resources.'

    If you are an undercover moderator you should make yourself known.

    Southern Californian's really, really really do not like attempts to deceive us.

    Surely you wrote this in good humor, but let's clarify for you...

    Mike Green is not working undercover. Mike is a very highly regarded member of our community, and he has shared a significant amount of his expertise and time with you here, trying to guide you and answer your questions.

    Especially given the amount of time given to helping you, I don't think it unreasonable that Mike asks you to limit your posts to actual questions or your own code and experiments. Rather than posting excerpts from user guides and sometimes posting a running commentary on your thoughts, which understandably takes away from the time available for Mike and others to follow your progress and help with your questions.


    (Whilst writing, I should also add that using these forums as a type of personal blogging site is not appropriate, and not allowed according to the forum rules. Perhaps you could read the rules over and see for yourself. I'm sure that refreshing the current rules in your mind could help you become more concise and improve your posting style, whilst also helping you reach a wider audience of contributors to your experiments and discoveries).

    http://forums.parallax.com/discussion/134682/forum-rules-and-guidelines#latest
  • 'not a reasonable use of the forum's resources.'

    If you are an undercover moderator you should make yourself known.

    Southern Californian's really, really really do not like attempts to deceive us.

    Be careful on how you address forum members that have a steller history with these forums. Mike's observation is correct in the eyes of this Public Moderator.
  • Mike GreenMike Green Posts: 23,101
    edited 2018-06-22 16:10
    "This is what we are doing" ...

    "What's a Microcontroller?" can be downloaded from Parallax (for free like most of Parallax's educational material). The code examples are written for the BS2, but many are easily modified for the BS1. In particular, look at Chapter 3 Activity #3 starting on page 70.
    ' {$STAMP BS1}
    ' {$PBASIC 1.0}
    
    SYMBOL  button = PIN6      ' Note: different I/O pins used
    SYMBOL  LED    = 7     ' Pin # used here rather than PIN7
    
      LOW LED          ' LED pin set to output mode, turned off
    
    Main:
      IF button = 0 THEN Main     ' Wait for button to go high
      HIGH LED          ' LED turned on
      PAUSE 50          ' Wait for button bouncing to finish
    
    Wait:
      IF button = 1 THEN Wait      ' Wait for button to go low
      LOW LED          ' LED turned off
      PAUSE 50         ' Wait for button bouncing to finish
    
      GOTO Main          ' Repeat
    
    There's a good Wikipedia article on contact bounce if you're interested.

    Note: The BUTTON statement shown in one of your earlier posts can also be used to handle a pushbutton, but it's a complicated statement, hard to understand, takes a bit of program memory, and requires the use of a byte variable of which there's a limited amount. It does handle debouncing for you. It's rarely used in "real life".

  • VonSzarvas

    Message received

    Publison

    Message received

  • Thank you Mike

    The button command 'covers up' the code we are working with.

    So Thanks but this will not help us with prototyping flow control with assembler.

    See the 5 step flow chart above.

    So.Prototyping with Basic Stamp did not work in this case.

    Will keep hacking away on using SX assembler.

    'good Wikipedia article on contact bounce'

    The Googlium tutorial went on for a whole chapter about that. Debounce routines and formulas, programs.

    I think our current book side steps that somehow.

    Will let you know.
  • Mike GreenMike Green Posts: 23,101
    edited 2018-06-22 18:12
    The BS1 program above is a practical implementation of the five program steps you've listed. You're given as well a link to a reference book that discusses how to hook up a switch and LED to a Basic Stamp for just the sort of exercise you described. Why then does prototyping with a Basic Stamp not work? Why would using SX assembler be better particularly since you don't seem to be experienced in it? The Basic Stamp was originally developed to make programming with microcontrollers easier than using their assembly language and instruction set directly. If your goal is simply to learn some microcontroller assembly language, then you could certainly have chosen an easier to learn instruction set and assembly language. Why start with trying to get a pre-programmed PIC16C56A to run with some interface pieces missing and ignore the tool that you get when you hook it up correctly?

    If you seriously would like just to learn about the SX assembler and instruction set, by all means have at it, but keep your postings to the Microcontrollers forum and remember that this is not a blog ... it's a place to ask specific questions about Parallax products. If you have negative comments about what this or that book or posting doesn't say, either ask for help or keep the comments to yourself. If you don't like the answer you get, try to be more specific. Maybe we didn't understand what you wanted.
  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2018-07-10 04:04
    Thank you for all the help Mike

    This is no longer productive.

    If it is okay with you I will move on.

    Good luck!



  • I noticed on the forum there are people at all different levels some are beginners, novices, genius, brain, surgeons, etc. I would certainly encourage any beginner or novice even if there question seems trivial to the genius. When a beginner figures out stamp basic or spin the more they will like it and use it.
  • JonnyMacJonnyMac Posts: 8,924
    edited 2018-07-09 19:16
    The BUTTON command is not really very useful -- and I know, because I've been programming the BS1 since 1994. EFX-TEK makes a small SBC called the Prop-1 (came out before the Propeller did) that uses the BS1. It gets used in a array of light industrial applications, including a large amusement park in SoCal that you've probably been to.

    Most of the time we just need basic debouncing but no auto repeat with rate. Here's how I do it with the BS1:
    SYMBOL  Trigger         = PIN7
    
    SYMBOL  debounce        = B2
    
    Reset:
      debounce = 0
    
    Main:
      PAUSE 5
      debounce = debounce + 5 * Trigger
      IF debounce < 25 THEN Main
    
    You need an input pin (Trigger) and a byte variable (debounce). Adjust the PAUSE and loop count for desired debounce timing; I use 25ms for buttons.
    Note that this works because there is no operator precedence in PBASIC 1. This being the case, debounce is either incremented by 5 (input is high) or cleared to zero (input is low).
    The button command 'covers up' the code we are working with.
    In point of fact, PBASIC and every other compiled language does that. The LONG DEAD SX/B Compiler (which I helped with while at Parallax) was unique in its approach -- mostly for simplicity, partly for education. Scott Edwards wrote a book about PIC Assembly (I have copy #1) that shows you how to re-create many PBASIC-like instructions in PIC assembly. You can probably find an electronic copy of it somewhere.
  • JonnyMacJonnyMac Posts: 8,924
    edited 2018-07-09 19:10
    For grins, I knocked together a PASM version of the code above. Note that it counts backward for simplicity.
    check_button            mov     debounce, #25                   ' set debounce timer
                            mov     t1, ms1tix                      ' set t1 for 1ms
                            add     t1, cnt                         ' sync with system counter
    :loop                   waitcnt t1, ms1tix                      ' wait 1ms, reload
                            test    triggermask, ina        wc      ' check input
            if_nc           jmp     #check_button                   ' if no input, reset
                            djnz    debounce, #:loop                ' decrement and check
    
    By the way... if you switch to the Propeller, you can use PropBASIC which was written by the same guy (Bean) who wrote the SX/B compiler. This will let you see how he translates high-level statements to PASM.
  • PublisonPublison Posts: 12,366
    edited 2018-07-09 22:18
    JonnyMac wrote: »
    For grins, I knocked together a PASM version of the code above. Note that it counts backward for simplicity.
    check_button            mov     debounce, #25                   ' set debounce timer
                            mov     t1, ms1tix                      ' set t1 for 1ms
                            add     t1, cnt                         ' sync with system counter
    :loop                   waitcnt t1, ms1tix                      ' wait 1ms, reload
                            test    triggermask, ina        wc      ' check input
            if_nc           jmp     #check_button                   ' if no input, reset
                            djnz    debounce, #:loop                ' decrement and check
    
    By the way... if you switch to the Propeller, you can use PropBASIC which was written by the same guy (Bean) who wrote the SX/B compiler. This will let you see how he translates high-level statements to PASM.

    Thanks Jon for all your little gems you bestow one the forums.



  • Johnny Mac

    ' Scott Edwards wrote a book about PIC Assembly (I have copy #1) that shows you how to re-create many PBASIC-like instructions in PIC assembly. You can probably find an electronic copy of it somewhere.'

    Thank you!

    Is it me or are the SX instructions backwards from PIC?

    You code MOV W,F and it is executed 'Move contents of File Register to W'?

    Got that from a book you may have written.

    SX Key guide I think.
  • Is it me or are the SX instructions backwards from PIC?
    I don't know, nor do I care because the SX is -- for all intents and purposes -- dead. When it was alive, it was very close to (but faster than) low-level PICs at the time (which is why the ridiculous lawyers fouled things up).

    I have never written a book on PIC programming.

    I just stumbled on this -- it might help you: http://pictutorials.com/index.htm. It has a tutorial on the instruction set.

  • Thanks Johnny Mac

    Bottom line.We've moved up from Basic Stamps to PIC's.

    Which is logical because Stamps are based on PIC's.

    So.There we have it.

  • JonnyMacJonnyMac Posts: 8,924
    edited 2018-07-11 00:04
    Bottom line. We've moved up from Basic Stamps to PIC's.
    Well, hopefully you'll find a forum as helpful for PICmicros as this one is for Parallax products.

    PS: There is no "h" in my name.

  • Jonny Mac

    Thanks!
  • The only way you get a button down signal from a normally open push button is if the user pressed it down. You can do the button action immediately and then wait for the contacts to settle down before checking for button up.

    A good exercise is to write a routine that will reliably toggle an led controlled by a normally open button and still respond to rapid button presses.

    The settling time for different buttons varies significantly. Some aviation toggle switches rattle around for ages before they settle down.

    Sandy
  • microcontrollerusermicrocontrolleruser Posts: 1,194
    edited 2018-08-03 19:50
    Thanks!

    Let me look at Stamp boards and see which way the pull up resistors go.That's a start.

    Parallax writes their program samples for active high.

    I do remember an active low routine though.

    Might be in 'What's a...'

    You're talking about a 'dead man' button.

    You have to hold it down to keep it open.

    We have just been using regular buttons.



Sign In or Register to comment.