switches & LEDs ... my first attempt


Results 1 to 15 of 15

Threaded View

  1. #6


    Did you find this post helpful? Yes | No

    Lightbulb

    Hello,

    I generally use a fairly tight loop with small delays and counters.

    For the LEDs I use flash-pattern words.
    I start at bit 0 of the flash-pattern word and, if it's a ONE, I turn the LED ON.
    If it's a ZERO I turn the LED OFF.

    Each time I pass through the loop I increment the flash-pattern word's bit counter by one and do the check again.
    When the bit counter reaches 16 I reset it to 0 again (a word variable's bits are numbered 0 to 15).

    Currently, I have a project with 5 switches and 5 LEDs.
    Each LED has a flash-pattern word variable associated with it.

    It works well, the flashing is smooth, and does not require interrupts.

    If I want an LED to flash on and off quickly (50% duty-cycle), I load it's flash-pattern word variable with %0101010101010101. If I want the LED to flash on and off slowly (but also with a 50% duty-cycle), I load %0000000011111111. If I want it ON all of the time I load it with all ones. If I want it off all of the time I load the variable with all zeros. Basically, you have 65536 different flash-patterns to choose from and all you have to do is load one variable.

    The beauty of this is that each LED "seems" to be operating independently with a mind of it's own. Other LEDs' flash-patterns are unaffected when one LED's pattern changes. Also, you can define flash-patterns for different modes as constants. This makes your code much more readable.

    For instance...

    Main:
    If Button1 = 0 Then
    LED1Pattern = OPERATING_MODE
    Else
    LED1Pattern = STANDBY_MODE
    EndIf

    Pause 10

    Gosub UpdateLEDState

    Goto Main


    In the above example, "Button1" is the aliias for a switch input pin (active low). "Led1Pattern" is a word-sized variable the holds the flash pattern for LED #1. OPERATING_MODE and STANDYBY_MODE are 16-bit constants declared at the beginning of your program. And finally, "UpdateLEDState" is a subroutine (not shown above) that increments the flash-pattern bit-counter and then turns LED #1 on or off, depending on whether the current bit in "LED1Pattern" is a ONE or a ZERO. You could have as many LED flash-pattern words being "updated" in this routine as your RAM allows. I generally use arrays for my flash-patterns and then use a loop.

    Anyway, have fun and welcome to PIC programming!

    Last edited by picnaut; - 24th August 2005 at 17:12.
    ---> picnaut

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