- Joined
- Jan 21, 2010
- Messages
- 25,510
Sorry I didn't get back to you earlier.
I had a quick look at the code, but I couldn't see anything that was doing any timing.
I presume the program just scans the array as fast as possible.
The problem with this approach is that it takes a different amount of time to process LEDs in different states (especially because you're doing processing additional to just scanning the array within the loop). This means that the brightness of LEDs in some columns will vary due to the difference in time taken to process the subsequent column.
Remember that I have said that each column must be processed for the same fixed period. In your case, the LedAnode routine will take a different amount of time depending on the LED state. This will presumably result in the previous column being displayed for a different amount of time.
Because your code is intermingling the scanning and the brightness/fading, you have created a rod for your own back. It would be far better to have a single routine that scans the entire array in constant time, and then (with the LEDs off) you adjust any values you want to before scanning the array again. Even then, any change in the amount of processing you do between scans would have the effect of altering the brightness of the array. Thus, without any control of timing, you need to ensure that your processing is as close to constant time as possible.
One option is to have a timer that generates a periodic interrupt. Each time it runs, it sets a flag (time to scan the array). Your main loop simply processes each key in turn, doing whatever it needs to do to the brightness of each LED (perhaps the interrupt also increments a counter so you know how much time has passed). After each button is processed you check the flag, and if it is set you reset it and scan the array. This will ensure that your scans are done at fairly constant intervals.
Whatever you do, you need to ensure that the LEDs at a given intensity are always on for the same proportion of every second or you'll get weird effects that will be hard to understand, and thus even harder to control.
I had a quick look at the code, but I couldn't see anything that was doing any timing.
I presume the program just scans the array as fast as possible.
The problem with this approach is that it takes a different amount of time to process LEDs in different states (especially because you're doing processing additional to just scanning the array within the loop). This means that the brightness of LEDs in some columns will vary due to the difference in time taken to process the subsequent column.
Remember that I have said that each column must be processed for the same fixed period. In your case, the LedAnode routine will take a different amount of time depending on the LED state. This will presumably result in the previous column being displayed for a different amount of time.
Because your code is intermingling the scanning and the brightness/fading, you have created a rod for your own back. It would be far better to have a single routine that scans the entire array in constant time, and then (with the LEDs off) you adjust any values you want to before scanning the array again. Even then, any change in the amount of processing you do between scans would have the effect of altering the brightness of the array. Thus, without any control of timing, you need to ensure that your processing is as close to constant time as possible.
One option is to have a timer that generates a periodic interrupt. Each time it runs, it sets a flag (time to scan the array). Your main loop simply processes each key in turn, doing whatever it needs to do to the brightness of each LED (perhaps the interrupt also increments a counter so you know how much time has passed). After each button is processed you check the flag, and if it is set you reset it and scan the array. This will ensure that your scans are done at fairly constant intervals.
Whatever you do, you need to ensure that the LEDs at a given intensity are always on for the same proportion of every second or you'll get weird effects that will be hard to understand, and thus even harder to control.