Do Loop


Closed Thread
Results 1 to 14 of 14

Thread: Do Loop

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    8

    Default Do Loop

    I have a project with a few relays and a 12 volt dc motor. The motor opens and closes a door based on a light and dark (LDR) The door has hall sensors to limit travel. For the most part everything works fine but every so often the door starts to open and then the program goes into a loop or crashes. I compiled the program with the watchdog timer off. Since this start happening I did some research and found that without the watchdog timer employed the program can be reset after a per-determined time period. Obviously, I need to turn watchdog on. However, I have a question about my code that may be contributing to the problem.

    Below is a sample of a "DO Loop" I use:

    If (Light = 1) and (DoorClosed = 0) then

    Relay2 = 1
    DO
    Loop until DoorOpen = 0
    Relay2 = 0

    End if

    Notice there is no statement after "DO". It goes immediately to the "loop until DoorOpen = 0." This does work but I'm wondering if this might be contributing to the problem. Is this a proper way of using the DO Loop? That is without a statement in between DO and Loop?

  2. #2
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    838


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    i would not think its a problem , but you can always try using while wend loop see if changes the problem

    while dooropen <> 0
    wend

  3. #3
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Using while wend is a better method, but nothing shown here should cause your problem. Can you share all of your code? I had a similar issue by not exiting a subroutine properly, the program worked for a while then would eventually crash. It took me a while to pin point it.
    Shawn

  4. #4
    Join Date
    Feb 2012
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Shawn,

    Thanks for the reply. I do have a question concerning your answer. In the PBP manual it states the "While...Wend" has been deprecated and to use the "DO...Loop" instead. I never completely trust manuals because most become outdated with code updates. As such, I'd very much appreciate an elaboration as to why I should use the "While...Wend' when the manual states otherwise.

    Thanks

    Skybox

  5. #5
    Join Date
    May 2006
    Location
    Del Rio, TX, USA
    Posts
    343


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    For what you are doing, you can also use this method:

    Code:
    WaitForDoor:
        IF DoorOpen <> 0 THEN WaitForDoor

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Code:
            WHILE DoorOpen : WEND
            '
            '       or
            Repeat : Until !DoorOpen
            '
            '       or        
    SitAndSpin:  If DoorOpen then SitAndSpin
            '
            '       or        
            Do Until !DoorOpen : Loop
            '
            '       or        
            do : Loop WHILE DoorOpen
    As far as I'm aware of, PBP take care of the WatchDog timer, so it shouldn't be an issue...
    Last edited by mister_e; - 16th December 2012 at 09:01.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Apr 2007
    Location
    Pennsylvania, USA
    Posts
    158


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Quote Originally Posted by skybox View Post
    Shawn,

    Thanks for the reply. I do have a question concerning your answer. In the PBP manual it states the "While...Wend" has been deprecated and to use the "DO...Loop" instead. I never completely trust manuals because most become outdated with code updates. As such, I'd very much appreciate an elaboration as to why I should use the "While...Wend' when the manual states otherwise.

    Thanks

    Skybox
    I have found the while wend to always work, I know I had problems with the do loop, but that may have been something else causing the problem. Some of the other suggestions here look to be a better alternative to either of these anyways.
    Shawn

  8. #8
    Join Date
    Apr 2010
    Posts
    13


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    I am not sure if this would help or not , but I did have problem with some simple code that I had included Do,Loop.
    The code worked fine when it was compiled using PBP3.0 but it did not work if it was compiled by PBP2.50B , which I had on an older machine.
    The problem was solved when I changed the Do,Loop section to GOTO , and then the result from both PBP3.0 and 2.50B worked flawlessly.

Similar Threads

  1. FOR..NEXT Loop Bug?
    By MSapper in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 12th April 2011, 01:04
  2. if ... then loop
    By lerameur in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 9th November 2010, 23:08
  3. Can't get ADC to loop
    By TravisM in forum mel PIC BASIC
    Replies: 2
    Last Post: - 11th October 2009, 15:33
  4. Help with loop
    By wildbilly in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd January 2007, 16:59
  5. While LOOP
    By actionplus in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 5th March 2004, 14:59

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts