Interactive targets, novice, advice wanted
woda
Posts: 6
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
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
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
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...
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
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
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
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?
Here is a draft of a possible code. It's not tested, I have no BS1.
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
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.
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.
Here is what my code and display look like:
Any thoughts on why its not working or why mask= 00000010?
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: 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: 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
So, I don't know how to react when you tell me something works and I get this:
I also tried AND instead of the symbol &, but I get the same error.
Your explanation for how mask works, makes sense. Thank you.
So we need to do the '&' before the IF. Here is a code that gives no syntax error: Andy
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.
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.
This should expand a short button press to a longer puls at the transistor, but it's in no way tested.
Andy