Shop OBEX P1 Docs P2 Docs Learn Events
Interactive targets, novice, advice wanted — Parallax Forums

Interactive targets, novice, advice wanted

wodawoda Posts: 6
edited 2014-02-14 21:51 in BASIC Stamp
Good afternoon,

I am new to the world of do it yourself electronics and only have a little experience programming from high school, so any and all advice is appreciated and helpfull

For this project I have purchased a BS1 Starter kit, 8 buttons (Radio Shack Tact Switch), and 8 LEDs.

The objective is to have 8 targets each with an LED and a button. When the program starts Target 1's LED is illuminated, when button 1 is pressed, the LED shuts off, and one of the other 7 targets lights up (randomly).

The intended application is for a paintball shooting range, to practice my eye hand coordination.

I have a coworker/mentor who has been using Basic stamps for years, who is assisting me with this (my first basic stamp) project. I just want to see if anyone here has a method that he and I have not thought of.

Best regards,
Woda

Comments

  • BeanBean Posts: 8,129
    edited 2014-01-30 06:44
    Sounds like a cool project.

    I assume your paint ball is what will trigger the button ?

    It's going to be a little complicated because the BS1 only has 8 I/O pins and you want to connect 8 LEDs and 8 buttons. But a button and a LED can share one I/O pin, but the code is more complex.

    Bean
  • GadgetmanGadgetman Posts: 2,436
    edited 2014-01-31 04:30
    You could use a 74??138 to select which LED to light up. That will only use 3 IO-pins.

    The 138 is a '3 to 8 decoder', so outputting a 3bit binary value to it will activate one of the 8 outputs.

    That leaves 5 IO-pins that can be used to read the targets...
    And in fact, you don't need more than one.

    Connect the output from the switch together with the output of the 138 to an AND gate, then send every output from the AND to a big OR gate.

    Or something like that...
  • BeanBean Posts: 8,129
    edited 2014-01-31 05:30
    Gadgetman, That is a good idea. Something like this should work.

    attachment.php?attachmentid=106615&d=1391175005

    I've only shown two of the eight, but the rest would connect the same way.

    Pins 0 to 2 will select which LED is on, Pin 3 will sense the switch (it will read high until the switch with the on LED is pressed, then it will read low).

    If you could do with only 7 targets you wouldn't need the 74LS138 chip. Just use pins 0 to 6 to drive the LEDs low, and pin 7 to read the switch.

    Bean
    1024 x 472 - 37K
  • AribaAriba Posts: 2,690
    edited 2014-01-31 09:11
    Why not this simple circuit (8 times, once per pin):
    LedButtonPin.gif


    The LED will light always if the TactSwitch is pressed, but you can also let it light if you set the pin low.
    To get the state of the pin you need to switch the pin to input for a short moment. The LED+R acts then as pullup.
    Switch only between input and low (=LED off and on), do not set it to high, otherwise the Switch makes ashort at the pin.

    Andy
    203 x 159 - 698B
  • BeanBean Posts: 8,129
    edited 2014-01-31 11:41
    Andy,

    I didn't suggest that because I don't know if a BS1 will be fast enough to be able to detect a paint ball hitting the button.

    Bean
  • wodawoda Posts: 6
    edited 2014-02-10 13:24
    Guys I am the farthest thing from an electrical engineer so I dont really understand most of what you said.... but its appreciated because I am working on understanding it!

    First lets start with what I have so far:

    Main:
    RANDOM variable1
    variable2 = variable1 // 8
    DEBUG Variable2

    BRANCH Variable2, (case_0, case_1, case_2, case_3, case_4, case_5, case_6, case_7)

    Case_0:
    HIGH 0
    BUTTON Btn, 0, 255, 20, btnwrk, 0, No_Press
    LOW 0
    GOTO Main

    Case_1:
    HIGH 1
    BUTTON Btn, 1, 255, 20, btnwrk, 0, No_Press
    LOW 1
    GOTO Main

    The program has the other 5 cases but since they are nearly identical I left them out.

    I wired up just the LEDs with resistors on a bread board to be sure that part of the program works. To do this I just commented out the BUTTON comands.

    My issue now is I don't know how to wire it.

    Is what I have so far going to work if I just wire it like Andy suggested?
  • AribaAriba Posts: 2,690
    edited 2014-02-10 16:15
    I think you can make it more simple with a single loop for all possible LEDs. BS1 has no << or DCD operator so I used a LOOKUP table to make a bitmask for the pin. This mask has only one bit set, Bit0 for button 0, Bit1 for button 1 as so on. If you AND it with the state at the port input then you can check if the masked button is pressed.

    Here is a draft of a possible code. It's not tested, I have no BS1.
    SYMBOL states    = B0 
    SYMBOL mask      = B1
    SYMBOL num       = B2
    SYMBOL variable1 = W2
    
    Main:
      RANDOM variable1          'choose a LED randomly
      num = variable1 // 8
      LOOKUP num,(1,2,4,8,16,32,64,128),mask  'make bitmask
      DEBUG num,%mask
    
      LOW num                   'LED on
    
    WaitButtn:
      INPUT num                 'switch to input
      states = PINS             'read pin states
      LOW num                   'and let LED light again
      IF states & mask > 0 THEN WaitButtn   'not pressed
      DEBUG "pressed"
    
      INPUT num                 'LED off
      PAUSE 500                 'wait a moment with all LEDS off
      goto Main
    '
    

    The question is: How fast is the WaitButtn loop. For Buttons pressed by hand it should not be a problem, but when a paintball presses the button it may be not detected because the pin goes LOW only for a very short time. I think this is what Bean is worried about.

    Andy
  • ercoerco Posts: 20,257
    edited 2014-02-10 16:55
    Bean wrote: »
    Andy,

    I didn't suggest that because I don't know if a BS1 will be fast enough to be able to detect a paint ball hitting the button.

    Bean

    It's all in the mechanicals. I see a big lightweight target paddle pressing a switch (NC or NO, no matter). When hit, the target gets shoved backwards against gravity or spring return, releasing the switch momentarily, but for a few hundred milliseconds. A BS1 is certainly up to the task, coupled to Andy's circuit in post #5.
  • wodawoda Posts: 6
    edited 2014-02-11 06:13
    Andy - your program just needed a little tweak (If states > 0 and mask > 0 Then WaitButton)

    But I don't know if I have the wiring correct, because when I hit the switch the LED doesnt go out and the screen doesnt say "pressed". Actually the LED gets a little brighter.
  • wodawoda Posts: 6
    edited 2014-02-11 12:21
    Ok so I made a few modifications to the code and using a bread board wired four LEDs and Switches like this:
    attachment.php?attachmentid=106625&d=1391188140&thumb=1
    Here is what my code and display look like:
    screen shot.png


    Any thoughts on why its not working or why mask= 00000010?
    1024 x 363 - 31K
    829 x 778 - 36K
  • AribaAriba Posts: 2,690
    edited 2014-02-11 22:41
    woda

    Your tweaks can not work. mask is always > 0, so the If states > 0 and mask > 0 Then WaitButton will be the same as:
    If states > 0 Then WaitButton, and this is always true until you press all 8 buttons at the same time.

    The way I had it should work: IF states & mask > 0
    But you also need to make the lookup table (1,2,4,8) and not 0,1,2,3. The values in the table are 2^num.

    How this works:
    If the random number is 2 as in your picture, the lookup picks 4 from the table, so the mask is 4, which is %00000100 binary.
    So only bit2 of the mask is set. If you now make a bitwise AND (operator &) with the input states, then all bits that are zero in the mask will be zero in the result, only the bit2 can be 1 if you read a 1 at pin 2. If pin 2 reads zero then the whole result is zero:
    PINS:     %11111111    'not pressed
    MASK:     %00000100
    & result: %00000100
    
    PINS:     %11111011    'pressed
    MASK:     %00000100
    & result: %00000000
    
    So if you press button2 you get zero otherwise not zero, this is checked with the IF states & mask > 0.
    I know this is a bit complicated, it would be much simpler if a PIN-Read command would exist for the BS1 that can read a single pin according the pin-number. But I don't see such a command so I read the whole input port (all 8 bits) and mask the bit for the pin of interest.

    I would insert some debug code for a first test, so that you can watch the pin states and the & result:
    SYMBOL states    = B0 
    SYMBOL mask      = B1
    SYMBOL num       = B2
    SYMBOL variable1 = W2
    
    Main:
      RANDOM variable1          'choose a LED randomly
      num = variable1 // 4
      LOOKUP num,(1,2,4,8),mask  'make bitmask
      DEBUG num,%mask
    
      LOW num                   'LED on
    
    WaitButtn:
      INPUT num                 'switch to input
      states = PINS             'read pin states
      LOW num                   'and let LED light again
       DEBUG %states            '4 lines for debug, remove them later
       states = states & mask
       DEBUG %states
       PAUSE 500
      IF states & mask <> 0 THEN WaitButtn   'not pressed
      DEBUG "pressed"
    
      INPUT num                 'LED off
      PAUSE 500                 'wait a moment with all LEDS off
      goto Main
    '
    
    This scans the pins/buttons only twice a second and shows the states. You should see how the bits change if you press a button. It may also help if you disable the random number for debugging, so that you get always the same number, for example insert the line:
    num = 2
    before the lookup line. Don't forget to remove that later when it works.

    Andy
  • wodawoda Posts: 6
    edited 2014-02-12 11:42
    Andy - as I have said multiple times I am a rookie compared to most rookies when it comes to electronics and programming.

    So, I don't know how to react when you tell me something works and I get this:
    screen shot.jpg


    I also tried AND instead of the symbol &, but I get the same error.

    Your explanation for how mask works, makes sense. Thank you.
    1024 x 363 - 45K
  • AribaAriba Posts: 2,690
    edited 2014-02-12 13:49
    Okay I see that with the BS1 you can't have an expression in the IF command.
    So we need to do the '&' before the IF. Here is a code that gives no syntax error:
    Main:
      RANDOM variable1          'choose a LED randomly
      num = variable1 // 4
      LOOKUP num,(1,2,4,8),mask  'make bitmask
      DEBUG num,%mask
    
      LOW num                   'LED on
    
    WaitButtn:
      INPUT num                 'switch to input
      states = PINS             'read pin states
      LOW num                   'and let LED light again
        DEBUG %states           'indented lines for debug, remove them later
      states = states & mask
        DEBUG %states
        PAUSE 500
      IF states <> 0 THEN WaitButtn   'not pressed
      DEBUG "pressed"
    
      INPUT num                 'LED off
      PAUSE 500                 'wait a moment with all LEDS off
      GOTO Main
    '
    
    Andy
  • wodawoda Posts: 6
    edited 2014-02-14 08:04
    Andy - It works!

    Now all I have to do is build the targets.

    I might need to play around with spring mounted buttons to increase the amount of time the button is being depressed. When I was messing around with it on the bread board sometimes it wouldnt register a quick press.

    Thanks for all the help. I will try to get a video up once its built.
  • ercoerco Posts: 20,257
    edited 2014-02-14 09:46
    woda wrote: »
    I might need to play around with spring mounted buttons to increase the amount of time the button is being depressed. When I was messing around with it on the bread board sometimes it wouldnt register a quick press.

    Yep, the devil's in the details, mechanical-wise. Make sure to remove (or comment out) all of your DEBUG statements for your final installation. They slow your code execution down and may be contributing to your missed button presses.
  • AribaAriba Posts: 2,690
    edited 2014-02-14 21:51
    If you don't find a mechanical solution for a long enough "pressed" contact, you cand also try an electronical solution like that:
    LedButtonPin2.GIF

    This should expand a short button press to a longer puls at the transistor, but it's in no way tested.

    Andy
    203 x 159 - 2K
Sign In or Register to comment.