PWM LEDs at different rates

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

PWM LEDs at different rates

Post by RobH »

I'll admit my programming skills are a bit lack but I know enough to almost be dangerous. That being said, I've banged my head on this for over 2 weeks to no avail. How in the world do you make 1 LED pwm at 50% while fading another one on or off? Assume LEDs are NOT connected to the ccp pins. So, software pwm only? Thanks in advance!

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

Hi Rob,
If you post what you have done so far, then I will see if I can modify flowchart, so it does what you want it to.
If you can let me know the ideal frequency hand how many LEDs.

Martin
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

It was a generic question about how to make software pwm work in FC6 when you aren't using the specific pins for that purpose on your chip, but I can post a sample if you need it. Pic16f917@internal 8mhz. I may have figured it out but teaching myself is slow going. I'll try my latest idea tonight and possibly upload something tomorrow if it works. Be warned, it'll look hideous! :)

edit:

I got it to work finally! I brute force/hacked it. I don't know how to use the PWM component, so i just used some nested loops, decisions, and some trickery. :D

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

Hi Rob,
The PWM component is by far the easiest and best method to use!
But you can only use as many LED's as there are CCP channels
There are plenty of examples of flowcharts containing PWM components and we can help you of course.
If using software method i.e. bit banging, then a timer interrupt is used.
The timer interrupt frequency has to be 100 time the minimum non flicker frequency.
E.g if you decide the LED should run at 50Hz, then timer frequency should be 50*100 = 5KHz
Using 100 times min frequency will allow you to use a resolution of 100% duty
all the LED's are are enabled at the same time e.g when count variable is = 0
then on accessing the timer interrupt macro (also known as interrupt service routine or ISR for short) count is increment by 1.
When count = variable for the LED in question then the LED will be switched off.
Since you can have different variables for each LED then the brightness can be different on all LED's simultaneously.

By using either of the above methods and not loops, then all your other programming within main will not be effected. e.g using LCD display or halting at a switch input.
The interrupts or PWM modules just carry on regardless therefore would be the the ideal choices.

If you like I can create a flowchart using both methods when I get the chance so you can see how both methods would work.
The reason I wanted you to post a flowchart is so i know what set up your using. E.g target osc speed, output ofr LEDs etc.
Does not matter how
RobH wrote:hideous
the programming looks like.
We have all been there so we know what it's like as a newcomer.

Martin
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

You're right, and I'm glad to know there are interrupts available to use (I didn't use them because I do not know how, I tried). I'm including my loopty-loop (unfinished) code so you can have a laugh, but hey, it works well enough. Keep in mind there are only 3 macros in the unfinished code. There would have been a total of 9, but I'm waiting to hear about how I did it the hard way, etc. :lol:
Attachments
16F917_test.fcfx
loopty-loop. Weeeeeee!
(76.73 KiB) Downloaded 230 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

Hi Rob, due to limited time (Learning C :-) ), this is work in progress.
What this flowchart will do is light 7 LEDs at a different brightness, and the 8th LED will fade off and on.
You will need to add all the other 24 LEDs within timer0 macro
This is the result of drive across 8 of the outputs connected LEDs:
1 Individial duties.png
(109.7 KiB) Downloaded 4329 times
You will be able to control the 31 LEDs independently.
Hope this is kind of what you're after?

If you was wondering what

Code: Select all

LED_Brightness[31]
means then the name of the variable is called LED_Brightness
the [31] is the amount of elements within the array.
What is means is you can have 32 different variables assigned to LED_Brightness at the same time.
As you can see within the flowchart I have assigned 8 elements:

Code: Select all

LED_Brightness[1] = 12
LED_Brightness[2] = 24
LED_Brightness[3] = 36
LED_Brightness[4] = 48
LED_Brightness[5] = 60
LED_Brightness[6] = 72
LED_Brightness[7] = 84
LED_Brightness[8] = 0
I have not used element 0 to avoid confusion
so LED_Brightness[1] = LED1
LED_Brightness[2] = LED2 etc.

I can help you further if you get stuck.
Attachments
16F917_test v2a.fcfx
(39.19 KiB) Downloaded 218 times
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

You have an error - unknown or missing variable: Direction
I added it to the file and am playing with your code now. Thanks!

edit:
I've spent the last 2 hours breaking your code. I was wondering if you could point me in the direction of allowing porte.0 to be an input (already configured in the fcfx file) and read ambient room light and adjust LED brightness across the board for all LEDs (fading LEDs would fade to and from the brightness level). I've tried a few different things, but i get flickers at regular intervals. BTW, I have an ambient light sensor connected to porta.3 (output) and porte.0 (input). That feature works in the code I posted above.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

