Need Help With Inside For Next Loops and PWM
DigitalDj
Posts: 207
What I am trying to do is use the program i posted below as just a simple program with 3 loops and the PWM command(modified parallax program) to go through all the possible colors of a RGB Led that it can produce. First of all I am having problems with the For Next and being able to run them inside of each other. What i want to do is have a intensity of each led to be 0 to 255 then i want the For Next for loops to get all possible mixtures of color. What i am trying to say that i want the inside For Next to go 0 to 255 then the next loop to step 1 and the inside loop 0 to 255 then the next loop to 2 then the very outside loop to·step to·1 so on and so on.
I got to thinking more about this and i'm not sure if that will do the trick! Here is a website with a RGB Color Chart with RGB values.
http://www.tayloredmktg.com/rgb/#OR
Any Suggestions is appreciated!
Kevin
Post Edited (DigitalDj) : 4/4/2006 12:56:22 AM GMT
I got to thinking more about this and i'm not sure if that will do the trick! Here is a website with a RGB Color Chart with RGB values.
http://www.tayloredmktg.com/rgb/#OR
Any Suggestions is appreciated!
Kevin
Post Edited (DigitalDj) : 4/4/2006 12:56:22 AM GMT
Comments
You need to put ALL of the PWM commands in the inter-most loop.
Of course this will take 16 Million loops !!!
I would suggest using a larger step value. Step -16 for each loop will result in 4096 colors.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
Post Edited (Bean (Hitt Consulting)) : 4/4/2006 1:31:06 AM GMT
Thanks,
Kevin
Post Edited (DigitalDj) : 4/4/2006 2:29:14 AM GMT
When you have trouble debugging something, the key is to simplify. Instead of trying to get all three PWM loops working simultaneously, write a version of the program with a single PWM loop. If you can get the single PWM working inside a loop (with the LED getting brighter and darker), then start adding back the other PWMs. If you can't get a PWM working in a loop, go simpler. For example, create a program that just starts up, sets a PWM value, and then goes into an infinite loop. If that works, try it with a different value for the PWM and check that the LED is brighter or draker as expected by the new value. This at least proves that the PWM command is working as you expect it to.
The basic idea is to keep reducing to something simpler and simpler until you get the first success. After that, start adding back complexity until it fails and then you can zero in on what might be causing the problem.
Thanks, PeterM
Kevin
·
You said the PWMs aren't"holding their values." How much time are giving them to hold their values? In looking at your original program (and guessing the new version looks basically like Bean's), I see a CPU running at full speed through a series of loops and updating the PWM command as fast as possible.
Without some mechanism to wait a finite amount of time between each PWM update, you'll never get them to "hold their value" since you keep changing it as fast as you can. You need some sort of constant timebase to let you update the PWM values at regular intervals, like say 10 or 20 times a second.
Thanks, PeterM
Can you post a schematic ? You mentioned an RC circuit, so I assumed the PWM output was buffered. If not, you will need to use interrupts to control the 3 PWM output simultainously.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·
I have The output from RB0, RB1 and RB2 going to fets inside a light module with rgb leds. Explain what i need to do with the interupt and how that effects the pwm. I don't want to use a RC network because (correct me if i'm wrong) i want to control current not voltage!
I guess i need to understand more about how the pwm with the SX works and how to program it.
Kevin
· Okay, let's not try to get too fancy right off the bat.
· Try this program and let me know if it works...
· It should take about 9 seconds to cycle through 4000 colors.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·ranges for the light output·and current·for your LEDs.
Chris I.
I see the color changes but the leds are still flashing instead of one continues on time of·color changing. Does that mean something is not holding values or is the pwm not fast enough so you don't see the leds flash. I believe anything above 1KHZ would be fine.
Kevin
Try running at 20MHz or 50MHz.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·
How do i need to do that? I have the SX Key plugged in all the time so i need to take and disconnect it and install the 50mhz resonator, correct? What about the code?
Kevin
No the SX-Key will provide the 20mhz or 50mhz (or whatever frequency you require). Just change the DEVICE line from "OSC4MHZ" to "OSCHS1" and change the FREQ value.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·
I changed the clock from 4MHZ to 50, 20, 1 & 1KHZ and the same all the way through except for the speed of the pulsing leds.
Kevin
PWM PWM's the pin while that command is running, yes?
It's not a background thing, and, if there's no circuitry holding the PWM'd value (tricky for a LED, without buffering), then each LED will only come on (at the selected brightness) while its PWM command is running. When the other LEDs are being PWM'd, it'll be off.
Wouldn't ditching PWM be the right solution, and, in the inner loop, roll your own 3-channel PWM. Count from 0 to 255, and, if the counter's less than each LED value, turn that LED on, else turn it off.
All LEDs will be off outside the central loop, so don't spend much time out there...
That, or do this properly, under timer interrupts, and it'll all work seamlessly.
Steve
· Great idea. That's what I did. Look at the RGBPWM.SXB program I posted.
Kevin,
· You do realize that the LEDs will flash because the loops go from 0 (completely off) to 255(completely on) ?
· I just hooked up 3 LEDs to RB.0, RB.1, and RB.2 and ran the program at 4Mhz and it seems to work.
· In fact the inside loop (green) run too fast and seems to be at a constant brightness. So I changed the step for green to -1.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·
Changing the step made a big difference, now changing the frquency helps also.
I guess i don't understand how this works without the pwm command! Can you explain?·Something else that would·be nice with this is not for it to fade into the colors each time but when it got it's new values then it would show the color and hold a few seconds then generate the next. The other thing i would like to do is use the decimal color chart above and step through the colors.
How hard is it to generate true PWM say at 1KHZ square wave on the 3 outputs?
Thanks,
Kevin
Post Edited (DigitalDj) : 4/6/2006 2:26:15 AM GMT
It allows you to make a "list" of colors to cycle through.
Be sure to run it at 4Mhz.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
from a color chart. How can i put a hold in to keep the color on for a few seconds? I've tried some things but didn't work, it makes the colors flash on and off when changing.
Can you explain what is going on in this program? I take it this doesn't use pwm but what would the waveform from the output look like? I don't have a scope at home and i was going to borrow one from work for the weekend and forgot.
How hard is it to program the true pwm with square wave under interupts for the 3 outputs?
Thanks,
Kevin
·
Thanks,
Kevin
Do you have an Sx-Key or an SX-Blitz? If it's the key, have you tried stepping through this code in the debugger? The best way to figure out what a program is doing is to use the debugger (or SxSim) to see what is going on. It sounds to me like you're blindly making changes, compiling, running the program, and then trying to guess what it happening. If you spend some more time learning the tools, you can make your life a lot easier.
Anyway, on to Bean's program. He has a main loop that controls everything, and a sub-routine that handles controlling the LEDs. Note the first line of his main loop is to read the four items from the DATA statement. These are the color values from red, green, blue, and how long to display the color for. The main loop then sets up a for/next loop using the duration it just read from the data statement and then calls the sub-routine named "UpdateLEDs".
In "UpdateLeds", the first thing that happens is that all the LEDs are turned on. Then there is a loop set up that counts from 0 to 255. In that loop, there are a series of IF/THEN statements which compare the value of the loop counter ("cnt") to the duty cycle for each color. If "cnt" = the duty cycle for that color, the LED is turned off. If not, if falls through to the next If/THEN, and ultimately back to the top of the "cnt" loop. When "cnt" = 255, the loop is done and control returns to main.
Back in the main loop, "durCnt" is updated and "UpdateLEDs" is called again,. This continues until "durCnt" equals the duration value read at the start of the main loop. When it's done "pos" is increment by 4, and the control goes back to the top of the main loop, where it reads the next four items in the DATA statements. Control then falls to the "durCnt" loop and the while process continues again.
So, as written, by changing the last value in any row of the data statement (the duration value), you can make any given color display for a longer or shorter time. 255 is the longest time available, so you can only get shorter than this current DATA table.
The loop that updates the LEDs is turning on the LEDs at the start, then turning each off at the end of the duty cycle duration, and the repeating a total of 255 times. The longer the duty cycle for any color, the longer it's left turned on. The shorter the duty cycle, the quicker it's turned off. This is PWM, just done in a hard coded loop.
In "updateLEDs", the code loops 255 times, and turns each LED on for some number of those 255 loops before turning it off. If duty cycle = 255, the LED is on for 254 of the 255 cycles, which means it's very bright. If duty cycle is 127, then the LED is on for half the time, and off for half, which makes it half brightness. Smaller and larger numbers scale the brightness accordingly.
Thanks, PeterM
Post Edited (PJMonty) : 4/13/2006 5:02:58 AM GMT
So i take it that the 4th number in the data control is the on time of the leds?
I want the leds to stay on a little longer but have tried a couple of things but no luck, everything except a·for next to create a delay.
With Bean's program would you know what the output would look like on a scope?
What i would prefer to do is pwm squarewave at about 1khz for the 3 outputs and if posible be able to change frequency to the different outputs. One other thing i would like to try sometime is to control the amplitude of the squarewave which would be considered amplitude pulsewidth modulation(but not a have to). I know i need to learn to program first.
Thanks,
Kevin
Post Edited (DigitalDj) : 4/14/2006 2:37:42 AM GMT
Yes, the fourth number is the "on" time. Bean's not much for comments in his code, but his variables provide clues as to his intent. This line:
Reads the "color table" with an offset of "pos" (short for "position" as in "position in the table"). The first three things it reads are all called "duty" followed by a color. The duty in this case refers to duty cycle which means the percentage of on time to off time. The last thing it reads is "duration", which refers to, well, the duration that each color is lit for.
It's tricky to read someone else's code, so I imagine you looked at it as one big lump without comments and kind of scratched your head. As obvious as it may sound, the key is to take it a line at a time and step through as if you are the computer. Since Bean used good variable names, his intent could be inferred with some deductive reasoning as long as you didn't get overwhelmed at the start.
Regarding longer on time, the way Bean has built his code, the duration is as long as it can be without changing the code. The key here is to extend the duration to be 16 bits instead of 8. This means the read statement would look something like this:
and the data statments would increase to something like this:
"Duration" is now split into a low byte and a high byte. The low byte works the way it does now, with a maximum duration of 255. The high byte ("durHigh") can be thought of as the number of times you want to execute the low value.
Finally, the loop would change to incorporate the high byte of the duration by doing something like this:
Note that "durCnt" is now also split into two variables called "durCntL" and "durCntH", which refer to the high and low bytes. Do you see how the inner loop is still the same, but now we wrap that in a loop as well? This lets you multiply the time of "durLow" by "durHigh", thus extending your duration by up to 255 times.
There are some "gotchas" with the code as written. For example, if "durhigh" is 0, then that outer loop will never execute, which means the inner loop will never execute, even if it has a value of 255. However, play with it and see if you can make this work. I think that there is a limit on the size of data tables in SX/B, so now that each DATA line has 5 elements, it might reduce the number lines you can have. I'm not an SX/B guy though, so don't quote me on that. Also, you'll have to change a variable name or two, and add a variable or two, but, as they always say in books, "I leave that as an exercise for the reader."
Finally, the waveform from Bean's code would look like a square wave where the overall duration of each wave is constant, but the amount of on time to off time varies.
Thanks, PeterM
Thanks for explaining my program. I'm not very good at interfacing with DNA [noparse];)[/noparse]
I'll try to force myself to put more comments in the examples I post here.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·
··=========================================================================
'
'·· File...... RGBPWM2.SXB
'·· Purpose... Generate RGB color patterns from data list
'·· Author.... Terry Hitt(Modified)
'·· Compiler... SXB Version 1.42.01
'·· E-mail....
'·· Started...
'·· Updated...
'
' =========================================================================
'
' Program Description
'
' Generate RGB color patterns on RGB leds
'
' Device Settings
'
DEVICE·· SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ···· 4_000_000
'
' IO Pins
'
LEDBlue··· VAR RB.0
LEDRed···· VAR RB.1
LEDGreen·· VAR RB.2
'
' Variables
'
dutyRed··· VAR Byte
dutyGreen· VAR Byte
dutyBlue·· VAR Byte
cnt······· VAR Byte ' Used by UpdateLEDs routine
pos······· VAR Byte
duration·· VAR Byte
durCnt···· VAR Byte
durLow···· VAR Byte
durHigh···· VAR Byte
durCntH···· VAR Byte
durCntL···· VAR Byte
' =========================================================================
· PROGRAM Start
' =========================================================================
UpdateLEDs SUB
'
' Program Code
'
Start:
· OUTPUT LEDRed
· OUTPUT LEDGreen
· OUTPUT LEDBlue
· pos = 0
Main:
· READ colors+pos, dutyRed, dutyGreen, dutyBlue, durLow, durHigh
· IF durLow = 0 THEN
··· pos =·0
··ELSE····· '_ELSE1_ Is Not Defined
· FOR durCntH = 1 to durHigh
······ FOR durCntL = 1 to durLow
··········· UpdateLEDs
······ NEXT
· NEXT
· END IF
· GOTO Main
'
UpdateLEDs:
· LEDRed = 1
· LEDBlue = 1
· LEDGreen = 1
· FOR cnt = 0 to 255
··· IF cnt = dutyRed THEN
····· LEDRed = 0
··· ENDIF
··· IF cnt = dutyBlue THEN
····· LEDBlue = 0
··· ENDIF
··· IF cnt = dutyGreen THEN
····· LEDGreen = 0
··· ENDIF
· NEXT cnt
· RETURN
'
colors: ' Red, Green, Blue,·durLow, durHigh·(duration of zero durLow ends list)
· DATA 255, 0, 0, 255, 255
· DATA 255, 0, 255, 255, 255
·
· DATA 0, 0, 255, 255, 255
· DATA 0, 255, 255, 255, 255
· DATA 0, 255, 0, 255, 255
· DATA 255, 255, 0, 255, 255
·
· DATA 255, 63, 0, 255, 255
· DATA 255, 0, 63, 255, 255
·
· DATA 200, 255, 100, 255, 255
· DATA 0, 0, 0, 255, 255
· DATA 0, 0, 0, 0 ' End of list
Thanks,
Kevin
No problem. You post a ton of helpful messages and code, so I don't want to sound ungrateful, but a few well placed comments can really help folks struggling with a programming problem.
Kevin,
In this area:
You have a space between the "END" and the "IF". So, instead of telling the compiler you want to end an IF statement, you're telling it you want to start a new one. Just change "END IF" to "ENDIF" and it'll compile just fine.
Compiler errors are often opaque and mysterious, and this one was misleading in terms of where it suggested the problem was. It seems like SX/B might need some way of keeping track of IF/ENDIF pairs to help in situations like this. That way the error could be pointed out before generating assembly code. Still, Parallax is constantly updating SX/B. Perhaps a fix to this will appear in the next SX/B version.
Thanks, PeterM
Now the way i take this is the durLow is to control the time the led is off and the durHigh is to control how long the led is on. I have adjusted the durLow but don't get any off time in between color changes. The durHigh seems to control on time. Is there something i'm missing to control the updateled for off time?
I geuss I don't understand the updateleds subroutine!
Kevin
· It seems you don't understand nested loops...
· The duration for each data line is (durHigh * durLow), durLow has nothing to do with "off time". If you want the LEDs to all be off then you need to add a DATA line with zero for Red, Green, and Blue with whatever duration values required to get your off time.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module"·available from Parallax for only $28.95 http://www.parallax.com/detail.asp?product_id=30012
"SX-Video OSD module"·available from Parallax for only·$49.95 http://www.parallax.com/detail.asp?product_id=30015
Product web site: www.sxvm.com
Available now! Cheap 4-digit LED display with driver IC·www.hc4led.com
"I reject your reality, and substitute my own." Mythbusters
·
Tried the loops and i guess what i found is that the SX will only let you go as high as 255 for max on the loop, so that is why peter had 2 loops(correct me if i'm wrong) but if that's true then this is different than typical basic!
I used to be pretty decent at programming back in the Commodore and TRS80 computer days.·I used to be able to write a program at work and take it home and plug it into the computer and magic it worked. I'm just a little rusty and basic has changed a little. At that time i wrote·3 programs i was really proud of, one was a program that used 3 loops and if statements that figured out that a farmer had $100.00 and needed to buy 100 animals the chickens were .50 each the ducks i believe were $10 each and the sheep were $15 each i was able to write the fastest program in our club. One of the other programs was a battle ship game before you could play them over a modem to another computer. I wrote the program but the neat thing was the grids and placing the ships for playing against the computer. The hard thing was making sure none of the ships got placed on top of each other in the grid of coarse this was for the computer to do the player placed his own ships but if tried to do the same thing it would tell you it was not possible. The 3rd program was a christmas music player with song menus and graghics(really cool). Oh the last programming i ever did was with gwbasic where i wrote a program when i was a machinist to figure the·depth of a 45deg cut for a given width and this is not using a 45deg cutter. What i found of coarse this is trig calculations that the depth of cut has a constant of .707 i think it was. So then i was able to make a chart to speed up my machining abilities. I was eventually going to do the same thing with compound angles and never got a chance.
Sorry not trying to prove anything here just thought you might get a kick out of the early computer days.
I'll be alright once i get back in the swing.
Kevin
Post Edited (DigitalDj) : 4/16/2006 12:47:20 AM GMT