A for loop without its i++ at the end looks strange to me :-)
I'd use a direction variable with values +1 or -1 and would add that to the current pwm value.
If direction is greater that 1 then 255 needs to be decreased accordingly.
HTH
I'd use a direction variable with values +1 or -1 and would add that to the current pwm value.
Code:
int i;
int pwm = 0;
int direction = +1; // either +1 or -1
analogWrite(pin, pwm);
for (i = 0; i < 255; i++) {
pwm += direction;
analogWrite(pin, pwm);
delay(d); // see "blink without delay" example for how to get rid of this!
}
direction = -direction;
int pwm = 0;
int direction = +1; // either +1 or -1
analogWrite(pin, pwm);
for (i = 0; i < 255; i++) {
pwm += direction;
analogWrite(pin, pwm);
delay(d); // see "blink without delay" example for how to get rid of this!
}
direction = -direction;
If direction is greater that 1 then 255 needs to be decreased accordingly.
HTH
That looks a lot better then mine.. thanks