Problem calculating the right scale and duty for dimming LED´s
Crosswinds
Posts: 182
Hi guys!
I have now got my other problems sorted with feeding issues and stuff!
Have come very far with the code for my Aquarium lamp controller. But ran in to a problem.
I have experimented with the "single-ended DUTY mode code" example from the propeller kit labs book.
It works really great with my LED´s through an ULN2803 (have this temporary setup on the demoboard while developing the code)
But when i tried to incorporate that code in to the real program, i ran in to a problem that has to do with the clockspeed.
In the example they have not given any directions for the prop on the clockspeed. But in the real code i need:
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
That give me very irratic behaviour from the LED´s. As stated erlier they work great when no speed is given in the code.
So my guess is that the scale and duty needs to be calculated differently when using full speed.
In the example the scale is:
16_777_216
and duty from 0 to 255.
How should i calculate this to work with my faster speed?
I have now got my other problems sorted with feeding issues and stuff!
Have come very far with the code for my Aquarium lamp controller. But ran in to a problem.
I have experimented with the "single-ended DUTY mode code" example from the propeller kit labs book.
It works really great with my LED´s through an ULN2803 (have this temporary setup on the demoboard while developing the code)
But when i tried to incorporate that code in to the real program, i ran in to a problem that has to do with the clockspeed.
In the example they have not given any directions for the prop on the clockspeed. But in the real code i need:
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
That give me very irratic behaviour from the LED´s. As stated erlier they work great when no speed is given in the code.
So my guess is that the scale and duty needs to be calculated differently when using full speed.
In the example the scale is:
16_777_216
and duty from 0 to 255.
How should i calculate this to work with my faster speed?
Comments
The demo will run on a Demo Board or QuickStart board. It's an advance version of the "Larsen Scanner," popularized by the Cylons of "Battlestar Gallactica."
Thank you very much for that code. And also help.
I hooked it up and edit the pins to fit my stuff.
But i actually experienced the same thing with that code. (well a slight different behaviour), but it sure dont sweep as i understand the example code does?
It also runs in the full clockspeed.
I am actually not in need for more than 2 channels (will have white LEDs and BLUE)..
Is there really no way to calculate the scale in a way that would fit the faster clockspeed? (I still think that should be the problem)
Finally, my object requires that the pins be contiguous; trying to write an object for multiple pins that are not grouped is possible, but would be very very messy.
What are the loads you're applying to the ULN? I use them all the time without problems; they will behave if you keep the output current in spec.
Hello again JonnyMac!
Oh i feel so stupid! I had forgotten the 10k resistor parallel from Prop-pin to ground. -.-
Hooked that up and everything works fine!
Guess it was just dumb luck that it worked with slow clockspeed.
Right now during on the development board. the ULN have only two units, drawing about 130ma each.
Im pretty sure after looking at your code that i will implement that on my version 2.0 code
Do you have any pictures or something on your project you talk about? sounds very very cool!
Best regards
Daniel
this is
- your complete code
- a detailed descritpion of what you experienced
I mean a description like
I set duty to 50% and expect the brightness to be 50% but I can clearly see it dropped already down to 5-10%. How can I code something that
I set a variable "brightness to 50% and the duty is adjusted in that way that the brightness is 50% whatever duty this means.
What do you mean by "scale". The thing described above? Frequency? a correctionfactor?
I got the impression that you are a beginner in coding. I apologise if this is wrong. So the bug might be somewhere else in the code.
Therefore it is very important to attach your complete code
keep the questions coming
best regards
Stefan
Hello Stefan.
No problem, i post the code here.
Please note that that is the exact same code as from the propeller kit labs-book explaining this stuff.
The only thing ive added is on the top:
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
that screws everyting up.
Edit: I was able to get away with that with the resistor. but that is probably covering something up!
Hope you understand me better now!
********************
CON
scale = 16_777_216
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000
Pub TestDuty | pin, duty, mode
ctra[30..26] := %00110
ctra[5..0] := 1
frqa := duty * scale
dira[1]~~
repeat
repeat duty from 0 to 255
frqa := duty * scale
waitcnt(clkfreq/128 +cnt)
On this page you'll find a photo of a project I did for Steve Wang, the guy who created the Predator and many other very cool movie monsters:
-- http://www.efx-tek.com/php/smf/index.php?topic=1585.0
(If you look closely, you'll see a Propeller Platform and my output board on the floor -- in the photo just to the right of my left calf).
This uses a 12-channel dimmer with TIP125 outputs so I can drive a lot of current. The dimmer code is the only PASM in the project, the other cogs (main to start everything, plus three background cogs that are written in Spin) handle the lighting. Each lighting control cog uses a state machine so that it can 1) start up, 2) run normally and, 3) shut down. On the Starcraft Marine I added an IR input so we could control it with a TV remote, though we disabled that for the display (Blizzard convention).
On Monday night we finished a display that will go public in Seattle next week. This one is for the game, Firefall, by Red-5 Studios.
-- http://vimeo.com/14628833
Steve's team made full-size Typhoon (male) and Morningstar (female) characters; another shop built a full-sized thumper that operates. The characters use my 12-channel lighting controller. The thumper (which is the master of the display) use the EFX-TEK Prop-2 (BASIC Stamp 2) to manage the show and the Propeller-powered AP-16+ audio player for sound FX. Again, I can't post photos until next week, but I will as soon as I'm able. It's easy stuff from the electronics side, but very cool form the client end; they loved that I could quickly change lighting behavior and add sound effects to the display.
Hello!
Yep! Will have a look at the code for the next version! Seem really really handy.
But for now i need to keep the code that i have written because of the time.
Eventhough i came around the problem with the resistor, it would be fun to know how to calculate the "scale" and "duty" to fit the fast clockspeed.
Did you have a chance to look at the code-snippet i posted?
I also noticed that to have the smooth transition between high and low light, i have to scale down the duty. For example. 115 is the lowest, and 140 is the highest as it is now. But at least its smooth!
Then again, i would want it to work exactly as the example in the book. With 255 brightnesses.
Could this have something todo with the ULN-circuit for some reason? Im pretty puzzled.
Oh and btw. Your projects is extremely cool. Do you do this for a living?It must be real exciting!
By Blizzard, do you mean Blizzard the computer game company?
Best regards
Daniel
Yep! That statute was of Jim Raynor, the main protagonist of starcarft 2 wing of liberty!
The SCALE constant is not dependent on clock speed, it is dependent on span of brightness. The demo you're using (from PEK book) assumes a 0 to 255 brightness level so the scale is calculated from that (2^32 / 255). If you wanted a 0 to 100 range you would have to recalculate.
Keep in mind that not all LEDs will behave nicely with modulation that can run as fast as this does. You may need to use a cog and PWM loop -- but then, it would be easier just to use the code I wrote (and have used professionally) and save yourself a lot of trouble.
I've attached a little demo that uses the code from PEK with a method called set to make the code clean. I also converted it to 0 to 100 scale so you can see the difference. Note that with signed numbers you have to use POSX (31 bits) divided by your maximum value, then multiplied by two for correction to 32 bits.
Note, too, that LED brightness is not always linear; you may need to create a lookup table to get the kind of output from your LED based on your desired input range; this is especially true with very high frequency modulation as is the case with using counters.
My primary interest in the film industry is acting, though I do make extra money on the side working behind the camera from time-to-time.
That's a different scale value -- perhaps I misunderstood the question. Okay, let's say you have determined that when you feed 0 to 255 to the LED you really want it to go 115 to 140 (not much of a range). You can convert the first range (0 to 255) to the second (115 to 140) like this:
This scales the full range of the LED control to your restricted range (mx + b equation). When level0 is 0, br0 will be 115; when level0 is 255, br0 will be 140.
That code is really really helpful. Its also simple and clean, and i can clearly see how to incorporate that in to my code that i already have!
Will work on this tomorrow when i get more time!
The experience mainly i get when i use the code from the PKE book and my leds. Is that when i use the fast clockspeed. I get an irratic behavior when it scrolls from 0 to 255. On that range it have "spots" that actually dims the brightness again. So when it scrolls fast, it looks like the LED´s dims up, then down, then up, then down, and finally dims up. That does not happen with the slow clockspeed. I dont know why! But with your nifty code i think we can get around this?
Also in the finished system, i will probably have an adjustment feature to make the user (me) choose from what and too brightness it should travel for the two separate rails. Because i have no idea how it will work with the fish tank, and how it look! (I do know however, that my armature that i built and the leds i have put in, i will have enough light)
I may take some flack for this, but code from the PEK book is instructional, though not always ready for "prime time." Again, the problem with the "set-and-forget" mode of the counters is that you have NO CONTROL over the modulation frequency, and some pulses generated may be too fast to allow your LED module to light properly. The way around this is a little code -- which I've attached -- that allows you to control the duty cycle AND frequency. This lets you control the system behavior for your LEDs.
Use the attached code as a starting point for your project and I think you'll be in better shape. I'm running this on the QuickStart board and the LEDs are behaving much more nicely with a 500 Hz modulation frequency than with the uncontrolled frequency of the counters-only approach. Note that version also uses the counters, but it contains them in a synchronized loop to gain control over modulation frequency. Setting the ticks for duty cycle is also a lot easier to understand.
Hello again!
Firstly, thank you so very very much for your time and help with this. You have been just great.
And this last code you posted, seem to be exactly what i need. It´s clean and simple to incorporate.
Just have one concern, and excuse my ignorance on this, but will the new cog adapt the clockspeed wich in this case will be the fast one. Or can it run on its own speed?
I ask because i understand that use another cog to come around the fast clockspeed messing with the frequencys, right?
Sorry if i misunderstood something, but just want to be 100% sure of what im doing here, usually a lot easier down the road
If you look at the constants section at the top of the program (hint, hint) you'll see that I am in fact running it at 80MHz (16x PLL with 5MHz XTAL) and the PWM frequency is determined by your clock settings and desired PWM frequency (PWM_FREQ constant); you set the frequency (presently at 500 Hz), the code takes care of the rest. As ever, I try to write my code to be as clock frequency agnostic as possible.
This will be very interesting to try out! Will the 500hz correspond to the speed that the prop is running with speed unset? (uncertain what speed it runs on when nothing is set)
You really need to set the speed; I believe not setting it puts the Propeller into RC mode and it will run very slowly. You may have noticed that my programs have a similar structure; the reason for this is that I start with a clean template that takes care of a lot of the basics. This can save a lot of headaches.
It will be nice when the Propeller Tool [finally] gets the ability to use a template file when File >> New is specified (yes, I'm aware that BST has this feature).
As Marko points out below, this feature is now available in Propeller Tool 1.3.
Jonny, i have no way to say this. But you have singled handely solved all my problems with this.
I have just now tried out your code with my LED's and it works like a charm. For some wonderful reason, the LED´s that i had problem dimming (4 stayed on much longer then the rest on the card) have also been solved. I have no explanation for this. But im so excited it almost brings tears to my eyes! Haha!
This is just great. Can put the finishing touches on my code and implement this, and i will atleast have a lamp-control beta version, that will run on my desk for a couple of days!
Again, thank you for all your help man.