Shop OBEX P1 Docs P2 Docs Learn Events
bug in code, anyone see my problem? — Parallax Forums

bug in code, anyone see my problem?

ArchiverArchiver Posts: 46,084
edited 2004-03-21 19:57 in General Discussion
Everything is working except for my second microswitch. It seems
that even if the input is 0 it still enters the if=1 loop in the
statement "IF IN14=1 THEN ReleaseItem ReleaseItem:" I even tried
eliminating all other code and it still seems to enter that loop.
Any good debuggers out there?




' { $STAMP BS2}

'Variables
Time1 VAR Word 'Specified amount of time 1
Time2 VAR Word 'Specified amount of time 2
ReleaseTime VAR Word 'Specified amount of time 3
t VAR Word 'test
PulseTime VAR Word '

'Outputs
OUTPUT 5 'Motor Up
OUTPUT 4 'Motor Back
OUTPUT 3 'Motor Counterclockwise
OUTPUT 2 'Motor Clockwise
OUTPUT 1 'Motor Forward
OUTPUT 0 'Motor Down

'Inputs
INPUT 15 'Microswitch 1
INPUT 14 'Microswitch 2

'Test Statements
DEBUG "State of Input Pin 15: ", BIN IN15, CR
DEBUG "State of Input Pin 14: ", BIN IN14, CR

DEBUG "State of Output Pin 0: ", BIN OUT0, CR
DEBUG "State of Output Pin 1: ", BIN OUT1, CR
DEBUG "State of Output Pin 2: ", BIN OUT2, CR
DEBUG "State of Output Pin 3: ", BIN OUT3, CR
DEBUG "State of Output Pin 4: ", BIN OUT4, CR
DEBUG "State of Output Pin 5: ", BIN OUT5, CR

'Wait for Receiver
Again:
PULSIN 7, 1, PulseTime 'Measure Positive Pulse
DEBUG CLS, DEC ? PulseTime 'If not Display
IF PulseTime < 450 THEN Again 'If 0 try again
IF PulseTime > 650 THEN Again

Main:
'Initializations
'OUT2 = 1 'Motor Clockwise
OUT5 = 1 'Motor Up
DEBUG "Motor Up", CR

Hold:
IF IN15=0 THEN Hold 'If Microswitch 1 is not hit, keep
going...

IF IN15=1 THEN Backward 'If Microswitch 1 is hit
Backward:
DEBUG "Microswitch 1 Hit", CR

IF IN14=0 THEN Backward2 'Check to see if Microswitch 2 is
hit, if not...
Backward2:
OUT5 = 0 'Stop Motor Up
DEBUG "Stop Motor Up", CR
'HIGH 4 'Motor Reverse for Half sec and Pause
for Quarter sec
'PAUSE 500
'LOW 4
'PAUSE 250

IF IN14=0 THEN Backward3
Backward3:
OUT4 = 1 'Motor Reverse
DEBUG "Motor Reverse", CR


IF IN14=1 THEN ReleaseItem 'If Microswitch 2 is hit
ReleaseItem:
DEBUG "State of Input Pin 14: ", BIN IN14, CR
DEBUG "Microswitch 2 Hit", CR
OUT4 = 0 'Stop Motor Reverse
DEBUG "Stop Motor Reverse / State of Output Pin 4: ", BIN
OUT4, CR
OUT2 = 0 'Stop Motor Clockwise
DEBUG "Stop Motor Clockwise / State of Output Pin 2: ", BIN
OUT2, CR

FOR ReleaseTime = 1 TO 10 'Tested Release Time
OUT3 = 1 'Motor Counterclockwise
NEXT

OUT3 = 0 'Stop Motor Counterclockwise

FOR Time1 = 1 TO 2 'For specified amount of Time 1
OUT1 = 1 'Motor Forward
DEBUG "State of Output Pin 1:", BIN OUT1, CR
NEXT
OUT1 = 0 'Stop Motor Forward
DEBUG "State of Output Pin 1:", BIN OUT1, CR

