Problem with Variable
djmathman
Posts: 1
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!
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