News:

Let's find out together what makes a PIC Tick!

Main Menu

While Wend Loop

Started by pjdenyer, Dec 16, 2023, 12:37 PM

Previous topic - Next topic

pjdenyer

Hi all, I have a little routine that controls two motor an limit switches.

        Low EntryUpMotor
        Low ExitUpMotor
        While EntryDoorUp = 1 Or ExitDoorUp = 1
            If EntryDoorUp = 0 Then High EntryUpMotor
            If ExitDoorUp = 0 Then High ExitUpMotor
        Wend

When the first limit switch closes it stops the appropriate motor, however, when the second limit switch closes the motor continues and the program does not exit the While Wend loop.
My question is are you allowed multiple conditions in a While statement, if you are then why is the second if not executing which would satisfy the While conditions and exit.

trastikata

Hi,

to exit the While loop, both EntryDoorUp and ExitDoorUp must be 0 at the same time, is that what's happening?

pjdenyer

#2
Yes, sometime is works and sometimes it doesn't even when I close the limit switches manually

I have been playing with it for a while and it might be a problem with my dev board, so I will try another

pjdenyer

Hi, I seem to have resolved the issue with the following code by using a Flag Byte

        While DoorFlag <> 3
            If EntryDoorDown = 0 Then
                High EntryDownMotor
                Set DoorFlag.0
            EndIf
            If ExitDoorDown = 0 Then
                High ExitDownMotor
                Set DoorFlag.1
            EndIf
        Wend

trastikata

I suspect that you are using Pin aliases in those loops and depending on the speed and delay of the switch action you can get transients and the Pin state will change several times before it reaches steady state.

The difference between the first and the second example you posted is that flags are being set only once and they are not cleared if the variable change, effectively de-bouncing the pin.

Thus without having more information about hardware, speeds, switch de-bouncing can't say really what the issue is.


Stephen Moss

Quote from: pjdenyer on Dec 16, 2023, 12:37 PMHi all, I have a little routine that controls two motor an limit switches.

        Low EntryUpMotor
        Low ExitUpMotor
        While EntryDoorUp = 1 Or ExitDoorUp = 1
            If EntryDoorUp = 0 Then High EntryUpMotor
            If ExitDoorUp = 0 Then High ExitUpMotor
        Wend

When the first limit switch closes it stops the appropriate motor, however, when the second limit switch closes the motor continues and the program does not exit the While Wend loop.
My question is are you allowed multiple conditions in a While statement, if you are then why is the second if not executing which would satisfy the While conditions and exit.
It is a little unclear from that what you want it to do (comments would help), but I think your logic is wrong.
Presuambly Low EntryUpMotor & Low ExitUpMotor start the motors and limit switches set the states of EntryDoorUp & ExitDoorUp to 0 to indcate when the respective door is up and stop its motor.
If that is correct then is not
While EntryDoorUp = 1 Or ExitDoorUp = 1 wrong?
Thus, whichever door reaches its up position first will have its motor stopped by the code within the While loop, however as soon as the second door reaches its up position and both EntryDoorUp & ExitDoorUp are 0 the While loop will exit immediately and so the code within the While loop that should stop the second motor will not be executed, hense the motor keep running.

This may be a better solution...
Do
If EntryDoorUp = 0 Then High EntryUpMotor
If ExitDoorUp = 0 Then High ExitUpMotor
Loop While EntryDoorUp = 1 Or ExitDoorUp = 1
This way the code within the loop will see the state change in EntryDoorUp & EntryDoorDown, stopping the relevant motor before the While condition is assessed, unlike your original code where it was after the While condition was assessed.
Note: I have not tried the code and so I do not know if Loop While will accept an OR condition, if not you will have to use you DoorFlag there instead (Loop While DoorFlag <> 0).