Shop OBEX P1 Docs P2 Docs Learn Events
A question about multiple pushbuttons — Parallax Forums

A question about multiple pushbuttons

BriceBrice Posts: 2
edited 2011-04-14 09:29 in BASIC Stamp
Hello, I'm pretty new to all of this, but I've read some in the basic stamp syntax manual and the whats a microcontroller book, but I guess I'm not getting it. I have attached the code that I'm working with so far. It seems that there must be a better way of doing this. I just want to be able to press a pushbutton and have the BSII make a pin high and when I release the button and press it again it makes the same pin low and repeat continuously. And I want to be able to do this for multiple pushbuttons. Basically just turning a momentary pushbutton into a latching or constant switch (3 times or more). from there, I can control an led or use a transistor to operate a relay etc. Any help is appreciated. Thanks!

Comments

  • Mike GMike G Posts: 2,702
    edited 2011-04-10 14:38
    I didn't test this... it depends how you have pins 13, 14, and 15 wired.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    Init:
      ' Set pin group A to input low
      DIRA = %0000
      OUTA = %0000
    
      'Set pin group D to output low
      DIRD = %1111
      OUTD = %0000
    
    Main:
    
      IF IN0 = 1 THEN  TOGGLE 13
      IF IN1 = 1 THEN  TOGGLE 14
      IF IN2 = 1 THEN  TOGGLE 15
    
    GOTO Main
    
  • BriceBrice Posts: 2
    edited 2011-04-10 15:40
    Thank you for your reply. I tried the code you sent, something still seems to be wrong. I'm attaching a rough diagram of how I have it wired. Thanks again!
    1024 x 1325 - 67K
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-04-10 15:54
    When simple tasks have to be done in parallel, like you have here, multiple pushbuttons to multiple outputs, a state machine is a time-honored technique. The following program has 3 internal states stored in variables x0, x1 and xx. Variables x0 and x1 are simply the current and previous states of the input pins, and each bit corresponds to one of the pushbuttons. Variable xx detects changes that occur when any (or any combination) of those pushbuttons are pressed and then released. But you see, all that takes is the bitwise XOR and AND operators, "^" and "&" on the Stamp. Then another bitwise "^" is used to toggle the states of the corresponding output bits. All that in parallel within a loop that runs over and over quickly. It is possible now to add more instructions to the loop, which will slow it down some, but the logic of the state machine will continue to operate.

    x0 VAR nib
    x1 VAR nib
    xx VAR nib
    x0 = INA      ' initial state
    DIRD = %1110   '   pins p13, p14, p15 are outputs   
    DO
      x1 = INA     ' read the inputs
      xx = x0 ^ x1 & x1   ' look for transition 0->1 (release button to high state)
      x0 = x1      ' update the previous state variable
      OUTD = (OUTD ^ xx) << 1  ' toggle outputs, move up to p13..p15, p12 is low.
    LOOP
    
  • ZootZoot Posts: 2,227
    edited 2011-04-10 17:15
    A slight variation to Tracy's code that will "preserve" the output state of pin 12 so that other areas of the program may use it...
    x0 VAR nib
    x1 VAR nib
    xx VAR nib
    x0 = INA      ' initial state
    DO
      x1 = INA     ' read the inputs
      xx = x0 ^ x1 & x1   ' look for transition 0->1 (release button to high state)
      x0 = x1      ' update the previous state variable
      OUTD = (OUTD ^ xx) << 1  | OUTD.BIT0   ' toggle outputs, move up to p13..p15, keep p12 where it was
       TOGGLE 12 ' flash p12 rapidly regardless of what buttons are doing to leds
    
    
      ' remember if the pin assignments OUT = the pin assignments IN (in groups of 4) then you would not
    ' need the shift above (e.g., if the leds were on pins 12,13,14 and the buttons were on pins 0,1,2
    ' though you might still want to mask out pins not used by the LEDs so they are available to other
    ' parts of the program, e.g.
    '  OUTD = (OUTD ^ xx) & %0111  | OUTD.BIT3   ' toggle leds on p12,13,14 leaving p15 alone
    
    
    LOOP
    
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2011-04-10 20:37
    Good point Zoot.

    Also I forgot to make the pins outputs using the command,
    DIRD = %1110
    but edited that into the program in my previous post. That leaves the pin p12 as an input.
  • jc3IIIjc3III Posts: 21
    edited 2011-04-14 09:23
    i know how to use the pushbutton, but I want to use the pc keyboard instead of the pushbutton. For example if I hit the "L" button then it will activate a pin. just like the pushbutton does.
  • ZootZoot Posts: 2,227
    edited 2011-04-14 09:29
    jjc3III -- use DEBUGIN. Also, you may want to start your own thread for your question.
Sign In or Register to comment.