RobH wrote:You have an error - unknown or missing variable: Direction
Yes,
Sorry about that,
It also gets complicated for me as I have not got same hardware as you, so I'm having to use multiple files as I'm testing on hardware first.
it was late for me and did not add the Direction variable to your file but added it to mine.
I will compile your file the next time before posting it.
Also I will post both file, just in case there is a simulator issue in the future.

I had started again and ran out of time so I thought I would show the concept of being able control individual LEDs
I will add the brightness tonight.
is it full brightness when ambient sensor see a lot of light to say 10% duty if no light?
Can you temporally set LEDs up so they display the the variable value when:
a) There is the brightest light and
b) The lowest level of light so I can allow for the whole range
RobH wrote:I've spent the last 2 hours breaking your code
Do you fully understand how it works now?
If you wish to control all the LEDs at the same brightness and all on or off at the same time, then Flowchart can be simplified a bit more.
Current it's set up so
RobH wrote: How in the world do you make 1 LED pwm at 50% while fading another one on or off? Assume LEDs are NOT connected to the ccp pins. So, software pwm only?
Martin
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

medelec35 wrote:
is it full brightness when ambient sensor see a lot of light to say 10% duty if no light?
Can you temporally set LEDs up so they display the the variable value when:
a) There is the brightest light and
b) The lowest level of light so I can allow for the whole range
Martin
Yes, LEDs will run at full brightness in bright light and low brightness in low light. I had 25%, but 10% sounds good too. I originally thought I had a 50% duty cycle on my original file and those looked good. I'll play around with different brightnesses and see what I like best. I used 127 for the halfway point when reading the input. Anything above that would run the LEDs at full brightness; anything below that would run them at low brightness.
medelec35 wrote: Do you fully understand how it works now?
If you wish to control all the LEDs at the same brightness and all on or off at the same time, then Flowchart can be simplified a bit more.
Current it's set up so
RobH wrote: How in the world do you make 1 LED pwm at 50% while fading another one on or off? Assume LEDs are NOT connected to the ccp pins. So, software pwm only?
Martin
I understand how you set the brightnesses of the LEDs and I understand how you looped the fade up/down for the one LED, thanks. I didn't understand it enough to successfully insert my own code into it to set the brightness levels based on ambient room light. It either didn't work at all, or flickered , or ran slower. Here's basically the code I was interjecting at various places in your flowchart, I even tried to add it in as a second timer interrupt:

ambient light level = 127
turn on porta.3 // ambient light sensor anode on porta.3
Get brightness // ambient light sensor cathode on porte.0
If brightness > ambient light level
no: LED brightness = 10
yes: LED brightness = 100
turn off porta.3

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

RobH wrote:ambient light level = 127
turn on porta.3 // ambient light sensor anode on porta.3
Get brightness // ambient light sensor cathode on porte.0
If brightness > ambient light level
no: LED brightness = 10
yes: LED brightness = 100
turn off porta.3
No problem, I can sort that out.

I have noticed you have not no hysteresis.
Around the change point you will get flickering as well
So perhaps we can use If brightness > (ambient light level +9) then LED brightness = 100
and also include brightness < ambient light level then LED brightness = 10
That gives a hysteresis of 10 or there abouts.
if the value falls in between then there are no changes.
It will be a bit like a window comparator.

The values can be changed after some experimenting.
Hopefully will get some time tonight to post a flowchart.

Martin
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

medelec35 wrote:
No problem, I can sort that out.

I have noticed you have not no hysteresis.
Around the change point you will get flickering as well
So perhaps we can use If brightness > (ambient light level +9) then LED brightness = 100
and also include brightness < ambient light level then LED brightness = 10
That gives a hysteresis of 10 or there abouts.
if the value falls in between then there are no changes.

Martin
I didn't even think of that. That is a good idea, but should not have affected my testing last night.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

RobH wrote:I didn't even think of that. That is a good idea, but should not have affected my testing last night.
No I don't think that would be the issue as I would assume testing light would either be on/off and not fading.
I will test hardware before posting.
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

Hi Rob,
Attached Flowchart works well for me.
I have made some alteration so you can see how to turn on/off (hence alter brightness) of multiple LEDs
You will have to add any LED's not already added.

Now you will have seen how to control sing or multiple LED's simultaneously,
hopefully you will have enough to go by.

I was a bit puzzled to why you connect the anode to pin A3 then after making A3 go high read ADC value.
I can only guess it's so save power?
So I have modified flowchart so that A3 goes high, then ADC is read, then A3 goes low every 2 seconds.
Of course if this function is not required it's very easily removed.

Any way after increasing voltage on port E0, the LED's I have connected to portD all go from dim to high.
there is 0 flickering. all LED's are solidly dim or solidly bright depending on E) voltage.

