Shop OBEX P1 Docs P2 Docs Learn Events
Problems with the Button command — Parallax Forums

Problems with the Button command

MrdavidjenkinsMrdavidjenkins Posts: 49
edited 2005-10-24 22:25 in BASIC Stamp
Good Afternoon,

I have a problem with a relay closing and giving me multiple counts.· The Relay sounds good and the line between the relay and the stamp is about 3ft.· I am trying to use a BUTTON command and am having no luck.· I am getting usually 10 button presses when I expect to get only 1.· (The code below is based upon the code in the help files.)

' {$STAMP BS2sx}
' {$PBASIC 2.5}

btn···· PIN 5
mycount VAR Word
btnwrk· VAR Byte

btnwrk = 0

DO
······· PAUSE 5
······· BUTTON BTN, 1, 255, 255, btnwrk, 1, readlight
ret:··· PAUSE· 5
'·· Works Better if below is inserted, but still not properly
'······ PAUSE 500
LOOP


READLIGHT:
·· mycount = mycount + 1
· DEBUG "My Count is ", DEC· mycount, CR
GOTO Ret


AS I understand it, the syntax I have above has·the delay set to 255, so only there is no repeat, but is debounced.· The rate is set at 255, but this should be irrelevent because there is no delay.

Can you help me with this Counting problem?

