Help with loops..


Closed Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Posts
    48

    Default Help with loops..

    If I want LED1 to blink 3 times and then LED2 3 times is there an "easier" way than this:

    Code:
    LED1 var PORTB.1
    LED2 var PORTB.2
    
    loop:
    HIGH LED1
    PAUSE 500
    LOW LED1                     'blink 1
    PAUSE 500
    
    HIGH LED1
    PAUSE 500
    LOW LED1                     'blink 2
    PAUSE 500
    
    HIGH LED1
    PAUSE 500
    LOW LED1                     'blink 3
    PAUSE 500
    
    HIGH LED2
    PAUSE 500
    LOW LED2                     'blink 1
    PAUSE 500
    
    HIGH LED2
    PAUSE 500
    LOW LED2                     'blink 2
    PAUSE 500
    
    HIGH LED2
    PAUSE 500
    LOW LED2                     'blink 3
    PAUSE 500
    
    GOTO loop
    END
    Cant you only make something like this and get loop1 to repeat only 3 times and then start loop2.. ?

    Code:
    LED1 var PORTB.1
    LED2 var PORTB.2
    
    loop1:
    HIGH LED1
    PAUSE 500
    LOW LED1                     
    PAUSE 500
    GOTO loop1
    
    loop2
    HIGH LED2
    PAUSE 500
    LOW LED2                     
    PAUSE 500
    GOTO loop2
    
    END
    So I want a loop that repeats itself 3 times and then starts loop2, how do I do that?

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    To execute a loop three times...

    Example 1
    Code:
    	CounterA var Byte
    
    	..
    
    	For CounterA=0 to 2
    		HIGH LED1
    		PAUSE 500
    		LOW LED1                     
    		PAUSE 500
    		Next CounterA
    In the above example, counting starts at zero... it's the same as specifying
    Code:
    	For CounterA=1 to 3

    Example 2
    Code:
    	CounterA var Byte
    
    	..
    
    	CounterA=3
    	While CounterA>0
    		CounterA=CounterA-1
    		HIGH LED1
    		PAUSE 500
    		LOW LED1                     
    		PAUSE 500
    		Wend
    I'm sure a few more examples can be had too... but that's a starter...

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    Code:
    X VAR BYTE
    LED1 VAR PORTB.1
    LED2 VAR PORTB.2
    
    Main:
        X = 0
        REPEAT
          HIGH LED1
          PAUSE 500
          LOW LED1
          PAUSE 500
          X = X + 1
        UNTIL X = 3 ' pulse LED1 3 times
        
        REPEAT
          HIGH LED2
          PAUSE 500
          LOW LED2
          PAUSE 500
          X = X + 1
        UNTIL X = 6 ' pulse LED2 3 times
        GOTO Main
    
        END
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

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


    Did you find this post helpful? Yes | No

    Default

    Not to make this a contest of "How many ways can you...", but I thought another example, using different logic, would also be instructive.
    Code:
    X VAR BYTE
    LED1 VAR PORTB.1
    LED2 VAR PORTB.2
    
    
    
        X = 0		' Reset Iteration Count
    Loop:
        IF X < 3 THEN	' Iterations 0,1,2
    	HIGH LED1
          	PAUSE 500
          	LOW LED1
          	PAUSE 500
        ELSE		' Iterations 3,4,5
          	HIGH LED2
          	PAUSE 500
          	LOW LED2
          	PAUSE 500
        ENDIF
        X = X + 1		' Increment Iteration Count
        IF X < 6 THEN Loop  ' Loop back if less than 6 iterations
    
        END
    So, now you have examples using all of the following:

    FOR...NEXT
    WHILE...WEND
    REPEAT...UNTIL
    IF...THEN

    All of these will produce the same results, just with subtle differences in the logic used to accomplish the task, making this tread a nice little study.

    HTH,
    SteveB

Similar Threads

  1. 4 Channel Thermostat using PID loops
    By malc-c in forum Code Examples
    Replies: 36
    Last Post: - 18th March 2013, 10:17
  2. Going Loopy - How do i get my Loops sorted out ???
    By gtvmarty in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th September 2009, 05:02
  3. While Loops Slow??
    By Frozen001 in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 29th December 2008, 18:12
  4. For Next Loops and Serin2?
    By scottl in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 10th November 2007, 20:28
  5. Proximity detection - tuned loops
    By George in forum Off Topic
    Replies: 1
    Last Post: - 31st July 2007, 17:25

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