switches & LEDs ... my first attempt


Closed Thread
Results 1 to 15 of 15

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    So the requirement is as long as the switch is closed you want the respective LED to blink, but they blink at different rates?

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


    Did you find this post helpful? Yes | No

    Default

    Your request is slightly more complex than you first imagine, because if the PIC is busy timing one LED, it can't really be attending to the other. So we have to do a bit of magic...

    Here I've changed the Labels to make association between Switches, LED's, Constants and Variables more readable.

    Like Clint Eastwood said in the Dirty Harry films... "Every man has to know his limitations". Now this applies equally to PICs and programs as well. Here, in our example program, the limitation I've set, is that everything happens in multiples of 10mS... but the flip side is that with this method you can add as many LED's and Switches as you have pins on your PIC and each can have different timing rates...

    Code:
    	SwitchA var PORTA.0 	' switch on porta.0
    	SwitchB var PORTA.3	' switch on portb.0
    
    	LedA VAR PORTB.0	' LED on portb.0
    	LedB VAR PORTB.5 	' LED on PortB.5
    
    	TRISA.0 = 1 		' porta.0 is now an input
    	TRISA.3 = 1 		' Porta.3 is now an input
    	TRISB.0 = 0 		' Portb.0 is left
    	TRISB.5 = 0 		' Portb.5 is STB
    
    	CMCON=%00000111		' ensures PortA is in Digital Mode
    
    	LedABlinkRate con 10	' Blink Rate in Steps of 10mS (10=100mS)
    	LedBBlinkRate con 33	' Blink Rate in Steps of 10mS (33=330mS)
    
    	LedACounter var BYTE	' Counter for LedA
    	LedBCounter var BYTE	' Counter for LedB
    
    	Low LedA		' Startup with all LEDs OFF
    	Low LedB
    	LedACounter=0		' Startup with all Counters OFF
    	LedBCounter=0
    
    Main:
    	If SwitchA=0 THEN 	' Closing SwitchA blinks LedA
    		If LedACounter=0 then
    			Toggle LedA
    			LedACounter=LedABlinkRate
    			else
    			If LedACounter>0 then LedACounter=LedACounter-1
    			endif
    		else
    		LedACounter=0
    		low LedA
    		endif
    
    	If SwitchB=0 THEN 	' Closing SwitchB blinks LedB
    		If LedBCounter=0 then
    			Toggle LedB
    			LedBCounter=LedBBlinkRate
    			else
    			If LedBCounter>0 then LedBCounter=LedBCounter-1
    			endif
    		else
    		LedBCounter=0
    		low LedB
    		endif
    
    	Pause 10
    	Goto Main
    
    	end
    Just added a minor change (zeroing LedACounter and LedBCounter in the main loop), so if you missed it first time, copy the code again.

Similar Threads

  1. Run a string of LEDs from the mains
    By The Master in forum Off Topic
    Replies: 30
    Last Post: - 1st October 2009, 18:55
  2. Using LEDs as light sensors
    By skimask in forum Code Examples
    Replies: 3
    Last Post: - 30th December 2006, 22:19
  3. arrays LEDs Switches
    By ilteris in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 22nd October 2005, 03:05
  4. controlling leds with the switches
    By ilteris in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th October 2005, 21:02
  5. Can anyone help a beginner in a struggle?
    By douglasjam in forum mel PIC BASIC
    Replies: 1
    Last Post: - 5th May 2005, 23:29

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