Shop OBEX P1 Docs P2 Docs Learn Events
Two buttons muti functions — Parallax Forums

Two buttons muti functions

bennettdanbennettdan Posts: 614
edited 2007-07-06 16:43 in General Discussion
Hello,
···· I want to be able to have two buttons one up and one down and have it output to only four different outputs. Would this work?

Position···· Var·· byte

Main:
· If ButtonUp = 1 then
Position = Position max 4
Position = Position + 1
Elseif ButtonDown = 1 then
Position = Position min 1
Position = Position - 1
endif
If Position = 1 then
outputA = 1
elseif Position = 2 then
outputB = 1
elseif Position = 3 then
outputC = 1
elseif Position = 4 then
outputD = 1
endif
· GOTO Main

Comments

  • BeanBean Posts: 8,129
    edited 2007-07-05 13:21
    I think you want something like:

    position     Var   byte
     
    Start:
      position = 1
      OutputA = 1
    Main:
      IF ButtonUp = 1 THEN
        position = position + 1 
        position = position MAX 4
        ' Wait for button to be released
        PAUSE 50
        DO
        LOOP UNTIL ButtonUp = 0
      ELSEIF ButtonDown = 1 THEN
        position = position - 1
        position = position MIN 1
        ' Wait for button to be released
        PAUSE 50
        DO
        LOOP UNTIL ButtonDown = 0
      ENDIF
      IF position = 1 THEN
        OutputA = 1
        OutputB = 0
        OutputC = 0
        OutputD = 0
      ELSEIF position = 2 THEN
        OutputA = 0
        OutputB = 1
        OutputC = 0
        OutputD = 0
      ELSEIF position = 3 THEN
        OutputA = 0
        OutputB = 0
        OutputC = 1
        OutputD = 0
      ELSEIF position = 4 THEN
        OutputA = 0
        OutputB = 0
        OutputC = 0
        OutputD = 1
      ENDIF
      GOTO Main
    
    

    If the outputs are sequencial on the same port you could just use the shift operator instead of having to set each on individually.

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • JonnyMacJonnyMac Posts: 8,945
    edited 2007-07-05 14:21
    If you keep things zero-indexed (0 to 3 instead of 1 to 4) you can also do this:

    Check_Buttons:
      IF BtnUp = 1 THEN
        INC pos
      ELSEIF BtnDn = 1 THEN
        pos = pos + 3
      ENDIF
      pos.2 = 0
    



    In this case the pos.2 = 0 does the same thing as pos = pos // 4 -- without the division.
  • bennettdanbennettdan Posts: 614
    edited 2007-07-05 19:14
    Thanks Bean I didnt think about turning off the other outputs.

    JonnyMac That is a little less code and it should work I cant test it right now my SX blitz is not working after my Three year old assitant technician spilt her Cola on it..lol but the SX48 protoboard survived..
  • JonnyMacJonnyMac Posts: 8,945
    edited 2007-07-05 19:21
    You should still debounce your buttons as Bean demonstrated in his code. And if you go with the zero index and keep your outputs in a contiguous group (e.g., RB.0 - RB.3) you could do something like this:

    RB = RB & $F0
    tmpB1 = 1 << pos
    RB = RB | tmpB1
    



    The first line clears your outputs; the second sets the correct bit of a temporary variable; the final line sets that bit on the port.
  • bennettdanbennettdan Posts: 614
    edited 2007-07-06 03:32
    Thanks again JonnyMac
    Your code steps the 1 of tmpB1 through the pos variable that way their would only be one output on at a time right?
  • JonnyMacJonnyMac Posts: 8,945
    edited 2007-07-06 06:06
    Right. If you don't have an SX-Key that you can run in Debug mode, hook up some LEDs and try it. Experimenting is an important part of the learning process.
  • bennettdanbennettdan Posts: 614
    edited 2007-07-06 15:38
    JonnyMac,
    I have an extra prototype board and I plan to solder a ribbon cable header to the SX48 proto board and then run the wires to the prototype board so I can hook up some leds to tes with.
    Thanks again
  • JonnyMacJonnyMac Posts: 8,945
    edited 2007-07-06 15:55
    I am forever suggesting to those interested in the SX to save their pennies and buy the Parallax Professional Development Board. I use it all day, every day, and am never wanting for the ability to create something with the SX. If I do need to use the SX48/52 I plug an old SX Con Carne module into the PDB breadboard and I'm set. I know it costs a couple dollars, but I can tell you from experience that the time and hassle saved by having it handy is worth every penny you spend on it.
  • bennettdanbennettdan Posts: 614
    edited 2007-07-06 16:43
    What do you mean by the SX Con Carne module I do not know what that is? Is that some type of TQPF to Dip module?
Sign In or Register to comment.