Shop OBEX P1 Docs P2 Docs Learn Events
Get button form 74hc165 — Parallax Forums

Get button form 74hc165

SabaiSabai Posts: 27
edited 2009-10-05 16:50 in BASIC Stamp
I am using a shift register and I have problem to with the code determine what button is pressed. It all work and I can see in the debug window that the button is pressed. I am using the example code 24.

My goal will be to do like this
If switches.bit0 = 1 then
Gosub button1
Endif
If switches.bit1 = 1 then
Gosub button2
Endif
If switches.bit2 = 1 then
Gosub button3
Endif

And so on for all 8 buttons.

I have tried this code:
Get_165:
PULSOUT Load, 5 ' load switch inputs
SHIFTIN SerData, Clock, MSBPRE, [noparse][[/noparse]switches]
IF switches.BIT0 = 1 THEN
GOTO test

ENDIF ' shift them in
RETURN

But I can not get it to work

B

Comments

  • dev/nulldev/null Posts: 381
    edited 2009-10-04 19:24
    You should put the IF tests right below where Get_165 is called. If you GOTO within a procedure that's GOSUB'd you'll loose program control.

    Somewhere in your code there's "GOSUB Get_165" (probably in the Main procedure).
    Right below it, put your "IF .. THEN Button" statements.

    Main:
      GOSUB Get_165
      IF switches.bit0 = 1 THEN GOSUB Button1
      IF switches.bit1 = 1 THEN GOSUB Button2
      IF switches.bit2 = 1 THEN GOSUB Button3
      etc....
    
      GOTO Main
    
    Button1:
      ' do something
      RETURN
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • SabaiSabai Posts: 27
    edited 2009-10-04 19:52
    Still not working. Have changed the code to this.

    Main:
    
        GOSUB Get_165
        IF switches.BIT0 = 1 THEN GOSUB Button1                            ' get switch inputs
        DEBUG CRSRXY, 10, 2, BIN8 switches          ' display current status
        PAUSE DelayTime                             ' pad the loop a bit
     GOTO main
    
    
    ' -----[noparse][[/noparse] Subroutines ]-----------------------------------------------------
    
    Get_165:
      PULSOUT Load, 5                               ' load switch inputs
      SHIFTIN SerData, Clock, MSBPRE, [noparse][[/noparse]switches]    ' shift them in
      RETURN
    
      Button1:
            DEBUG CLS,"Result"
            RETURN
    
  • dev/nulldev/null Posts: 381
    edited 2009-10-04 20:07
    Change your debug statements to:
    DEBUG "switches: ", BIN8 switches, CR
    



    and

    DEBUG "Button1", CR
    



    and tell us what you get when you press the button.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Don't worry. Be happy
  • SabaiSabai Posts: 27
    edited 2009-10-05 16:50
    Sorry i mixed the bottons so now its working.

    B
Sign In or Register to comment.