Results 1 to 8 of 8

Thread: getting out of a loop

Hybrid View

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Angry getting out of a loop

    Hi guy I know that you can solve this

    I am making an led flash through a parallel port now the code which I am using, it send "1" and then stop for some seconds and then again it sends "0" and then stops for some seconds. That is all in a loop like this

    Code:
    Do
    PortOut 888, 1
    delay
    PortOut 888, 0
    delay
    Loop Until stopl = True
    This delay function is this

    Code:
    For i = 1 To 1000000
    Next i
    now I have two command buttons, one is for flashing the led and the other one is for stoping it.

    Whenever I press the stop button, it will say that

    stopl = true

    so the loop will stop

    But the problem is that whenver the led starts flashing, my whole program is frozen and I cannot click anywhere.

    I know that there is something wrong but i cannot figure it out
    Please give me a solution!

    Thanks
    Baaaaaaaaah

  2. #2
    Dreamlax
    Guest
    Put a DoEvents in your loop (the 'Do...Loop' one).

  3. #3
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    You might like to try DoEvents in the For..Next loop as well.

  4. #4
    Dreamlax
    Guest
    I suppose the sleep API wouldn't quite work in the same way as the program would freeze...

  5. #5
    Matthew Gates
    Guest
    Use Exit For or Exit Do to exit a loop.

  6. #6
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    'Use a boolean variable to stop a For Next Loop 
    
    Option Explicit 
    
    Dim bGetOut As Boolean 'trigger to stop loop 
    
    Private Sub Command1_Click() 
    bGetOut = False 'reset if you want to start again 
    Dim myInt As Integer 'incremen 
    
    Do While Not bGetOut 
    Randomize 'Rnd Function 
    myInt = myInt + CInt(Rnd * 10) + 1 'create new caption 
    Me.Caption = myInt 
    DoEvents 
    Loop 
    End Sub 
    
    Private Sub Form_DblClick() 
    'dbl click the form to stop the loop 
    bGetOut = True 
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  7. #7
    Matthew Gates
    Guest

  8. #8

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    OK

    DoEvents


    That works now but there is another problem

    I am creating a stepper motor controller which has a stepper motor and a LED.

    -- When the stepper motor moves, I want to flash the led. To flash the LED, I need to first send a "1" and then a delay of some second, and then a "0".

    -- When the motor is moving -- it is in a loop -- I want to flash the led.

    --I have created a function for the delay that looks like this

    Code:
    For delaytime = 1 to 40000
    Next
    --Now If I put this code:

    Code:
    Do
    (Here is all the code to rotate the motor in a loop which is too long so I did not put it here)
    
    portn = PortIn(888)
    Call PortOut(888, portn + 16)
    delay
    Call PortOut(888, portn + 0)
    delay
    Loop

    (PortOut, PortIn are just two functions to send and retrieve a value to and from the parallel port. "16" is just the value which lights the led, and 0 turns the led off. I retrived the value from the parallel port first because I wanted the motor to keep rotating)

    The problem is that when the delay is used, it stop the led flashing and also the stepper motor. I want the stepper motor to keep doing its work but the led should stop for the time which is given the loop.

    If you dont understand anything then please ask me
    Baaaaaaaaah

Posting Permissions

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



Click Here to Expand Forum to Full Width