Shop OBEX P1 Docs P2 Docs Learn Events
Switching Input Problem BS2 — Parallax Forums

Switching Input Problem BS2

wvmusicwvmusic Posts: 4
edited 2011-04-06 09:09 in BASIC Stamp
I've been using a BS2 as a controller system to switch direction on a small DC motor. In the past I was using two switches for each direction. Ive incorporated a couple MOSFETs to help with the switching, but now I have a new issue.

When I send a signal to one of the input pins, it latches into place. I put a line of code to give me the input status of my switches and sure enough, once given +5, it latches into place until given a ground. It has not always been this way, wondering if I may have @!%#-ed something up on my BOE or on my BS2.

If anyone has any ideas on why my INPUT latches and won't go to 0 unless I verify ground I would appreciate it. As I said, in the past, I was able to simply not toggle the switch and it would go to 0. I'm not using a latching command or loop. This same principle worked just fine a month ago, and though Im using MOSFETs, even when I remove them and go back to the original setup, the switches are latching.

I know it is messy, but here is my code for controlling a bi-directional motor based on switched inputs and the height status of two PING sensors. It's a mock lift system that accounts for descent position and the user must verify certain characteristics of the system before completing the descent. Thanks for the help.









' {$STAMP BS2}

' {$PBASIC 2.5}
' {$PORT COM7}

Ping PIN 14
Ping2 PIN 15



Trigger CON 5 ' trigger pulse = 10 uS
Scale CON $200 ' raw x 2.00 = uS
RawToIn CON 889 ' 1 / 73.746 (with **)
RawToCm CON 2257 ' 1 / 29.034 (with **)
IsHigh CON 1 ' for PULSOUT
IsLow CON 0
height1 VAR Bit
height2 VAR Bit
safety1 VAR Bit
safety2 VAR Bit
rawDist VAR Word ' raw measurement
inches VAR Word
cm VAR Word
Trigger2 CON 5 ' trigger pulse = 10 uS
rawDist2 VAR Word ' raw measurement
inches2 VAR Word
cm2 VAR Word
safety1 = 0
safety2 = 0
height1 = 0
height2 = 0
lower VAR Bit
lower2 VAR Bit
compare VAR Bit
on1 VAR Bit
on2 VAR Bit
up1 VAR Bit
down1 VAR Bit

Reset:
DEBUG CLS,
"Parallax PING))) Sonar", CR, ' setup report screen
"======================", CR,
CR,
"Time (uS)..... ", CR,
"Inches........ ", CR,
"Centimeters... ", CR,
" ", CR,
"Time 2 (uS) ", CR,
"Inches2....... ", CR,
"Centimeters2.. ", CR,
" ", CR,
"Tilt Status... ", CR,
"Switch Status. ", CR,
"CONTROL STOP. . ", CR,
"up. ", CR,
"down. . ", CR


Main:
DO
GOSUB Get_Sonar
GOSUB Get_Sonar2 ' get sensor value
inches = rawDist ** RawToIn ' convert to inches
cm = rawDist ** RawToCm ' convert to centimeters
inches2 = rawDist2 ** RawToIn ' convert to inches
cm2 = rawDist2 ** RawToCm ' convert to centimeters
GOSUB switches

DEBUG CRSRXY, 15, 3, ' update report screen
DEC rawDist, CLREOL, CRSRXY, 15, 4,
DEC inches, CLREOL, CRSRXY, 15, 5,
DEC cm, CLREOL,CRSRXY,15,7,
DEC rawDist2, CLREOL,CRSRXY,15,8,
DEC inches2, CLREOL,CRSRXY,15,9,
DEC cm2, CLREOL
DEBUG CRSRXY, 15, 11,
BIN safety1, CLREOL , CRSRXY, 15, 12,
BIN safety2, CLREOL, CRSRXY, 15, 13,
BIN height1, CLREOL, CRSRXY, 15, 14,
BIN up1, CLREOL, CRSRXY, 15, 15,
BIN down1, CLREOL



IF(IN3 = 1) THEN
up1 = 1
ENDIF
IF(IN3 = 0) THEN
up1 = 0
ENDIF

IF(IN2 = 1) THEN
down1 = 1
ENDIF
IF(IN2 = 0) THEN
down1 = 0
ENDIF


PAUSE 10
LOOP
END


switches:

IF(inches>inches2) THEN
height2 = inches-inches2
IF(height2<= 1) THEN
compare = 1
ELSE
compare = 0
ENDIF
ELSEIF(inches2>inches) THEN
height2 = inches2 - inches
IF(height2 <= 1) THEN
compare =1
ELSE
compare = 0
ENDIF
ENDIF