FOR Time2 = 1 TO 2 'For specified amount of Time 2
OUT0 = 1 'Motor Down
DEBUG "State of Output Pin 0:", BIN OUT0, CR
NEXT
OUT0 = 0 'Stop Motor Down
DEBUG "State of Output Pin 0:", BIN OUT0, CR
STOP

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-03-21 05:59
    At 05:04 AM 3/21/04 +0000, Christina wrote:
    >Everything is working except for my second microswitch. It seems
    >that even if the input is 0 it still enters the if=1 loop in the
    >statement "IF IN14=1 THEN ReleaseItem ReleaseItem:" I even tried
    >eliminating all other code and it still seems to enter that loop.
    >Any good debuggers out there?

    Christina -

    I suspect you are misunderstanding how IF...THEN operates.

    Example -

    IF [noparse][[/noparse]condition] then [noparse][[/noparse]routine name of where to go when condition is met]

    IF x = 1 then X_is_ON
    X_is_ON:

    When the next following statement is indeed that routine name (as directly
    above), then it's as if the conditional statement was never made.

    There are two ways to correct this.

    Method 1 -

    IF x = 1 then X_is_ON

    X_is_OFF:
    'Process the x is off condition
    GOTO Continue_Checking

    X_is_ON:
    'Process the x is on condition
    GOTO Continue_Checking

    ... other code ...

    Continue_Checking:

    Method 2 -

    IF x <> 1 then X_is_OFF

    X_is_ON:
    'Process the x is on condition
    GOTO Continue_Checking

    X_is_OFF:
    'Process the x is off condition
    GOTO Continue_Checking

    ... other code ...

    Continue_Checking:
    - - -

    I hope that doesn't add to your confusion.

    Regards,

    Bruce Bates
  • ArchiverArchiver Posts: 46,084
    edited 2004-03-21 06:29
    I figured it out. It is usually something as stupid as forgetting
    to keep checking the if statement. But thank you

    revised section:
    Backward3:
    OUT4 = 1 'Motor Reverse
    DEBUG "Motor Reverse", CR
    IF IN14=0 THEN Backward3

    --- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
    > At 05:04 AM 3/21/04 +0000, Christina wrote:
    > >Everything is working except for my second microswitch. It seems
    > >that even if the input is 0 it still enters the if=1 loop in the
    > >statement "IF IN14=1 THEN ReleaseItem ReleaseItem:" I even
    tried
    > >eliminating all other code and it still seems to enter that
    loop.
    > >Any good debuggers out there?
    >
    > Christina -
    >
    > I suspect you are misunderstanding how IF...THEN operates.
    >
    > Example -
    >
    > IF [noparse][[/noparse]condition] then [noparse][[/noparse]routine name of where to go when condition is
    met]
    >
    > IF x = 1 then X_is_ON
    > X_is_ON:
    >
    > When the next following statement is indeed that routine name (as
    directly above), then it's as if the conditional statement was never
    made.
    >
    > There are two ways to correct this.
    >
    > Method 1 -
    >
    > IF x = 1 then X_is_ON
    >
    > X_is_OFF:
    > 'Process the x is off condition
    > GOTO Continue_Checking
    >
    > X_is_ON:
    > 'Process the x is on condition
    > GOTO Continue_Checking
    >
    > ... other code ...
    >
    > Continue_Checking:
    >
    > Method 2 -
    >
    > IF x <> 1 then X_is_OFF
    >
    > X_is_ON:
    > 'Process the x is on condition
    > GOTO Continue_Checking
    >
    > X_is_OFF:
    > 'Process the x is off condition
    > GOTO Continue_Checking
    >
    > ... other code ...
    >
    > Continue_Checking:
    > - - -
    >
    > I hope that doesn't add to your confusion.
    >
    > Regards,
    >
    > Bruce Bates
  • ArchiverArchiver Posts: 46,084
    edited 2004-03-21 19:57
    Christina,

    I think the problem is with the structure of your IF…THEN statements.
    These can be a bit tricky.

    SIMPLIFIED EXAMPLE:
    Microswitch pushed = Motor on, then continue with rest of program
    Microswitch not pushed = Motor off, loop back up to check for switch push

    Out1= Motor output

    Line 1: Start:
    Line 2: IF microswitch is pushed THEN TurnMotorOn
    Line 3: Out1 = 0 'motor off
    Line 4: GOTO Start
    Line 5: TurnMotorOn:
    Line 6: Out1 = 1 'motor on
    Line X: Continue with rest of program

    Here's how it works:
    Line 2: Tests condition of switch
    Lines 3 & 4: Handles NOT pushed condition
    Lines 5 & 6: Handles pushed condition

    The tricky part is Line 2. IF Line 2 is False then the program
    automatically increments to the next line, Line 3. Therefore you need
    to include Lines 3 & 4 to handle the False condition. Notice that if
    Line 2 is True, it "leapfrogs" over Lines 3 & 4 by using the
    "TurnMotorOn" label.

    I've taken the liberty of reshuffling your IN14 logic to include this
    leapfrogging technique. Basically I just moved the IN14=1 statements
    so they are directly after the IF IN14=0 statement.

    IF IN14=0 THEN Backward2
    'if IN14 = 1, the program automatically increments down to
    'OUT4 = 0, therefore you don't need the "ReleaseItem" label
    OUT4 = 0 'Stop Motor Reverse
    OUT2 = 0 'Stop Motor Clockwise
    GOTO Release 'LEAPFROGS over Backward2

    Backward2:
    OUT5 = 0 'Stop Motor Up
    HIGH 4 'Motor Reverse
    PAUSE 500
    LOW 4
    PAUSE 250 'continues to Release:

    Release:
    FOR ReleaseTime = 1 TO 10 'Tested Release Time
    OUT3 = 1 'Motor Counterclockwise
    NEXT
    OUT3 = 0 'Stop Motor Counterclockwise

    FOR Time1 = 1 TO 2 'For specified amount of Time 1
    OUT1 = 1 'Motor Forward
    NEXT
    OUT1 = 0 'Stop Motor Forward

    FOR Time2 = 1 TO 2 'For specified amount of Time 2
    OUT0 = 1 'Motor Down
    NEXT
    OUT0 = 0 'Stop Motor Down

    STOP



    Dave
Sign In or Register to comment.