I was expecting that the following code would turn out in a 3 seconds delay with the led showing brightness 0, then a quick fade in and fade out, and then again 3 seconds delay with brightness 0.
But the delay (3000); is actually having an influence in the fade´s rate. why is that?
I haven't tried this, so it may need some work, but give it a try:
#define FADEDELAY 30 // Make the change 3x slower. Change as needed
int ledcontrol = 9;
int brightness = 0;
int fadeAmount = 1;
void fadeLed()
{
int i;
for (i = 0; i < 255; i++) {
analogWrite(ledcontrol, i); // Since the fade amount is 1, just use i
delay(FADEDELAY); // pause for this long between levels...
}
}
void setup() {
pinMode(ledcontrol, OUTPUT);
}
void loop() {
delay (3000); // This makes it wait 3 seconds between "fades". Maybe decrease a bit?
fadeLed ();
}
If you want the fade change to be something other than 1, you'll need to add it back into the code.
thank you. Your code works, except that is missing the fade out, but then I really dont understand why the code I posted in #1 doesnt work. It is the same principle isnt it?+
also, after the first loop, the delay is done with the brightness value at 255, and not at 0 where I would need it.
No, it's not the same principle - your fade routine makes one change to the fade level of the led, then returns to loop where it will hit the delay(3000) again.
Econjack's fadeled routine does the entire fade before returning so you only get one delay per fade cycle.
ok, so I am trying to add the fade out to Econjack's routine, and I did the following, but I have a very strange behaviour: during the delay, the led is not completely off, but dimply lit. Am I doing a mistake?
#define FADEDELAY 10
int ledcontrol = 9;
int brightness = 0;
int fadeAmount = 1;
void setup() {
pinMode(ledcontrol, OUTPUT);
}
void loop() {
delay (3000);
fadeLed ();
}
void fadeLed() {
int i;
for (i = 0; i < 255; i++) {
analogWrite(ledcontrol, i);
delay(FADEDELAY);
}
for (i = 255; i > 0; i--) {
analogWrite(ledcontrol, i);
delay(FADEDELAY);
}
}