IF(inches <2) AND (compare = 1) THEN
height1 = 0
safety2 = 0
ENDIF

IF(inches > 5) AND (inches<8) AND (compare = 1) AND (safety2 = 0) THEN
height1 = 1
ENDIF

IF (inches< 6) AND (compare = 1) AND (height1 = 1) AND (safety1 = 1) AND (safety2 = 1) THEN
height1 = 0
ENDIF

IF(inches>=8) AND (safety2 = 1)AND (compare = 1) OR (safety1 = 0) THEN
safety2 = 0
ENDIF

IF(safety2 = 1) AND (inches<8) AND (compare = 1) THEN
lower2 = 1
ELSE
lower2 = 0
ENDIF

IF(height1 = 1) AND (inches < 6) AND (compare = 1) AND (safety1 = 1) AND (IN1 = 1) OR (lower2 = 1) THEN
safety2 = 1
ENDIF

safety1 = 1

IF(inches<13) AND (IN3 = 1) THEN
HIGH 7
HIGH 6
ELSE
LOW 7
LOW 6
ENDIF

IF(inches>5) AND (compare = 1) THEN
lower = 1
ELSE
lower = 0
ENDIF


IF(inches>1)AND (compare = 1) AND (height1 = 0) AND(safety1 = 1) AND (IN2 = 1) OR(lower = 1)THEN
HIGH 4
HIGH 5
ELSE
LOW 4
LOW 5
ENDIF

RETURN


Get_Sonar:
Ping = IsLow ' make trigger 0-1-0
PULSOUT Ping, Trigger ' activate sensor
PULSIN Ping, IsHigh, rawDist ' measure echo pulse
rawDist = rawDist */ Scale ' convert to uS
rawDist = rawDist / 2 ' remove return trip
RETURN

