Having read this post
PWM not fully off - Frequently-Asked Questions - Arduino Forum
and the analogWrite reference, I am still at a loss as to why an analogWrite of 0 to pin 11 (or 9 or 10) does not kill the LED completely. It maintains a nice dim glow, not enough to notice unless you are looking. Pins 5 and 6, OK, so there is timer interaction, but 9,10,11 (different timers 9,10 and 11) is what has me befuddled.
This is revamped old code, but I do not remember it leaving the LED lit while working on it in IDE V1.0. Now I am at V1.6.6, but I don't see why that would make a difference.
/*Write a slow LED fade routine without delay.
* Tried pins 9, 10, 11
* 20mm LED anode to pin 10 to 255 ohm resistor to GND.
* Could not get LED to fully extinguish
* without using digital write and setting pin 10 to low.
* (digitalWrite commented out in fadeHold().
* Is there a better way?
*
* 1/3/16 -fab
*
*/
const int LED_PWM_PIN = 10;
int brightness = 255;
int fadeAmount = 3;
unsigned long previousMillis = 0;
unsigned long interval0 = 100;
unsigned long interval1 = 5000;
unsigned long currentMillis = 0;
void setup(){
pinMode(LED_PWM_PIN, OUTPUT);
Serial.begin(9600);
}
void loop(){
currentMillis = millis();
if(brightness <= 0){
fadeHold();
}
else{
fade();
}
}
void fade(void){
if (currentMillis - previousMillis > interval0){
previousMillis = currentMillis;
Serial.println(brightness);
analogWrite(LED_PWM_PIN, brightness);
brightness-=fadeAmount;
}
}
void fadeHold(void){
// digitalWrite(LED_PWM_PIN,LOW); //added to fully extinguish LED
Serial.print("5 sec 'Hold', brightness = ");
Serial.println(brightness);
if (currentMillis - previousMillis > interval1){
brightness = 255;
}
}
Looking for a better way than the digitalWrite 'low' to pin after hitting pwm 0. Seems like that is what should be an unnecessary byte load in the code.
Actually, this is an experiment to learn to fade a bank of high power LEDs from a Meanwell dimming puck, and having tested the puck, it makes no difference as the power LEDs are OFF. Still, I need to know if I am making some error somewhere, and, if there is a better way to fade LEDs.
The final project will start out fading up or down slowly (20 mins), and if a button is pushed while fading, fade up or down quickly(20 sec). The button will toggle the lights from current state to opposite state when pressed while not fading. If held, the button will cause full on or full off depending on starting state.
Having this happen so early in my learning curve for this code, is a bit unnerving, so i wanted to check it out here on the forum!
Thanks,
-fab