Hello everyone. I'm trying to get a smooth fade led using attiny85. Here is the sketch I'm using that i found online a long time ago.
/*
/*
Fade
This example shows how to fade an LED on pin 9 using the analogWrite()
function.
The analogWrite() function uses PWM, so if you want to change the pin you're
using, be sure to use another PWM capable pin. On most Arduino, the PWM pins
are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Fade
*/
int led = 0; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
For some reason it's no smooth and seems to be a little Jittery. Not sure how to make it smooth. Can someone please help me to figure this out?
I hastened to ensure that there are no problems in the code.
After your brightness has gone beyond the limits, you need not only to change the fadeAmount, but to recalculate the brightness.
Otherwise, for example, it is possible that your brightness is less than zero
I did that and seems to be a little better but it has a blink effect when the led comes back on to 100%. Is there away to fade back in not out just out?
I did that and seems to be a little better but it has a blink effect when the led comes back on to 100%. Is there away to fade back in not out just out?
It dosn't fade back in. The led just flash back on to 100% and starts again.
You received several different advices and say "I did that", without even specifying to whom you are answering or what you did.
It will be better if you indicate the nickname of whom you are replying to or quote his message (as I did in this message)
I have tested your sketch using the following ATtiny85 Dev Board (Fig-1)and the brightness of the onboard LED (connected at PWMPin-1) is changing smoothly back-and-forth.
Figure-1:
if your fade amount is some value that doesn't exactly count up to 255 or down to 0 (like 2 or 3) then your code does not make sure you are only writing a value between 0 and 255. You need to make that a little more robust.
...
brightness = brightness + fadeAmount;
if ( brightness < 0 ) brightness = 0;
if ( brightness > 255 ) brightness = 255;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
...
The human eye's response to intensity is logarithmic, not linear. So, you want very small steps at low levels but can tolerate larger ones as it get brighter. There's been lots of such code posted on the forum. Perhaps a lookup table in PROGMEM?
Even with logarithmic response, at slow speeds it may still be possible to see the steps at low brightness levels with 8-bit PWM. For example, going from 1 to 2 is a relatively big step in brightness. If that is an issue for OP's application, there's a trick to do 10- or 12-bit PWM to increase the resolution for the smaller values.
Those interact to form the speed of the fade. It also sets the frame rate to 33 Hz, which may be visible to some.
Try double the frame rate and half the step at a time, like 15 and 2 or 3. Maybe triple rate 10 ms delay and steps of 1 or 2.
Your code looks like it should fade up and down.
The simple PWM to LED fade is not as good as you can get. You may see what seems like a bunpy peak brightness. Going to a little trouble you can make the curve of brightness match what the eyes see better. I've never needed to care.
b707 You are correct my fault there. I found out the timing is all wrong with the fadeamount and the delay. I went down in numbers until The effect That looks good to me and now it is working correctly. Thank you.