Get_Sonar2:
Ping2 = IsLow ' make trigger 0-1-0
PULSOUT Ping2, Trigger2 ' activate sensor
PULSIN Ping2, IsHigh, rawDist2 ' measure echo pulse
rawDist2 = rawDist2 */ Scale ' convert to uS
rawDist2 = rawDist2 / 2 ' remove return trip
RETURN

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-04-03 11:28
    It would help if you attach your program to the post or at least use "[code]" tags.
  • ercoerco Posts: 20,256
    edited 2011-04-03 14:23
    Are you using pullup or pulldown resistors on your input pins? You can't let them float, they must be forced high or low through a high-value resistor, like 10K.
  • davejamesdavejames Posts: 4,047
    edited 2011-04-03 15:12
    WV...civility please.

    Could you edit your post? Thanks much.

    DJ
  • wvmusicwvmusic Posts: 4
    edited 2011-04-04 06:48
    People really mad about my language? Are we children?

    Anyways, Ill try the pull-up resistors and see if I get a better result. Thanks for the help.

    Also, if your whole point in replying to my post is to criticize me, please find something better to do with your time. I'm new to Microcontrollers and appreciate the help.
  • wvmusicwvmusic Posts: 4
    edited 2011-04-04 06:49
    Thanks erco!
  • vaclav_salvaclav_sal Posts: 451
    edited 2011-04-04 08:36
    It wouold be nice to know exactly where your code fails.
    Maybe that way you will not get comments on the format. Or maybe not - some folks just like to make noise. If you cannot impress with briliance, dazzle them with BS.
    .
    Anyway - I would check

    Ping = IsLow ' make trigger 0-1-0

    If you are setting the Ping low - I did not see anywhere in your code setting the direction of that pin to output.

    Personnaly I would read the state ot the pin first, just to make sure.
    From your description it was not clear to me what exactly you changed in hardware from you originally working code.
    (That's why I did not make my initial comment on pull-up / pull- down resistors.)
    Vaclav
  • ercoerco Posts: 20,256
    edited 2011-04-04 09:21
    I see input switches on pins 2 & 3, which should have pullup or pulldown resistors as appropriate. Your code:

    IF(IN3 = 1) THEN
    up1 = 1
    ENDIF
    IF(IN3 = 0) THEN
    up1 = 0
    ENDIF

    IF(IN2 = 1) THEN
    down1 = 1
    ENDIF
    IF(IN2 = 0) THEN
    down1 = 0
    ENDIF

    can be crunched down to

    UP1=IN3
    DOWN1=IN2

    or since your program appears to check them each loop, you can simply use aliases, or simply refer to them in your following code as IN3 (instead of UP1) and IN2 (instead of DOWN1).
  • Beau SchwabeBeau Schwabe Posts: 6,559
    edited 2011-04-04 10:11
    wvmusic,

    "People really mad about my language? Are we children?" - we aren't, but there are plenty of children that frequent these forums. Will you please edit your post?
  • Jessica UelmenJessica Uelmen Posts: 490
    edited 2011-04-04 10:14
    @wvmusic - Just a heads up that I edited your original post. While the people responding may not be children, we do have several youngsters that frequent our forums. And according to our Forum Guidelines, swearing is not allowed for this reason.
  • wvmusicwvmusic Posts: 4
    edited 2011-04-04 18:10
    Thanks for all the help everyone. The pull down resistors did the trick. I'll be sure to suppress my language. But I'm really not understanding of it at all.
  • ercoerco Posts: 20,256
    edited 2011-04-05 08:14
    vaclav: Rules are rules; they apply to everyone equally. There's nothing wrong with keeping things G-rated and family friendly, especially if you're the person asking for help. Better to point out a breach of etiquette early with a new poster than let it become a bad habit. Part of what makes this Parallax forum great (and different, look at other forums) is that everyone is enthusiastic, helpful, and polite. I don't blame Parallax one byte for enforcing their rules, I'm quite thankful for it.
  • sam_sam_samsam_sam_sam Posts: 2,286
    edited 2011-04-06 07:04
    People really mad about my language?......>Yes because we have Forum members that under the age of 18 years old AND WE NEED TO SET AN EXAMPLE....> HOW TO ACT<.... on this forum this is a MUST
    Are we children?..........> No but we have Forum members that ARE under the age of 18 years old So if you feel the need to swear Please use this ******* or nothing at all
    I second this point
    vaclav: Rules are rules; they apply to everyone equally. There's nothing wrong with keeping things G-rated and family friendly, especially if you're the person asking for help. Better to point out a breach of etiquette early with a new poster than let it become a bad habit. Part of what makes this Parallax forum great (and different, look at other forums) is that everyone is enthusiastic, helpful, and polite. I don't blame Parallax one byte for enforcing their rules, I'm quite thankful for it.


    I do not argee with you on these points
    Here is why because by what you are saying are we setting a GOOD example of how to ack on this forum ?
    To all you Parallax moderators / employees hypocrites.
    Obviously your employer value your time spend on editing four letter words and replacing them with #@?? and other characters – fine.
    I fully understand your obligations to your employer and your desire to remain on payroll – been there done that.
    But as the poster said , we are not children and you are obviously more qualified and eager to be watchdog editors than to contribute to the technical resolution of the problem.

    By the way – it must be OK to use old fashioned expressions which escaped your censors pen. <......... That depend on what are saying

    You must be the children you so gallantly protect, because what you demonstrated here is absolutely childish.

    IF Don't_like THAN
    GOTO Delete
    ENDIF

    Vaclav
  • ZootZoot Posts: 2,227
    edited 2011-04-06 09:09
    wv -- "pull down" or "pull up" resistors are "weak" resistors that "force" a circuit (in this case, the input pin of your Stamp) into a known state (low or high). The reason that "weak" resistors are used is so that the pulled "default" state of the circuit can be overridden by the "real" input (a switch, a sensor, etc.).

    The idea is that the pull-up or pull-down resistor is "just enough" to make the circuit a 0 or 1, but any input from the outside will be so much stronger that the input will change. Common values for pull-up or pull-downs are 5k-20k. If you connect an input pin through a 10k resistor to 5v, that pin will always see 1. But if a switch connected *directly* to the pin is switched to ground, the incoming 0 (ground) is so much "stronger" than the 5v coming through the 10k resistor that the input pin will see a 0. When the switch is released, the circuit is immediately "pulled" back to 5v.

    You often see pull-ups or pull-downs on transistor bases for the same reason, so the input of the transistor does not "float around" and accidentally switch on or off.

    The Stamp manual and "What's a Microcontroller" both have examples of using pull-ups and such.

    P.S. -- to everyone else, wv seemed still unclear on *why* the pullup resistors were necessary, yet the thread descended into bickering over whether or not it is OK to edit a post for vulgar langauge, and the original question remained somewhat unanswered. Given that a huge portion of Parallax's promo material and writing efforts are geared towards the elementary, middle-, and high-school educational markets, it's a no-brainer that foul language is going to be -- and should be -- heavily redacted. Personally, I see no reason to use such language in an online community anyway (and believe me, I swear like an old-time sailor at home).
Sign In or Register to comment.