How to blink 8 LEDs at different rates- concurrently?


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    May 2007
    Posts
    604

    Default How to blink 8 LEDs at different rates- concurrently?

    To do the equivalent of the code below with each LED blinking at a different rate - except that all 8 LEDs must blink concurrently.
    Code:
    PORTB = 0
    TRISB = 0
    
    MAIN:
    
    LOOP1:
      TOGGLE PORTB.0
      PAUSE 500
    GOTO LOOP1
    
    LOOP2:
      TOGGLE PORTB.1
      PAUSE 220
    GOTO LOOP2
    
    LOOP3:
      TOGGLE PORTB.2
      PAUSE 380
    GOTO LOOP1
    
    LOOP4:
      TOGGLE PORTB.3
      PAUSE 750
    GOTO LOOP4
    
    LOOP5:
      TOGGLE PORTB.4
      PAUSE 170
    GOTO LOOP5
    
    LOOP6:
      TOGGLE PORTB.5
      PAUSE 400
    GOTO LOOP6
    
    LOOP7:
      TOGGLE PORTB.6
      PAUSE 620
    GOTO LOOP7
    
    LOOP8:
      TOGGLE PORTB.7
      PAUSE 130
    GOTO LOOP8
    
    GOTO MAIN
    Last edited by rmteo; - 25th April 2010 at 16:10.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    You want them all to come on at the same time and drop out at different rates?
    Code:
    PORTB = %11111111
    PAUSE XXX
    PORTB = %11111110
    PAUSE XXX
    PORTB = %11111100
    ...
    LOOP OVER...
    Something like that?
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    Have as many LED's as you have pins...
    Code:
    	LoopALED var BYTE
    	LoopBLED var BYTE
    	LoopCLED var BYTE
    	LoopDLED var BYTE
    	LoopELED var BYTE
    	LoopFLED var BYTE
    	LoopGLED var BYTE
    	LoopHLED var BYTE
    	'
    	LoopACON con 50
    	LoopBCON con 22
    	LoopCCon con 38
    	LoopDCon con 75
    	LoopECon con 17
    	LoopFCon con 40
    	LoopGCon con 62
    	LoopHCon con 13
    	'
    	TRISB=0
    Loop:
    	If LoopALED=0 then
    		LoopALED=LoopACON
    		Toggle PortB.0
    		else
    		LoopALED=LoopALED-1
    		endif
    	'
    	If LoopBLED=0 then
    		LoopBLED=LoopBCON
    		Toggle PortB.1
    		else
    		LoopBLED=LoopBLED-1
    		endif
    	'
    	If LoopCLED=0 then
    		LoopCLED=LoopCCON
    		Toggle PortB.2
    		else
    		LoopCLED=LoopCLED-1
    		endif
    	'
    	If LoopDLED=0 then
    		LoopDLED=LoopDCON
    		Toggle PortB.3
    		else
    		LoopDLED=LoopDLED-1
    		endif
    	'
    	If LoopELED=0 then
    		LoopELED=LoopECON
    		Toggle PortB.4
    		else
    		LoopELED=LoopELED-1
    		endif
    	'
    	If LoopFLED=0 then
    		LoopFLED=LoopFCON
    		Toggle PortB.5
    		else
    		LoopFLED=LoopFLED-1
    		endif
    	'
    	If LoopGLED=0 then
    		LoopGLED=LoopGCON
    		Toggle PortB.6
    		else
    		LoopGLED=LoopGLED-1
    		endif
    	'
    	If LoopHLED=0 then
    		LoopHLED=LoopHCON
    		Toggle PortB.7
    		else
    		LoopHLED=LoopHLED-1
    		endif
    	'
    	Pause 10
    	Goto Loop
    Homework Done! Melanie's off down the pub...

    Now repeat the above (excluding definitions) in less than 8 Lines of Code... (yes it can be done)!

  4. #4
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    mack, the LEDs need to blink all at the same time, so that will not work.

    Mel, use arrays and a for-next loop - along these lines?
    Code:
    Loop:
    for x = 0 to 7
      If LoopLED[x] = 0 then
        LoopLED[x] = LoopCON[x]
        Toggle PORTB.x
      else
        LoopLED[x] = LoopLED[x]-1
      endif  
    next x
    Pause 10
    Goto Loop

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


    Did you find this post helpful? Yes | No

    Default

    Yes, pretty much, but I don't think PortB.x will work, so...

    Code:
    	ShadowPortB=0
    Loop:
    	for x = 0 to 7
    		If LoopLED[x] = 0 then
    			LoopLED[x] = LoopCON[x]
    			ShadowPortB.0(x)=ShadowPortB.0(x)^1
    			else
    			LoopLED[x] = LoopLED[x]-1
    			endif  
    		next x
    	PortB=ShadowPortB
    	Pause 10
    	Goto Loop
    Where ShadowPort is a BYTE.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Just to be different ...
    And because she said "less than 8 Lines of Code" ...

    Code:
    TRISB = 0
    Main:
        x = (x + 1) // 8
        PORTB.0(x) = !(LoopLED(x) < LoopCON(x))
        LoopLED(x) = (LoopLED(x) + 1) // (LoopCON(x) << 1)
        if x=7 THEN PAUSE 10
    GOTO Main
    DT

Members who have read this thread : 3

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