Shop OBEX P1 Docs P2 Docs Learn Events
Need PBASIC pro to help me in this program!! — Parallax Forums

Need PBASIC pro to help me in this program!!

CreativeCreative Posts: 11
edited 2005-11-16 03:31 in BASIC Stamp
My program lights up 9 LEDs in a "pseudo-random". I had this problem, the LEDs light up eg. lets say 4 times, lights up LED 1,8,2,9. So the user now had to input according to the LEDs lighted up (LED 1,8,2,9) using 9 push-button switch ( they are assigned to pin0-pin8 (IN0 - IN8 )). The problem is i couldnt press the switch that are previously lighted up generated by "pseudo-random" which is LED 1,8,2,9. How can i solve this problem? Thanks in advance for the help.

' {$STAMP BS2p}
' {$PBASIC 2.5}

x = LED from 1 - 9

light:
HIGH x
PAUSE 1000
LOW x
DEBUG CLS, "press switch", CR

userloop:
IF (IN0|IN1|IN2|IN3|IN4|IN5|IN6|IN7|IN8 = 0) THEN userloop ' wait till 1 is pressed
ukey = 0
GOTO checkloop
END

checkloop:
GOSUB buzzer
IF (IN0 = 1 ) THEN ukey = ukey + 1
IF (IN1 = 1 ) THEN ukey = ukey + 2
IF (IN2 = 1 ) THEN ukey = ukey + 3
IF (IN3 = 1 ) THEN ukey = ukey + 4
IF (IN4 = 1 ) THEN ukey = ukey + 5
IF (IN5 = 1 ) THEN ukey = ukey + 6
IF (IN6 = 1 ) THEN ukey = ukey + 7
IF (IN7 = 1 ) THEN ukey = ukey + 8
IF (IN8 = 1 ) THEN ukey = ukey + 9
DEBUG ? ukey
RETURN
END

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-10 03:10
    I don't understand what you're trying to do, but I can simplify the code you have.· Here's how you can scan the inputs:

    Main:
    · DO
    ··· btnIns = INS & %01FF
    · LOOP UNTIL (btnIns > 0)

    Now you can scan the captured inputs like this:

    · uKey = 0
    · FOR idx = 0 T0 8
    ····uKey = uKey + (btnIns.LOWBIT(idx) * (idx + 1))
    · NEXT

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • CreativeCreative Posts: 11
    edited 2005-11-10 03:33
    thnk for ur reply but i sitll not quite sure abt the code u gave me .
    anyway i will repharse my question.

    firstly i m making a program on lighting 4 LEDs in random sequence and after which i will store this 4 sequence in an array and i have connected push button switch for the user to key in the same sequence where my program will check whether is the user input the sequence correct. everytime the user press the switch, the led corrosponding to the switch will light up. (i attach an immage of my connection)

    i have sucessfully make 4 led to blink in random sequence, storing them in an array, but now i m stuck at the user input. firstly after the LEDs light up, eg. 3 , 3 , 2 , 1 (3rd led lightup, 3rd led lightup, 2led lightup, 1st led lightup) the program will wait for the user to key. therefore i try keying the same sequence(3,3,2,1), the program cant detect that i have key them in(i have set a debug window for it) and the led corroesponding did not light up as well. then i press reset and try key in the wrong sequence. for eg. i key 4 instead of 3 and led 4 lighted up as well as show error msg of keying the wrong key.

    inshort my program can detect the key i pressed wrong instead of the one i pressed correct.
  • CreativeCreative Posts: 11
    edited 2005-11-16 02:51
    hi mr jon..

    you had simplify my program into :

    Main:
    DO
    btnIns = INS & %01FF
    LOOP UNTIL (btnIns > 0)

    Now you can scan the captured inputs like this:

    uKey = 0
    FOR idx = 0 T0 8
    uKey = uKey + (btnIns.LOWBIT(idx) * (idx + 1))
    NEXT

    But i had queries about

    1) btnIns = INS & %01FF.

    2) ukey = ukey + (btnIns.LOWBIT(idx) * (idx + 1)).

    Thanks in advance for your reply!!
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-11-16 03:31
    Creative,

    Have you cracked open the manual or help file?· You should ... it's worth a read.· I'll give you quick answers here but this stuff is serious and you'll want to read the "good stuff."

    1) btnIns = INS & $01FF

    This makes a copy of the input pins (INS) and then clears the upper seven by applying a mask.· I notice a typo in my example that followed into yours ... it should be $01FF which is the same as %0000000111111111, albeit more tidy.· In the end, the variable called btnIns will end up with the status of pins 0 - 8 (all others are forced to zero by the mask).

    2) ukey = ukey + (btnIns.LOWBIT(idx) * (idx + 1))

    This is a little tricky, but what it does is add the button value to "ukey" for each of the pressed buttons -- the same as your big IF-THEN statement.· The LOWBIT modifier loops through each of the bits of btnIns and if the bit is set (button was pressed), it adds that value.· Notice that the loop is zero-indexed, but you values start at 1, hence the "idx + 1" section.

    This is not simple stuff for a beginner -- you'll need to spend a bit of time to understand it and let it sink in.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
Sign In or Register to comment.