Hi Polliwog,
so how would you describe your knowledge-level about programming
Just a rough estimation:
a) I know what void setup() and void loop() do but that's it
b) I can write a small program that blinks an LED using the command delay()
c) I know how non-blocking timing works I know what PWM is
So the steps are making a single LED fade in fade out.
Therefore you can look into the Arduino-example-code
file - examples - analog - fading
The demo-program uses this unspeakably bad function delay()
but it shows how analogWrite can be used to fade LEDs
As you wish to fade multiple LEDs a software-tecnique call non-blocking timing is needed.
One method to realise your LED-fading with a time-offset is to use a loop that increments a counter once every x amount of milliseconds.
To show the basic principle I use a smaller number of steps from LED OFF to full bright ON
Counter LED1 LED2 LED3 LED4 LED5 LED6
1 10% | | | | |
2 20% | | | | |
3 30% | | | | |
4 40% | | | | |
5 50% | | | | |
6 60% 10% | | | |
7 70% 20% | | | |
8 80% 30% | | | |
9 90% 40% | | | |
10 100% 50% | | | |
11 90% 60% 10% | | |
12 80% 70% 20% | | |
13 70% 80% 30% | | |
14 60% 90% 40% | | |
15 50% 100% 50% | | |
16 40% 90% 60% 10% | |
17 30% 80% 70% 20% | |
18 20% 70% 80% 30% | |
19 10% 60% 90% 40% | |
20 0% 50% 100% 50% | |
21 40% 90% 60% 10% |
22 30% 80% 70% 20% |
23 20% 70% 80% 30% |
24 10% 60% 90% 40% |
25 0% 50% 100% 50% |
26 40% 90% 60% 10%
27 30% 80% 70% 20%
28 20% 70% 80% 30%
29 10% 60% 90% 40%
30 0% 50% 100% 50%
31 40% 90% 60%
32 30% 80% 70%
33 20% 70% 80%
34 10% 60% 90%
35 0% 50% 100%
36 40% 90%
37 30% 80%
38 20% 70%
39 10% 60%
40 0% 50%
41 40%
42 30%
43 20%
44 10%
45 0%
if counter is between 1 and 10 fade-in LED1 by incrementing duty-value of the analogWrite by +10
if counter is between 11 and 19 fade-OUT LED1 by incrementing duty-value of the analogWrite by +10
if counter is bigger than 19 LED1 duty is 0
if counter is between 6 and 15 fade-in LED2 by incrementing duty-value of the analogWrite by +10
if counter is between 16 and 24 fade-OUT LED1 by incrementing duty-value of the analogWrite by +10
if counter is bigger than 25 LED2 duty is 0
etc. etc.
here is a "sketchy" sketch to show the principle
[code]
// not full functional code just a "sketchy" sketch to show the principle
void setup() {
// put your setup code here, to run once:
}
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if ( currentMillis - expireTime >= TimePeriod )
{
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false; // not expired
}
unsigned long MyTimer;
unsigned long MyIntervalInMilliSeconds = 50;
int Counter = 0;
int LED1_Duty = 0;
int LED2_Duty = 0;
int LED3_Duty = 0;
int LED4_Duty = 0;
void loop() {
// do a timed counting up as the timebase
if (TimePeriodIsOver (MyTestTimer, MyIntervalInMilliSeconds) ) {
Counter++;
if Counter > 45 {
Counter = 1;
}
}
// max Duty = FULL BRIGHT ON is 255 so a 1/10th-step is 255 / 10 = 25.5
// LED1
if (Counter > 0 && Counter =< 10) {
LED1_Duty += 25; // fade-in
}
else if (Counter > 10 && Counter =< 19) {
LED1_Duty -= 25; // fade-out
}
else {
LED1_Duty = 0;
}
// LED2
if (Counter > (0 + 5) && Counter =< (10 + 5) ) {
LED1_Duty += 25; // fade-in
}
else if (Counter > (10 + 5) && Counter =< (19 + 5)) {
LED2_Duty -= 25; // fade-out
}
else {
LED2_Duty = 0;
}
// after all duty-values have bee setup up update PWM-signals
analogWrite(LED1_Pin, LED1_Duty);
analogWrite(LED2_Pin, LED1_Duty);
}
[/code]
this sketchy sketch does not (yet) compile
best regards Stefan