I want to light an LED in a sketch and then have it fade out. I tried to use analogWrite and have it decrease from a value of 255 to 0 using a for() loop, but I can't seem to get it to work. Using digitalWrite I can turn it on and off but cant get it to fade out. Can anyone help please.
const int ledPin1 = 2;
const int ledPin2 = 3;
const int ledPin3 = 9;
int j = 0;
void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop() {
j = 255;
delay(1000);
if (j >= 5);
{
analogWrite(ledPin3,j);
delay(10);
j = j - 5;
}
delay(1000);
}
-
6Please show what code you have, along with your wiring. There is an example in the IDE that fades an LED. Did you get that to work?Majenko– Majenko09/18/2017 10:30:56Commented Sep 18, 2017 at 10:30
-
Yes I did get that sketch to work. I am using pin 9Stonedragon– Stonedragon09/18/2017 11:57:49Commented Sep 18, 2017 at 11:57
-
2So please show us your code and better define "can't seem to get it to work".Majenko– Majenko09/18/2017 11:58:29Commented Sep 18, 2017 at 11:58
-
What sketch did you get to work?SDsolar– SDsolar09/18/2017 15:42:22Commented Sep 18, 2017 at 15:42
-
Yes my complaint is a bit vague. The LED comes on and stays on. It is in my code. I am missing what I doing wrong. Some sort of mental block. Wiring is just a LED and resistor from pin 9 to gnd.Stonedragon– Stonedragon09/19/2017 01:32:19Commented Sep 19, 2017 at 1:32
2 Answers 2
Not all output pins are able to perform PWM ("AnalogWrite").
On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.
https://www.arduino.cc/en/Reference/AnalogWrite
Note that the PWM-enabled ports are marked by a "~" on the board.
If you try analogWrite, e.g., on port 4, you'll get your observed switch behaviour.
-
-
1Well - if that's not the problem, I'm totally with @Majenko. Post code, wiring and tell us what boards you are using.Tommy– Tommy09/18/2017 12:03:44Commented Sep 18, 2017 at 12:03
-
const int ledPin1 = 2; const int ledPin2 = 3; const int ledPin3 = 9; int j = 0; void setup() { pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); pinMode(ledPin3, OUTPUT); } void loop() { j = 255; delay(1000); if (j >= 5);{ analogWrite(ledPin3,j); delay(10); j = j - 5; } delay(1000); }Stonedragon– Stonedragon09/19/2017 01:27:52Commented Sep 19, 2017 at 1:27
-
New at this so my posts may be a mess. I am using an UNO.Stonedragon– Stonedragon09/19/2017 01:29:12Commented Sep 19, 2017 at 1:29
Problems noted in comments:
void loop() {
j = 255; // Resets j EVERYTIME
delay(1000);
if (j >= 5); // Semi-colon here makes this a null statement
{
analogWrite(ledPin3,j); // j is always 255 (see line 1)
delay(10);
j = j - 5; // No effect as j is reset in line 1
}
delay(1000);
}
Here's a simple fix:
int j = 255; // Start at 255
void loop() {
analogWrite(ledPin3, j); // Set the brightness
delay(10);
j -= 5;
if (j <= 0) j = 255; // Reset j?
}
-
of course. Thank you very much. Works like a charm. I must be getting old.Stonedragon– Stonedragon09/19/2017 04:18:36Commented Sep 19, 2017 at 4:18
-
1How about we double what we are paying you.Stonedragon– Stonedragon09/19/2017 04:18:59Commented Sep 19, 2017 at 4:18