Shop OBEX P1 Docs P2 Docs Learn Events
Button press — Parallax Forums

Button press

Hi everyone,
I am trying to create a mini pinball table with my BS2, I would like to create a code where the servo turns a certain distance when I press a momentary contact switch, then goes back to it's original position when I release. I have tried to program this for about three days now, and I cannot figure it out. For now, we can say the switch is wired to IN0, and the servo is plugged in to 12. I can change the code later if I need to. Can someone help me to code this?
Below is my current code.
Thanks for the help.

counter VAR byte

DO
IF (IN0 = 0) THEN

GOSUB up
ENDIF

IF (IN0 = 1) THEN
GOSUB down
ENDIF

LOOP

up:
FOR counter = 1 TO 25
PULSOUT 12, 850
NEXT
RETURN

down:
FOR counter = 1 TO 25
PULSOUT 12, 650
NEXT
RETURN

END

Comments

  • So, what's happening? The main problem with your code is that you're testing for whether the switch is closed or open and re-testing every time through the loop. First, most switches aren't clean ... They bounce for a sizable fraction of a second. When you push the button, you get a series of irregular pulses ... first on, then off, then on, then off. Eventually the switch settles down into a steady on state. The same thing happens when you let up on the button ... a series of off then on pulses settling eventually into the off state. You handle this using "debouncing". An easy way to handle some bouncing is, when you get an on pulse, PAUSE for maybe 30ms, then test again. If the switch is still on, you probably have a real on state. Similarly, when you get an off pulse, PAUSE for about 30ms and test again. If it's still off, you probably have a real off state.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2016-05-02 15:13
    You're also overdriving the servo. Pulses coming in too fast like that can cause serious twitching and random sporadic results with many analog servos. Every servo PULSOUT should have PAUSE 20 following it, minus code overhead. But literally to make the servo go to one position when a button is pressed and back when release is just a few lines of code.
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    
    DO
      IF IN0 THEN
        PULSOUT 12, 500   ' Move to one extreme
      ELSE
        PULSOUT 12, 1000  ' Move to the other extreme
      ENDIF
      PAUSE 20
    LOOP
    
  • Hi Chris,
    Thanks for the help, but my servos are continuous rotation. so when the switch is pressed it runs one way, and when I let off it runs the other way. I would like it to turn about a quarter turn the stop both ways, which is why I used the counter variable. Any ideas?
  • Hi Mike,
    I get what you are saying, how do I tell it to pause when I get an on or off pulse?
    Thanks!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    CR servos can't move to a specific position. That's what standard servos are for. The only way you could do that with CR servos is if you have encoders and were able to track the position of the servo. Without that the BASIC Stamp has no idea where the servo is to tell it how much to move.
  • Thats what I wanted to use the counter variable for. If I leave out the IF THEN statement, the servo moves to where I want it.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    That won't be consistent. If you use a count and move a CR servo back and forth the same number of counts you will find that it will get further off on the positioning.
  • Okay,
    What standard servo would you recommend?
    It has to be fast and strong enough to hit the pinball.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    A CR servo isn't any more powerful than its unmodified counterpart. So a standard version of whatever servo you're using should work. The main thing is that the CR servo can't be positioned without feedback.
  • ercoerco Posts: 20,257
    Also, servos are VERY slow compared to the solenoids used in real pinball games. You won't get arcade-like performance moving a flipper with any type of servo.
  • SMennSMenn Posts: 16
    edited 2016-05-03 18:32
    Are there any solenoids that work with the bs2 and board of education?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    For the most part any solenoid could be controlled if you have the right power supply and driver circuitry. You can't drive a solenoid directly off an I/O pin though.
  • ercoerco Posts: 20,257
    Per Chris, powerful solenoids require large currents, thus they are basically like a mini-EMPs, so other electronic gremlins await. Ain't life grand?
  • SMennSMenn Posts: 16
    edited 2016-05-04 00:14
    How would you recommend using a solenoid with the BoE?
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    Well, that would depend on the specifications of the solenoid; voltage, current, etc. Then you would choose an appropriate driver.
  • What is a driver? I'm sorry if this is an obvious question, I am relatively inexperienced in this.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2016-05-04 16:57
    It could be anything designed to allow a low-power signal to drive a high-power load. It could be a transistor, MOSFET, Relay, SSR, etc. Depends on the application.

    As erco hinted at, a solenoid is an inductive load, so depending on the driver additional protection may be required.
  • Hi Publison,
    Yes that link was helpful. What voltage solenoid would be powerful enough to knock the pinball all the way up the board?
  • You don't say how long the board is. I would say a 6 or 12 volt would be sufficient.

    Start with 6 volt first.
  • The board is roughly 3 feet long, on a 5 or 6 degree angle.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    I should think the mass of the metal ball, the incline, distance to travel and throw distance of the solenoid would all be more important factors, which are unknown at this point. You may have to do some empirical testing.
  • What is empirical testing?
  • SMenn wrote: »
    What is empirical testing?

    Trying things to see if they work. Worst case.
  • Hi,
    i found this for running solenoids with arduino. Would it work with a bs2 on the BoE as well?
    Thanks
    Sam
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    It should, yes, assuming the Solenoid you use is within the ratings of that transistor. Looks like they specify up to 4A.
Sign In or Register to comment.