Thanks
Dave
«1

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-10 19:42
    Dave,

    ·· What exactly happens when you press the button?· How is your button connected?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-10 20:03
    Thanks Chris,

    I am using the circuit similar to the one shown on page 135 of BASIC STAMP SYNTAX AND REF MANUAL 2.1 - Active High. That is, I have a 220 ohm Resistor and a 10 k ohm resistor in the active-high scheme. In place of the PB switch, I have a Radio Shack Relay that is triggered by a Allen Bradley retroflective sensor.

    When I run the code above, I get incremental mycount output from 1 to 7 on first momentary interuption . Then I get 8 to 15 on the second momentary interuption. Then I get 16 to 20 on the third interuption. I have only interupted the beam 3 times, but I get 20 mycounts back from the computer. If I put my hand over the sensor for , say, 5 seconds, I get a mycount of 20 to 50.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-10 23:08
    I've never been a fan of BUTTON -- here's another way to solve your problem that's simpler to implement and gives you better control over debouncing.· This method assumes you won't be getting a contact closure more than 10 times per second. And you should note that relay contacts are the worst for contact bounce.



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    IncCount······· PIN···· 5

    myCount········ VAR···· Word


    Reset:
    · myCount = 0

    Main:
    · DO
    ··· IF (IncCount = 1) THEN
    ····· myCount = myCount + 1
    ····· DEBUG HOME, "Count is ", DEC myCount
    ···· ·PAUSE 100
    ··· ENDIF
    · LOOP UNTIL (myCount > 10)
    · GOTO Reset

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-10 23:14
    Cool - I will try it. and get back to you.
    THanks
    Dave
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-10 23:37
    Jon,

    I tried your idea and really appreciate it - but it was not successful. The pause 100 reads 1 thru 6, then 7 thru 11 then.. . And increasing the pause length improves it substantially. If I change your pause statement from 100 to 2000, it does work fine, but the app requires about 40 strokes per minute.

    I am beginning to suspect that the hardware is snafu. If so, do you have a test that I could try? (No oscillascope)

    Would you suggest using a transistor or set of transistors? The Allen bradley needs 15 volts, so I have a separate transformer for that. The output from the allen bradley is 15 volts as well. Ideally, I could build a box that would I could swap out - keeping the circuits as separated as much as possible to protect both the stamp and the allen bradley (which is pretty expensive).

    Appreciate your thoughts.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-10 23:48
    Dave,

    ·· The test for the code Jon supplied would be to replace the Relay contacts with a switch and try that.· Bear in mind that an active high input would require a 10K pull-down resistor to ground (Vss).· Without it the input may actually oscillate, give you readings like you describe.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-11 00:07
    If you're saying that you're taking 15 volts to a mechanical relay and then into the Stamp -- STOP now.· Get an opto-transistor and contact bounce will be gone forever.· I've attached a circuit that should work.· I made the output active low, so you'll have to adjust the input test.

    Now... you could probably get away with a connection between the AB device and the Stamp using a 22K resistor.·

    AB ---> 22K ---> Stamp

    This would limit the current flow into the Stamp to a safe level.· We do this with RS-232 stuff all the time.· In that case you'd have an active-high input.· Of course you'd need to connect Vss on both sides as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    794 x 317 - 24K
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-10-11 06:44
    Gents -

    All of your attempts and efforts to remove this proposed "jitter" or bounce from this signalling system have been most laudable. One thing is missing so far, which may be of extreme importance, however. To date there has been no determination WHERE the actual signal repetition is occuring and what it looks like. It might be wise to grab an oscilliscope and check, serially, every output and every input in the signal path until the repetition is found. At that juncture, you'll then know how to properly deal with it.

    As a general rule, signal conditioning, to be most effective, needs to be done as close to any source of "interference" as is humanly possible. If for some reason the photo-electric sensor is spitting out a signal that looks like it came from output pin of a 555 timer, then all the de-bouncing in the world two or three stages down the road, will be quite ineffective in turning it back into a one-shot pulse.

    Just an outside pair of eyes chiming in smile.gif

    Regards,

    Bruce Bates

    Post Edited (Bruce Bates) : 10/11/2005 7:11:28 AM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-11 14:10
    Bruce Bates said...(trimmed)
    To date there has been no determination WHERE the actual signal repetition is occuring and what it looks like. It might be wise to grab an oscilliscope and check, serially, every output and every input in the signal path until the repetition is found. At that juncture, you'll then know how to properly deal with it.
    Bruce,

    ·· Mr. Jenkins already pointed out that he had no scope.· To that end, a workaround was suggested which might reveal the source of the jitter, or at least rule out one device when it was suggested he replace the currently connected device with a push-button.· Not very hi-tech, but will at least narrow the problem down significantly to the Stamp Input.· Jon also suggested an alternative which would potentially isolate the bounce/jitter problem.· these are currently being tested.· I guess my point is, sometimes you need lo-tech.· =)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com


    Post Edited (Chris Savage (Parallax)) : 10/11/2005 2:22:40 PM GMT
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-15 01:57
    Good Morning,

    I replaced my mechanical radioshack relay with a vn012 optical relay (thanks Jon W) and am still having problems. I used the code that Jon W suggested (thanks again), but it did not work well and I had to modify it

    ' {$STAMP BS2sx}
    ' {$PBASIC 2.5}

    IncCount PIN 5

    myCount VAR Word


    Reset:
    myCount = 0

    Main:
    DO
    IF (IncCount = 1) THEN
    PAUSE 100
    IF (INccount = 0) THEN
    myCount = myCount + 1
    DEBUG HOME, "Count is ", DEC3 myCount
    PAUSE 100
    ELSE
    PAUSE 100
    ENDIF
    ELSE
    PAUSE 100
    ENDIF
    LOOP UNTIL (myCount > 10)
    GOTO Reset

    I still get a bounce off of this and get a couple of false positive. Anyone have any neat thoughts? I will tryChris's button later this weekend. And I will post the circuit I built a little later.


    In other news, I bought one of the USB osciliscopes from Parallax and it arrived today. I just opened it and will play with it, but I need to learn how to use the unit now and need to figure out how to apply this to problem.


    Thanks, everyone.
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-15 22:25
    Hello,
    I tried the suggestion that Chris Savage suggested and put in a push button on the relay.· This was cool - I was able to see the bounces that I had in the past.· This tells me that the problem is very probably not the relay, but something bouncy inside the circuitry.·

    I reviewed the circuit, drew it up and attached it.·

    I am looking at the circuitry, but nothing appears to be the problem.·· I have now have a oscilloscope, but I don't know how to use it.· Can anyone suggest how to use it with this problem, or give me a plan of attack.

    Thanks
    Dave
    748 x 738 - 50K
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-15 23:09
    You should download our "Understanding Signals" book -- it will show you how to use the 'scope -- that way any suggestions will make sense.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-16 11:15
    Jon - I bought the whole package and am going thru it now. I am about 2 chapters into the book now. I have not figured out how to use it for this problem, but I will keep reading the book. I tried to put a green sensor on pin 5 and put the second on ground to watch what happens, but no signal appeared. I will keep reading the manual.
    Thanks everyone.
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-22 19:20
    Jon and Chris and others

    Still having problems with the Allen Bradley Sensor.· Two problems:

    Oscilloscope:· When I hook up the Parallax Ocilloscope to the circuit, the debug mode shuts down and I cannot test the circuits.· Weird.· Attached Diagram shows the circuit and the hookup.

    1) What Am I messing up Here?

    2)· I really liked the idea that Jon put together and the ideas behind it.· I would like to use an opto transistor like he shows in the diagram to eliminate all bounce, but my work with the 3908 and the 4n26 has not worked.· Jon, could I have a little more description of the circuit and the·use of a 22·K resistor?·

    I apologize for my ignorance.·
    662 x 762 - 80K
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-22 20:32
    What is the actual output of the AB device?· Is it a swtich closure (which I'd guess since you're showing a battery in series with it an the relay)?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • PJAllenPJAllen Banned Posts: 5,065
    edited 2005-10-23 02:22
    Mrdj,

    How about BUTTON BTN,1,255,20,BtnWrk,1,readlight?

    How about adding a·1K resistor between the "high side" of the switch contacts and VDD?
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-23 13:43
    Good Morning,
    Thanks everyone·for your help.· I am attaching the Allen Bradley sheets for the 42KL-U2LB-F4 unit that I am using.· I put the relay in as because I thought it would help isolate the Basic Stamp from any high voltage, but I am extremely interested in doing whatever you suggest and will do what every you suggest.· (Details would be extremely welcome and very helpful.)··

    Thanks again.
    Dave
    728 x 724 - 442K
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-23 14:50
    The output says it's NPN/PNP which means you have the opportunity to switch between ground or the positive side of the supply.· I think you can connect the thing directly to your BASIC Stamp, here's how:

    1)·Put a 10K pull-up (not down, as in your original circuit) on your BASIC Stamp input pin
    2) Connect BASIC Stamp Vss to Ground on the sensor -- should be pin 3 (see pdf link below)
    3) Connect BASIC Stamp input pin to NPN output on the sensor·-- should be pin 2

    Run this little test program:

    Main:
    · DEBUG Home, BIN1 IN0
    · GOTO Main

    Don't get crazy if it shows you a 0 for a condition where you want a 1 -- PBASIC can invert this in code:

    Main:
    · DEBUG Home, BIN1 ~IN0
    · GOTO Main

    Using an active-low input is easier when you have different device voltages; ground is common, so switching to it is not a problem.· And as I just showed you, PBASIC will handle making an active-low input look active-high.

    This is the document I used for reference: http://www.ab.com/catalogs/sensors/catalognumberpdf/ch1/1-39_1-53.pdf


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 10/23/2005 4:17:51 PM GMT
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-23 16:11
    Oh So Cool. Thanks. I will do this later today. Thanks, Jon,

    Best
    Dave
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-23 16:18
    Make sure you use the BIN1 (or DEC) modifier in the test program -- I just noticed I had left that out (see above).

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-23 21:47
    Good Afternoon,

    Here is a drawing of the circuit that I would like to put together.· Does this show the circuit you described?· The "WHAT IS A MICROCONTROLLER" book, on page 79, the circuit shows using a Vdd with a 10 k resistor, but I am not sure how to include it, unless I do what I show in yellow on the attached?



    Thanks!

    Dave
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-23 21:51
    Here is the jpeg file - the other was a visio file that you cannot read.



    Thanks



    Dae
    684 x 769 - 95K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-23 22:10
    David,

    ·· I just looked up the part on the Allen-Bradley website and per your diagram I would recommend having the 10K pull-up on the white wire as it exits the device.· You should only have a 220 ohm resistor between the white wire and the Stamp I/O pin though.· When the device is not active you will get a HIGH/1 on the I/O pin.· When the device is triggered you will see a LOW/0 on the I/O pin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-23 22:11
    Change the 10K inline to 220 ohms (like you had before). You must have the 10K pull-up; connect to Vdd of the BASIC Stamp.

    Addition: Your wiring connections are backward -- please read your AB docs carefully; pin 3 is ground (connect to the Stamp's Vss pin), pin 2 is the switched connection (connect to your input).· This information is in the graphic you provided.··Remember, this is a transistor (even though they simplify the diagram as a switch)·and it must be correctly biased to conduct.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax

    Post Edited (Jon Williams (Parallax)) : 10/23/2005 10:41:01 PM GMT
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-23 22:18
    David,

    ·· Yes, sorry if I wasn't clear...I was just made aware of your separate power supply...I didn't see that connection in your schematic.· Always try to include all data in the schematic when you include one.· =)· You don't want to pull-up to the other supply, otherwise you could burn a Stamp I/O pin.· Physically though the device should be the equivalent of a transistor, so pulling it up to the Stamp Vdd should work just as any standard transistor, so long as the ground (Vss) is common to the device.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-23 23:34
    I think I have it now.· I have modified the drawing to reflect the changes.· I did not understand changing the location of the 10 K ohm resistor though,· Chris.··Next time I will include things like the separate power supply.

    Is this a reasonable design?··

    (This is for work.· I will send you a picture of the $275 custom box I have made for it if you want.)

    Dave
    616 x 816 - 70K
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-10-23 23:36
    David,

    ·· I think you have it now.· That should work.· Good luck, and sure, post your cabinet picture.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-24 00:56
    Mrdavidjenkins said...

    <snip>· I did not understand changing the location of the 10 K ohm resistor though,· Chris.··</snip>
    What we didn't want you to do was use the AB power supply for the pull-up that connects to the Stamp -- this would have run a very high voltage into the Stamp pin and likely caused damage.· By moving the pull-up near the Stamp you'll remember to use its Vdd for the pull-up.

    We're all hoping this finally works -- you've been at it a little while now.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • MrdavidjenkinsMrdavidjenkins Posts: 49
    edited 2005-10-24 22:19
    Guys,

    Initially, it looks really really good. What a relief.

    Seriously, Thanks a lot.

    I took the liberty of sending you some almonds. Should be there end of week or early next week. Hope Chris and Jon work in same general area.

    Really appreciate your help

    Dave
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-24 22:20
    Did you send mine to Dallas?· If not, I'll never see 'em -- those guys in Tech Support can eat! tongue.gif

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