If your set up is flickering then your hardware will need looking into.
Perhaps if you have one, you can post a circuit diagram?

Martin
Attachments
16F917_test v2b.fcfx
(42.55 KiB) Downloaded 196 times
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

Yes, I added turning off porta.3 after reading voltage on port3.0 to the list in my previous message to save power. I assumed it wouldn't need to be on all the time.

OK, wish me luck on combining your two examples. Ultimately the list of challenges for me on this learning experience will be:

1. learn how to use an interrupt for individual LED brightness
2. learn how to make a light sensor change max brightness of the entire group of LEDs
3. how to turn individual LEDs in the group on and off via fading.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

RobH wrote:OK, wish me luck on combining your two examples.
Yes, good luck in deed.

Did you try the second program on its own to see if LED's are solid and are either bright or dim, depending on light level of the room?
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

I just now had the chance to try your program on hardware. The lights did not fade as intended. I may have to set up an lcd to show me some human readable info (that, I can do!).

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

Yep, sounds like your not getting correct values for light detection.
There are three methods I can think of.
1) is what you suggested i.e send value to LCD.
2) Send values via RS232. I like that method as you can send loads of different variables with text.
3) send the value to the whole port b. This is the last resort (as observing values in binary can be confusing) if options 1 or 2 are not available.

Ah just re-read your post:
RobH wrote:turn on porta.3 // ambient light sensor anode on porta.3
Get brightness // ambient light sensor cathode on porte.0
The potodiode must be reversed bias (not forward biased as in your description!)
So one way is to connect the cathode to A3 a resistor between the anode and 0V.
Then connect E0 ADC to the junction of the resistor and anode.
If you connect the diode so its forward bias and there is no or a low value resistance then you can damage your diode and / or microcontroller.
Also if forward bias you will only be reading the forward drop of the diode.
So the voltage across ADC will be about 0.7V (depending of the forward current)
Therefore the ADC reading will be about 255/5*0.7 = 35
So the room will always be detected as dark.

If you have a multimeter, you can measure the voltage across the E0.
But it will be best to either remove the on/off of A3 or connect the cathode directly to +5V first.

Martin
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

The light level sensing in the original code I posted in this thread works, but I didn't turn a3 on and off. It was on all the time. I tried that in your latest code variation to no avail. Also, I've noticed that using times, count, and loops bigger than 255 on this chip confuses it. Therefore, I also changed your timing on count2, but that didn't help either. In fact, I re-enabled some of the other functions and set LED_brightness[8]=100 and made it cycle like the other outputs you have on the timer macro, and it wouldn't even come on.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

I just found an eblock with a photodiode on it.
With the light on ADC was only reading 46.
So I changed my variable to

Code: Select all

ReadLightLevel > 33
and

Code: Select all

 ReadLightLevel < 30
Works perfect when I switch main light on and off, all LED's dim with the light off, and go to full brightness with the room light on.
So obtaining a value of ADC with light on/off is by far the best way to go forward.
Of course your values cold be completely different as it will depend on photodiode & resistance value + different room lux values.
I would be interested to know what your readings are.

Edit:
RobH wrote: Also, I've noticed that using times, count, and loops bigger than 255 on this chip confuses it.
Not in this case.
I'm using a signed integer and that will allow me to use a number up to 32767 with no issues at all.
As you can see from above, i have it working with a photodide.
I believe you're referring to loop count function which will only allow a maximum number of 255 which is a totally separate issue.
Martin

User avatar
RobH
Posts: 95
Joined: Wed Sep 11, 2013 7:51 pm
Has thanked: 29 times
Been thanked: 17 times
Contact:

Re: PWM LEDs at different rates

Post by RobH »

I'm using an "ambient light sensor":

http://www.radioshack.com/product/index ... d=22472186

Not quite sure why it works for my code, but not yours on the the same chip.

*edit* - I'm putting this one to bed. I get different voltage readings of the light sensor on the same hardware between the two different files. I'll chalk it up as a hardware design issue until I can further investigate. Too many projects on the books right now to worry about only this one. I truly appreciate your help on this matter and may revisit it in a few weeks.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: PWM LEDs at different rates

Post by medelec35 »

No problem.
The ambient sensor in your case is just a phototransistor and not a photodiode.
When you're ready we can go though the hardware step by step, so for then if you can have a circuit diagram ready that would be really helpful.
It should be fairly straight forward to sort out as the issues does sound like its to do with the phototransistor and/or ReadLightLevel value.
A quick test on the rest of the circuit would be just to remove connection for sensor to E0, then connect E0 to =5V then connect to 0V.
any way when you are ready, you can give me a shout if you like.


Martin
Martin

Post Reply