In SX Microcontrollers, SX/B Compiler and SX-Key Tool, PJMonty wrote: 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 blinding 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. [list]Thanks, PeterM[/list] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=118695#m120416 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)