Blink without the pause command


Closed Thread
Results 1 to 9 of 9
  1. #1

    Question Blink without the pause command

    Hi All,

    I want to blink a LED without the pause command because I want to do other things and not stop the entire loop every time the LED blinks.

    For example, with the pause command the code looks like this:

    main:
    high portb.0
    pause 500
    low portb.0
    pause 500
    goto main

    Without the pause command the code could look something like this:

    count_on var byte
    count_off var byte
    pause_led var byte

    count_on = 0
    count_off = 0
    pause_led = 5000

    main:
    if count_on = 0 then
    high portb.0
    endif

    if count_on != pause_led
    count_on = count_on + 1
    endif

    if count_on = pause_led then
    low portb.0

    count_off = count_off + 1

    if count_off = pause_led then
    count_on = 0
    count_off = 0
    endif

    endif

    goto main:

    My question is, if I want to leave the LED turned on for 1 second, how many "if" cycles should I do?

    Thanks!

    Daniel.
    Last edited by DanPBP; - 16th August 2007 at 18:29.

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


    Did you find this post helpful? Yes | No

    Default

    One thing and one thing only can be done at a time, that is until parallel or "quantum" computers hit the market.

    You will want to look at "interrupts" . That is how the impression of doing more than one thing at a time is accomplished. Even your desktop with all of the ram and cpu speed can only do one thing at a time - believe it or not.

    http://www.picbasic.co.uk/forum/show...ght=interrupts

    Look at the above link for a slick way of using interrupts with PICs or the manual for the old fashion way.
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default

    I agree with Dave. But the approach to take really depends on what it is you need to do in
    between the LED on\off periods, how long the LED should be on AND off, the oscillator speed,
    and which PIC you're using.

    Trying to work out the number of if-thens between toggling the LED on or off isn't a very
    good approach, since the time will vary as your code in between the on\off periods will
    change, and require recalibration with every change.

    More information would really help folks here to help you..;o}
    Regards,

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

  4. #4
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Another way would be to use a timer module.

    You can check the timer overflow bit and increment a variable at each overflow.

    Then, you can ON and OFF the LED at any desired time using this variable.


    --------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  5. #5


    Did you find this post helpful? Yes | No

    Default

    I'm using a pic 12F683 (internal oscillator) to turn on/off some lights on my R/C car...

    I'm reading the output from the receiver with pulsin and then I can turn on/off the lights...

    I'm using the pwm output to control the stop lights... Normally, they are 50% lit and when I hit the brakes they go 100% brightness...

    Also, I want to blink two yellow LEDs as turn lights, but I cannot wait while the LEDs are blinking because I need to know if something else happens, like hitting the brakes....

    So, that's why I wanted to know how to blink the LEDs without the pause command...

    I think I could do something with the "ifs", but as you said Bruce, will have to recalibrate the code...

    Thanks for your answers!

    Daniel.

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


    Did you find this post helpful? Yes | No

    Default

    I would go with Darrel Taylors' instant interrupts with timer1. Disable interrupts before using
    PULSIN. Then re-enable them after PULSIN. A few mS either way shouldn't be a big deal for
    a heart-beat type LED on/off.
    Regards,

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

  7. #7
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Lightbulb

    Hi, Daniel

    the easy way with R/C signals ...

    the servo signal is THE "missing timer" ... say the 1.5 ms is @ 40 Hz ( example )

    if you want to blink @ 2Hz ...

    just count the incoming 1.5 ms pulses and TOGGLE the led output for COUNT // 10 = 1,or 2 ... but not 0

    takes very little CPU time.

    hé,hé ...

    Alain
    Last edited by Acetronics2; - 17th August 2007 at 07:53.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  8. #8
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by danielhr77 View Post
    I want to blink a LED without the pause command because I want to do other things and not stop the entire loop every time the LED blinks.
    SNIP
    Without the pause command the code could look something like this:

    count_on var byte
    count_off var byte
    pause_led var byte

    count_on = 0
    count_off = 0
    pause_led = 5000

    main:
    if count_on = 0 then
    high portb.0
    endif

    if count_on != pause_led
    count_on = count_on + 1
    endif

    if count_on = pause_led then
    low portb.0

    count_off = count_off + 1

    if count_off = pause_led then
    count_on = 0
    count_off = 0
    endif

    endif

    goto main:
    Daniel

    I can suggest (without interrupts) the following

    Code:
    counter:  var   word
    Led:        var   PortB.0
    
    counter =0
    main:   if counter < HalfSecondValue then
                counter=counter+1  ' increment the counter
             else
                counter = 0
                Led = !Led      ' toggle every half second
             endif
             '  do whatever you have to do here
            ....
            ....
            goto main           ' go back to the top
    Here, HalfSecondValue is something you will have to determine either by counting the machine cycles or experimentally(easier)

    Jerson

  9. #9


    Did you find this post helpful? Yes | No

    Default

    Thanks to all for your help!

    I also found this from Darrel: http://www.darreltaylor.com/DT_INTS-18/blinky.html

    Regards!

    Daniel.

Similar Threads

  1. Delayed output 10 secs
    By lilimike in forum mel PIC BASIC Pro
    Replies: 37
    Last Post: - 14th October 2011, 06:28
  2. Old and beyond help ?
    By DavidFMarks in forum mel PIC BASIC Pro
    Replies: 46
    Last Post: - 11th December 2008, 15:23
  3. Replies: 11
    Last Post: - 12th July 2008, 02:36
  4. Fade out LEDs question
    By Sam in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 22nd June 2008, 10:50
  5. Help Quick Need to make code smaller
    By Programmednew in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 25th January 2005, 03:46

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