PWM pulsing LED

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey guys.

 

I want to make my LED turn on and off with 1 sec period time, with my 16 bit timer (0.5 sec on and 0.5 sec off). But I also would like to increase and decrease the LED's brightness while turning it on and off. Can you guys please give me a hint how could I do this?

Last Edited: Sun. Oct 15, 2017 - 01:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Generate interrupts on your PWM timer cycle.  In the associated ISR, count those interrupts and when you reach your 0.5 sec marks, turn on and off the LED by connecting/disconnecting the output pin from the timer.

EDIT: maybe you mean you want to fade your LED on and off.  Put that logic inside the ISR, increasing or decreasing the PWM value based on what point in the 1-sec cycle you are at (that is, using the count value you are updating in the ISR).

Last Edited: Sun. Oct 15, 2017 - 04:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for your answer. I did manage to turn the led on and off every 0.5 sec, but when I turn on the led I would like to increase its brightness, and when 0.5 sec has passed I want to decrease it to 0%. Then do it again every 1 sec. 

 

 

I am just not sure how can I do these 2 things at the same time.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I hinted at how you can do it in my EDIT section.  First, remember that to remove observable flicker, an LED must be refreshed at least 100 times/sec.  Let us say that you set up your PWM cycles to run at e.g. exactly 5ms (200/sec).  This means that your half-second off will be 100 PWM cycle counts, and your half-second on also 100 counts.  Assuming you start ramping on at the beginning of your 1-second period, pick a ramp rate e.g. 200ms (40 PWM cycle counts).  So with your PWM cycle counter (this will be a one-byte variable - able to hold your max count of 199 - which you increment and roll over in your ISR), when the value is between 0 and 39, increase your PWM value to fade in.  This increase can be linear or non-linear as you see fit.  Then keep it steady until the counter reaches 100.  Now between 100 and 139, decrease the PWM value down to 0, and begin all over again when the counter would increment from 199 to 200, at which point your code will reset the counter to 0.

Last Edited: Sun. Oct 15, 2017 - 09:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You might also consider setting up a Timer/Counter in CTC mode, to generate an interrupt once every 5 mSec, (or whatever).

 

Then inside the ISR you can either set a flag to recognize and process within a Main Loop, or for a simple LED program, just put the processing within the ISR.

The processing is maintaining a variable or two for the current PWM Duty Cycle, and then number on interrupts into the second, and an up/down flag.

In the ISR adjust the PWM duty cycle and adjust your variables.

 

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you ;)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this a 1-LED version of "cyclon eye" and/or "kitt"?  It might be "cylon eye"?  In any case, Forum searches with those terms and variants will bring up dozens of prior discussions.

 

To start your thinking:  For an LED to look like it is dim (when driving either on or off), that period must be faster than they eye can register.  Let's say 100Hz or faster -- 10ms period.

 

So you set up your timer to do "stuff" every 10ms.  Co9unt those periods, and the count is "state" in your state machine that determines what action to take during that state.

 

When you build this into your Halloween mask, better take care to be able to shut out the glow from the world, as [apologies to the Eagles]

 

You can't hide your Cylon eyes
And your smile is a thin disguise
I thought by now you'd realize
There ain't no way to hide your Cylon eyes

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Tue. Oct 17, 2017 - 03:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is this a 1-LED version of "cyclon eye" and/or "kitt"?  It might be "cylon eye"?  In any case, Forum searches with those terms and variants will bring up dozens of prior discussions.

Larson scanner is the name you're looking for.

 

There ain't no way to hide your Cylon eyes

Ah, brings me back ;-)

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]