Since MrDEB has been silent today, he may have figured out that a whole bunch of serial routines with delay statements aren't going to accomplish what he wants
at least not very well.
One way to do what he wants is to base all actions on a loop counter, with at most exactly ONE delay statement. Ok, let's start with the basic program loop.
Code:
Dim Counter as Longword
Counter = 0
While 1 = 1
// some code will go here
Inc (Counter)
If Counter > 4000000000 then
Counter = 0
End If
Wend
What happens in this program loop? The variable Counter counts from 0 to 4,000,000,000. This number can actually be 2^32, but rounding it off won't matter much. The IF statement may not be strictly necessary. The Counter value should automatically wrap back to zero.
Pretty neat, huh? Well, let's work on that. Let's add an LED, say at 50% brightness .
Code:
Dim Counter as Longword
Dim LED1On as Longword
Dim LED1Off as Longword
LED1On = 0
LED1Off = 0
Counter = 0
While 1 = 1
// some code will go here
If Counter = LED1On then
LED1 = 1
LED1On = Counter + 20
LED1Off = Counter + 10
End If
If Counter = LED1Off then
LED1 = 0
End If
Inc (Counter)
If Counter > 4000000000 then
Counter = 0
End If
Wend
Ok, so what happens here? Led1On starts at 0. When the Counter value equals 0, three things happen:
¤ LED1 is turned on
¤ LED1On is set to 20
¤ LED1Off is set to 10
When the Counter = 10, LED1 will be turned off.
When the Counter = 20, the sequence repeats.
LED1 is on half the time, off half the time, so the brightness is 50%. This is kind of crude; making it a
subroutine (do not call this a sub route. That is gibberish.) would be cleaner.
So now let's add a second LED, operating at the same time the first does it's thing.
Code:
Dim Counter as Longword
Dim LED1On as Longword
Dim LED1Off as Longword
Dim LED2On as Longword
Dim LED2Off as Longword
LED1On = 0
LED1Off = 0
LED2On = 0
LED2Off = 0
Counter = 0
While 1 = 1
// some code will go here
If Counter = LED1On then
LED1 = 1
LED1On = Counter + 20
LED1Off = Counter + 10
End If
If Counter = LED1Off then
LED1 = 0
End If
If Counter = LED2On then
LED2 = 1
LED2Off = 20
LED2On = 500
End If
If Counter = LED2Off then
LED2 = 0
End If
Inc (Counter)
If Counter > 4000000000 then
Counter = 0
End If
Wend
So now, while LED1 is flashing rapidly on and off to be 50% bright, LED2 is flashing very briefly on and then off for a long period, resulting in a twinkling effect. Two LEDs operating independently in the same loop.
This definitely is not polished code, but will show anybody willing to take a few minutes to understand the code how to use this technique. This loop incrementing up to 4,000,000,000 is going to execute very quickly. The on/off Counter times need to be adjusted to suit the effect desired, and a short delay may need to added at the end of the loop. Also, at the end of the loop, initial values will need to be reset to zero.
The picture shows the process. On and off periods might be set to random numbers. But remember, all actions are based on Counter values. Exactly one delay statement might be added. MrDEB's fading routine can also be adapted to this technique.
View attachment 137243