What's a Microcontroller Digital Input-Pushbuttons

microcontrollerusermicrocontrolleruser Posts: 549
edited November 1 in BASIC Stamp Vote Up0Vote Down
Sorry.

Had to back off the Stepper Motor plan for now.

Was getting in over my head.

Okay. Will work on Button Press.
JUNIOR ENGINEER
«1

Comments

  • 39 Comments sorted by Date Added Votes

  • Here's the source code from Stamp Editor BS 1 programs.
    ' BUTTON.BS1
    ' Connect an active-low circuit to pin P0 of the BS1. When you press the
    ' button, the DEBUG screen will display an asterisk (*). The program, as
    ' shown below, will print an asterisk at the first button press, then
    ' delay approximately one second (200 x 5 ms PAUSE) before auto-repeating
    ' at a rate of approximately 100 ms (5 x 20 ms).  Feel free to modify the
    ' program to see the effects of your changes on the way BUTTON responds.
    
    ' {$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
    
    JUNIOR ENGINEER
  • If you’re going to jump in to using the BUTTON statement, remember that it has to be executed repeatedly and functions a little like a GOTO that takes care of debouncing the switch and doing repeated button presses as it’s looping about. If you want to keep things simple, just test for a button press with an IF statement. You can avoid bounces by wiring a small capacitor in parallel with your switch. Look here for details.

  • Mike

    Have a heart! :smile:

    Debounce routines are where I was up to with PIC Assembler tutorial.

    Nasty. Nasty. Nasty.

    JUNIOR ENGINEER

  • Mike

    What does this program do?

    Is it only checking once for button press?

    I am going to look in What's a Microntroller.

    Just for a little more information. Not a lot! :smile:
    JUNIOR ENGINEER

  • That was a bad idea.

    I am going to just stick with this example.

    How do I wire up button as 'active low' button?

    It's a parallax breadboard button.
    JUNIOR ENGINEER
  • JonnyMacJonnyMac Posts: 5,858
    edited October 31 Vote Up0Vote Down
    The BUTTON instruction is not very useful unless run in a loop where you want to use the auto-repeat feature. Better to debounce the input using a loop like this:
    Main:
      PAUSE 5
      btnWork = btnWork + 5 * Btn
      IF btnWork < 25 THEN Main
    
    Important note for this to work: Btn must be an alias for a PINx variable. This allows the line after the PAUSE to accumulate the debounced timing if the button is pressed, or reset it to zero if the button input is low. Remember: In the BS1, there is no operator precedence; expressions are evaluated left to right. In this case, five is added to timer and then multipled by the state of the button pin. If the button is pressed, Btn evaluates as 1 and the timer accumulates; if the button is released then Btn evaluates as 0 and the timer variable is cleared.

    I was at LA Comic Con yesterday and saw lots of Cosplays with boring LEDs. Even the BS1 can spice things up with a simple bit of code. Here's a real-world program that I use as a demo for the Prop-1 controller with a trainer board (which has 6 LEDs and a button).
    Main:
      PAUSE 5                                       ' debounce delay
      timer = timer + 5 * Trigger                   ' update trigger timer
      IF timer < 100 THEN Main                      ' button debounced?
    
    Zig_LEDs:
      FOR ledPin = 0 TO 4                           ' forward through LEDs
        HIGH ledPin                                 ' LED on
        PAUSE 75                                    ' hold
        LOW ledPin                                  ' LED off
        IF Trigger = IsOff THEN Main                ' button released?
      NEXT
    
    Zag_LEDs:
      FOR ledPin = 5 TO 1 STEP -1                   ' go backward
        HIGH ledPin
        PAUSE 75
        LOW ledPin
        IF Trigger = IsOff THEN Main
      NEXT
    
      GOTO Main
    
    The complete listing is attached so you don't have to re-type it. One of the "advanced" things you'll notice is that the loops use different pins -- this prevents the bounce delay on the ends of the strip of LEDs.

    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • JonnyMacJonnyMac Posts: 5,858
    edited October 31 Vote Up0Vote Down
    Theory is fun, but I'm a real-world, practical guy. Here's a picture of the Prop-1 controller -- with a trainer attached -- that is running the Zig-Zag code.

    prop-1_with_trainer.jpg
    800 x 600 - 559K
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • That was a bad idea.

    I am going to just stick with this example.

    How do I wire up button as 'active low' button?

    It's a parallax breadboard button.

    One button contact to ground, the other to the I/O pin, and a pullup resistor (~10K) to Vcc.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.

  • Kwinn

    Thank you.

    Found Prop example. Got out of it because there was too much going on in it.

    Your explanation will work fine.

    Keeping it simple.
    JUNIOR ENGINEER

  • Jon

    Thanks!

    Let me get this going 'as-is'.

    Then look at your ideas.
    JUNIOR ENGINEER
  • JonnyMacJonnyMac Posts: 5,858
    edited October 31 Vote Up0Vote Down
    Then look at your ideas.
    They are not ideas, they're commercially-proven concepts that I've developed since 1994 when I bought my first BS1. Since then, I have written hundreds of BS1 programs for an astonishing variety of applications; things from simple movie prop control for Hollywood FX engineers to an interesting program that is used to activate the landing lights on a private airstrip when the owner flies in at night.

    EFX-TEK (I co-created) has sold a couple thousand Prop-1 (industrialized BS1) controllers across the world from everyone to home Halloween decorators to major amusement parks that you've probably visited. In fact, there is a Christmas tree on display at a very famous SoCal amusement park that has 80 Prop-1 controllers creating a faux candle effects. My customers aren't looking for ideas, they're looking for solutions that work in their applications.

    You can continue to post blurbs from the (20 year old) help file, but those are not proving that you're learning anything, nor are you providing any new information for other forum members.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.

  • Jon

    Okay.

    Will finish this experiment tomorrow.
    JUNIOR ENGINEER

  • Changing over to Basic Stamp 2 What's a Microcontroller button experiment.

    Need step by step through this.

    Going to go look for a Homework board.
    JUNIOR ENGINEER
  • BUTTON in the BS2 is no more useful than in the BS1.

    Here's my standard BS2 template for the EFX-TEK Prop-2 controller -- note that it does debouncing in a loop and allows the user to specify active-high or active-low trigger input.
    ' =========================================================================
    '
    '   File......
    '   Purpose...
    '   Author....
    '   E-mail....
    '   Started...
    '   Updated...
    '
    '   {$STAMP BS2}
    '   {$PBASIC 2.5}
    '
    ' =========================================================================
    
    
    ' -----[ Program Description ]---------------------------------------------
    
    
    ' -----[ Revision History ]------------------------------------------------
    
    
    ' -----[ I/O Definitions ]-------------------------------------------------
    
    Sio             PIN     15                      ' no ULN, SETUP = UP
    Trigger         PIN     14                      ' SETUP = DN
    
    
    ' -----[ Constants ]-------------------------------------------------------
    
    IsOn            CON     1                       ' for active-high in/out
    IsOff           CON     0
    
    TrOn            CON     1
    TrOff           CON     0
    
    Yes             CON     1
    No              CON     0
    
    
    T1200           CON     813                     ' serial constants
    T2400           CON     396
    T4800           CON     188
    T9600           CON     84
    T19K2           CON     32
    T38K4           CON     6
    
    SevenBit        CON     $2000
    Inverted        CON     $4000
    Open            CON     $8000
    
    Baud            CON     Open | T38K4            ' for EFX-TEK accessories
    
    
    ' -----[ Variables ]-------------------------------------------------------
    
    timer           VAR     Word
    
    
    ' -----[ Initialization ]--------------------------------------------------
    
    Reset:
      OUTH = %00000000 : OUTL = %00000000           ' clear all
      DIRH = %00000000 : DIRL = %00000000           ' set outputs
    
    
    ' -----[ Program Code ]----------------------------------------------------
    
    Main:
      timer = 0
      DO WHILE (timer < 100)                        ' debounce = 100ms
        PAUSE 5
        IF (trigger = TrOff) THEN
          GOTO Main
        ELSE
          timer = timer + 5
        ENDIF
      LOOP
    
      ' program code here
    
      GOTO Reset
    
    ' -----[ Subroutines ]-----------------------------------------------------
    
    
    ' -------------------------------------------------------------------------
    
    
    ' -----[ User Data ]-------------------------------------------------------
    
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.

  • Thank you Jon

    Still working my way up to that point.

    Right now.

    Will the Parallax Serial to USB adapter work with the Basic Stamp Homework boards?

    Parallax pushes it for the Stamp 1 Project board but not the Homework boards.

    Wondered if there was a snag there.
    JUNIOR ENGINEER
  • Mike GreenMike Green Posts: 22,577
    edited October 31 Vote Up0Vote Down
    There's a Homework Board with a built-in USB adapter, so the serial version of the Homework Board is not much used now. The USB to Serial adapter works fine with the older board. There's no USB update for the Stamp 1 Project Board.

    EFX-TEK (JonnyMac's company) does have a USB to BS1 adapter that takes the place of a USB to Serial Adapter plus a Serial to BS1 Adapter.
  • microcontrollerusermicrocontrolleruser Posts: 549
    edited October 31 Vote Up0Vote Down
    Thanks Mike

    Here's another tidbit.

    Light on Homework board is a 'programming activity' light.

    Light on BOE is 'board powered' light.

    Have time to notice these things now. At least for a little while. :smile:
    JUNIOR ENGINEER

  • Have two Serial Homework boards.

    Okay. Parallax Serial to USB works with them.

    Light on board did light up while program downloaded and then went off.

    Will continue this later.
    JUNIOR ENGINEER
  • JonnyMacJonnyMac Posts: 5,858
    edited October 31 Vote Up0Vote Down
    EFX-TEK (JonnyMac's company) does have a USB to BS1 adapter that takes the place of a USB to Serial Adapter plus a Serial to BS1 Adapter.
    You can use any USB-to-TTL adapter if you're careful with the connections. Here's an original Parallax USB2SER adapter (5V) connected to a BS1 programming header. Note that it's upside-down for correct pin allignment (>> is ground).

    usb2ser_prop-1.jpg

    And here's a generic USB-to-TTL adapter connected with jumper wires.

    usbttl_prop-1.jpg

    Yes, both work. In fact, the USB2SER thing is what caused me to create the USB-BS1 Serial Adapter for EFX-TEK.
    800 x 600 - 322K
    800 x 600 - 575K
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.

  • Thanks Jon

    Ran a 'Print to Terminal' program and it worked.

    USB to Serial only is for communication. It does not provide USB power.

    So. Terminal program with Identify reported COM port 4 but no stamp.

    Had to put battery in Homework board.

    Yes. That looks like a good USB to TTL board.
    JUNIOR ENGINEER

  • Fixed the title.

    Will build circuit tomorrow.

    'Test Pushbutton'.

    Get used to that 4 lead pushbutton.

    JUNIOR ENGINEER
  • @microcontrolleruser when are you doing the Blockly tour with the FLiP?

  • Ken

    Let me get back to you about that.

    We're on a roll here.

    Will build 'test button' circuit here shortly.

    Actually caught on to the part about 'pull down' and 'pull up' resistor.

    It's the 10K one on the 'opposite leg' of main circuit.

    Funny way to put it but that's the gist of it.
    JUNIOR ENGINEER
  • JonnyMacJonnyMac Posts: 5,858
    edited November 1 Vote Up0Vote Down
    IO pins have a very high impedance which makes them sensitive to external electrical influence unless placed in a known state. A pull-down is a resistor connected between the pin and ground; this pulls the input down to ground when there is no input signal. It also provides a load for the signal when present. A pull-up is a resistor that pulls the input to Vcc.

    (Fixed typo -- thanks, Dave)

    A pull-down is used when the input signal is active-high.
    A pull-up is used when the input signal is active-low.

    In breadboard experiments we tend to use active-high inputs because it's easier to think of having a "high" signal as on.

    Why use an input with a pull-up? Well, in the industrial world there are a lot of sensors and devices with "open-collector" outputs. This means that the output looks disconnected until active; when active the sensor provides a connection to ground which pulls the input down. You just have to remember that when reading the pin "1" is off, and "0" is on.

    Another area where pull-ups are used is the I2C bus between the processor and EEPROM. The pull-up creates the "1" state on the bus and either end can connect to ground to provide the "0" state.
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • JonnyMac wrote: »
    ...
    A pull-up is used when the input signal is active-high.
    A pull-up is used when the input signal is active-low.
    ...

    Did you mean pull-up on both statements?
  • microcontrollerusermicrocontrolleruser Posts: 549
    edited November 1 Vote Up0Vote Down
    Jon and Dave

    Will put some of the lesson circuits together today and post pic's.

    One problem with these older boards is they run the serial port out the top.

    That was for serial and parallel ports coming out the back of desktops.

    Now with tower's under the desktop and laptops you want the USB connector on the bottom of the board.

    I have to run the cable around to the top to keep board 'right side up'.

    I have to put a block of steel on the cable to keep the heavy cable from dragging little board off the table.

    Waah!
    JUNIOR ENGINEER
  • microcontrollerusermicrocontrolleruser Posts: 549
    edited November 1 Vote Up0Vote Down
    Building 'Test Pushbutton' LED circuit.

    Found a pushbutton and mounted it on breadboard.

    JUNIOR ENGINEER

  • Here's pushbutton test circuit.

    It works.

    Next is 'Turn LED off with pushbutton'.

    It does not look like 'pull-down' or 'pull-up' circuit.

    Looks like a shunt one.
    JUNIOR ENGINEER
  • JonnyMacJonnyMac Posts: 5,858
    edited November 1 Vote Up0Vote Down
    One problem with these older boards is they run the serial port out the top.
    Parallax sells a nice USB-to-Serial adapter that works well. I always keep one with me for older devices.

    -- https://www.parallax.com/product/28030

    28030a_0.png?itok=0f9Usoiu
    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
  • DaveJenson wrote: »
    JonnyMac wrote: »
    ...
    A pull-up is used when the input signal is active-high.
    A pull-up is used when the input signal is active-low.
    ...

    Did you mean pull-up on both statements?
    Nope -- was a typo. Thanks for the catch.

    Jon McPhalen
    Hollywood, CA
    It's Jon or JonnyMac -- please do not call me Jonny.
Sign In or Register to comment.