Do Loop


Closed Thread
Results 1 to 14 of 14

Thread: Do Loop

Hybrid View

  1. #1
    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

  2. #2
    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.

  3. #3
    Join Date
    Feb 2012
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Thanks everyone for your assistance. The problem end up being associated with the LDR. I have the output of the LDR circuit going to the comparator of the PIC. The result is then assigned to a variable and subsequent code is used to make decisions. Here is a simplified example of my original code- VL is the variable.

    For I = 1 to 20

    Pause 500
    ADCIN 0, VL

    If VL => 300 then
    Light = 1
    Indicator LED = 1
    Endif

    If VL =< 100 then
    Light = 0
    IndicatorLED = 0
    Endif

    Next I

    My thinking was that I could bypass the threshold that exists in a LDR between light and dark by inserting a gap of 100 ( difference between 300 and 100) and a half second Pause in the For / Next loop. I used the For/Next loop count to 20 along with the Pause to ensue the light change was permanent and not from some errant source.

    This did work but during the time VL hovered around the threshold zone (between 300 and 100) I start receiving unpredictable results. If at the end of the 20 count the threshold was still present the program became confused and would open the door halfway, stop, and crash.

    So I changed the code to this:


    For I = 1 to 20

    Pause 500
    ADCIN 0, VL

    If VL => 300 then
    Light = 1
    Indicator LED = 1

    Else

    Light = 0
    IndicatorLED = 0
    Endif

    Next I


    This seems to work but only have one night of results but I'm optimistic.

    skybox

  4. #4
    Join Date
    Dec 2011
    Location
    IO93ok
    Posts
    190


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    I did something similar for a chicken coop door and originally had problems with the ldr as well.

    I countered this and shadows etc by flipping a counter. Single microswitch and a small 12v model motor.

    This code worked well.
    Code:
    '###############################################
    '#                                             #
    '#  Automatic chicken coop sliding door.       #
    '#                                             #
    '#  12 Volt Battery powered.                   # 
    '#  Open when light, close when dark.          #
    '#  Sleep 4 hours between to conserve power    #
    '#  and to prevent retriggering.               #
    '#                                             #
    '#  Uses single microswitch as feed back.      #
    '#                                             #
    '#  Pic 16f818 --- Rob Lane - Dec 2011         #
    '#                                             #
    '###############################################
    
    OSCCON=$60                  'set 4mhz internal oscillator
                                '(Set INT_OSC in programmer fuses)
                                
    ' Define ADCIN parameters
    Define	ADC_BITS	10  	'Set number of bits in result
    Define	ADC_SAMPLEUS	50	'Set sampling time in uS
    
    Photo   	var	word	   	'Create 'photo' to store result of ldr input   
    Counter1    var byte        'flip flop counter variable
    
       'PIN ASSIGNMENTS
    LDR         var porta.0
    
    MotorUp     var portb.4      
    MotorDown   var portb.5
    Switch      var portb.6 
    
         Counter1=1         'Set counter for power up, door must 
                            'be closed in daylight for startup.
         pause 5000
         TRISA=%00000001	' Set PORTA 
         TRISB=%01000000    ' Set PORTB     
         input switch
         
         
    '#########################################################     
         
    Main:	          
            ADCIN ldr, photo                 'read ldr adc value
            IF Photo <15000 Then checkup     'daylight
            IF Photo >30000 Then checkdown   'darkness 
    		'sleep 300             'about 5 minutes low power
    	    goto Main 
    	    
    '##########################################################
    	    
    checkup:
            if counter1=1 then up
            goto main	    
    		
    up:		
            High MotorUp           'start motor up
       		pause 1000             'wait 1 sec so micro switch opens
    stopup:
       		if switch = 1 then pressedup 'check microswitch state
            goto stopup                  'check again till true
            
    pressedup: 
            low motorup           'stop motor
       		Counter1=2            'flip flop counter for up/down of door
       		sleep 150
       		'sleep 14400          'sleep 4 hours
       		goto main
    
    '##########################################################
    
    checkdown:
            if counter1=2 then down
            goto main    		
    down:		
    	    high MotorDown                   'start motor down
       		pause 1000
    stopdown:
       		if switch = 1 then presseddown   'check again
            goto stopdown
       		
    presseddown:
            low MotorDown
       		Counter1=1
       		sleep 150
       		'sleep 14400
       		goto main
    		
    end

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


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Problem often lie in how you interface the LDR to the PIC. But a software crash... well something's wrong somewhere else. Code, hardware... hard to pin point without having the whole thing here...
    Steve

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

  6. #6
    Join Date
    Feb 2012
    Posts
    8


    Did you find this post helpful? Yes | No

    Default Re: Do Loop

    Hello Rob,

    Thanks for sharing your code. It is always interesting to see others' solutions to common challenges. Would it be too much to ask to see the schematic of your LDR circuit? I'm curious as to how you configured it.

    Thanks

    Skybox

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 : 0

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