Shop OBEX P1 Docs P2 Docs Learn Events
Problem with Variable — Parallax Forums

Problem with Variable

djmathmandjmathman Posts: 1
edited 2011-02-02 18:08 in BASIC Stamp
Hey guys,
I have a problem with my program. I am trying to create a usage system that disables the gripper program after three tries. My servo is connected to port 14. It works, but it's very inconvenient. Here it is:

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

time VAR Word
cmDistance VAR Word
counter VAR Word
numtimes VAR Word
totaltimes VAR Word

cmConstant CON 2260
DO
PULSOUT 15,5
PULSIN 15,1,time

cmDistance = cmConstant ** time
IF cmDistance <= 4 THEN
numtimes = numtimes + 1
DEBUG DEC numtimes
FOR counter = 1 TO 100
PULSOUT 14,1000
PAUSE 20
NEXT
DEBUG DEC numtimes
PAUSE 2000
FOR counter = 1 TO 100
PULSOUT 14, 500
PAUSE 20
DEBUG DEC totaltimes
DEBUG DEC numtimes
NEXT
totaltimes = numtimes
ENDIF
DEBUG DEC totaltimes


IF numtimes = 3 THEN
END
ENDIF


PAUSE 100
LOOP

The bold part is my problem. I don't know why, but for some reason, debugging the value of the variable constantly and then referring it to another variable is the only way I can get it to work. Apparently the value of the variable resets after the if statement. Is this true? And is there any way I can fix this without the hassle? Please help!

Comments

  • FranklinFranklin Posts: 4,747
    edited 2011-02-02 17:50
    Which variable are you talking about? and it helps if you wrap your code in code tags so the indentation stays where we can read it. Also could you include a post of your debug statements?
  • Mike GreenMike Green Posts: 23,101
    edited 2011-02-02 18:08
    Keep in mind that DEBUG statements take time to execute. It's roughly 1ms for each character in the DEBUG output. Eventually, those delays plus the PAUSE 20 and the other statements in the loops get to where the servos won't work properly. You can keep the DEBUGs if you shorten the PAUSE delays and if the total loop time is approximately 20ms.
Sign In or Register to comment.