Fade sketch "skipping"?

TL;DR - The following fade-up sketch works properly, but after several cycles has random visible “skipping” or “pulsing” in the smooth fading. I am too new at Arduino to know how to diagnose this issue or avoid it. Searching has not helped me so far, and any assistance is appreciated!

/* Turn Signal Up-Fade
 *  LED on Arduino pin D3 (+) and GND *NO resistor*
 */

int ledPin = 3;    // LED connected to digital pin

void setup() {
}

void loop() {
  for (int fadeValue = 0; fadeValue <= 39; fadeValue += 1) {
    analogWrite(ledPin, fadeValue);
    delay(12);
 }
  analogWrite(ledPin, 0);
  delay(282);
}

Background: This project is a simple automotive turn signal, which fades-up brighter, then cuts-off and pauses for the next cycle. This is intended to not only be different, but also reduce irritation to drivers in traffic behind me with typically harsh LED on/off signals. Delay() is used to provide 80 cycles/minute and for the eye to adjust. Basic function testing is with a 3mm LED between D3 and GND. No resistor is used for function testing, as it is PWM at less than maximum average current, which works great. The final version will use high-voltage LEDs with MOSFETs and PWM to 100% duty cycle.

Why are you setting the analogue pin to zero every time round the loop only to set it to fadeValue when the loop cycles round to the start again?

 void loop() {
  for (int fadeValue = 0; fadeValue <= 39; fadeValue += 1) {
    analogWrite(ledPin, fadeValue);
    delay(12);
  }
  analogueWrite(ledPin, 0);
  delay(282);
}

You might also find that your local version of "Construction and Use" regulations might have something to say about it. Indicators need to be noticed, so wrapping them in cotton wool is a retrograde step.

pcbbc:
Why are you setting the analogue pin to zero every time round the loop only to set it to fadeValue when the loop cycles round to the start again?

Thanks for the reply! The effect is to fade brighter then go dark for the delay. The fade holds at full brightness unless I pull it back to zero before the pause. brighter…Brighter…BRIGHTER-dark (delay) … repeat cycle.

Again, the sketch works and tests perfectly in concept, but the fade-up is randomly (?) not smooth. Why is the fade stuttering in such a simple sketch? What am I missing?

Did you try my suggestion, or just not understand it?

w427:
Thanks for the reply! The effect is to fade brighter then go dark for the delay. The fade holds at full brightness unless I pull it back to zero before the pause. brighter…Brighter…BRIGHTER-dark (delay) … repeat cycle.

Yes but don’t set it back to zero EVERY time around loop. That means it can be zero for a whole PWM cycle during the fade up, which I expect is what is causing your problem.

Read the code in my post #1 again. See how it sets zero brightness right before the 282ms delay? That’s the only place zero brightness (off) needs to be requested. If you request it while fading up I can see how that would potentially introduce a stutter.

Thanks again, @pcbbc - I do understand it was inside the for-loop. It makes sense and was originally how I wrote it, but changed it groping for results. I did try it again, and the fade-up still stutters. Perhaps a bit less frequently, but more obvious if anything. :slightly_frowning_face: Other ideas I can try?

void loop() {
  for (int fadeValue = 0; fadeValue <= 39; fadeValue += 1) {
    analogWrite(ledPin, fadeValue);
    delay(12);
  }
  analogWrite(ledPin, 0);
  delay(282);
}

[EDIT] I pulled the for-loop from here: https://www.arduino.cc/en/Tutorial/Fading

How’s all this wired up? On a solderless breadboard perhaps?

I thought of that, but no, it's not connections or anything. I've tired this on 3 devices (2 Nano and 1 UNO), each with different components or jumper wires, and they all reliably exhibit exactly the same issue. I invite you to try it, as it's about as dead simple as it gets, as one version pictured below.

I was hoping someone could tell me "oh, they roll-over…" or "conflict with…" or something that would simply explain this to me and how to fix it. In any case, I guess it isn't capable of reliably running this sketch and I need to do it a different way. :confused: Thanks for trying.

The video frame-rate doesn't capture the skipping, but the naked eye can see it:

(deleted)

Is that led just pushed through the holes and not soldered?
Understand you are limiting total current with the PWM, but I’d use a current limiting resistor and test properly up to 100% duty cycle.
You also need to consider the effect you’re having on the Arduino as you will be exceeding the maximum current for the pin.
What you are currently testing is not representative of your final solution anyway as you will probably have more steps and/or less delay, so even if this is a side effect of how you are changing the PWM it may very well not be visible in the final design.

pcbbc:
Is that led just pushed through the holes and not soldered?

Yes, and only to show how easy it is for others to try it. :wink: I have another that is soldered on strip-board, and a third that is wired with DuPont jumper wires as typical for Arduino projects. The connection is not the issue. How the code is running is.

pcbbc:
Understand you are limiting total current with the PWM, but I’d use a current limiting resistor and test properly up to 100% duty cycle.

Let’s say this is exactly what I wanted in a project. Why does it have this issue?

pcbbc:
You also need to consider the effect you’re having on the Arduino as you will be exceeding the maximum current for the pin.

I doubt it. My son has done this with LEDs on projects that have been running literally for years. Continuously. Works well for him, and seems to work for me just to do function testing at <4% average DC. If there was an issue with the output, then it should get worse as it runs longer. These flashers have been running 24/7 on my desk for weeks without issue, and won’t be an issue with the final product for reasons you state.

pcbbc:
What you are currently testing is not representative of your final solution anyway as you will probably have more steps and/or less delay, so even if this is a side effect of how you are changing the PWM it may very well not be visible in the final design.

Just for giggles, I wired-up a 2057 LED brake light bulb and ran it through an IRLZ44 MOSFET. Yes, I know, a MOSFET can draw over 1A momentarily with the gate capacitance, but it seems a standard way of doing it. It also flickers, though usually faintly due to the speed (2ms/step), and sometimes the flicker appears brighter. :’( Would someone notice? I don’t know, but I do, and it’s bugging the hell out of me. I want to know why the “simple” code isn’t running as-expected.

To be blunt, I am more interested in it working correctly than if it’s “good enough”. I want to learn why such a simple sketch is doing this, which may help me learn how to do or not do other things as I try other projects. Thanks for trying to figure it out. Looks like I’ll be buying or borrowing an oscilloscope to diagnose my $2 Nano flasher, but that still won’t tell my why. :roll_eyes:

(deleted)

:disappointed_relieved: Still no success, and I set it up on a fresh Arduino, with LED resistor, MOSFET, full 0-255 PWM, and slow cycling to see it easily. While it would be more effective to try it yourself (code below), the video link still shows it randomly skipping a little or a lot. I am back to my first post - why is it not smoothly fading up? I have done everything (I think) to show it must be code related?

Video link: https://i.imgur.com/w9ZA8xZ.mp4

int ledPin = 3;    // LED connected to digital pin

void setup() {
}

void loop() {

  for (int fadeValue = 0; fadeValue <= 255; fadeValue += 1) {
    analogWrite(ledPin, fadeValue);
    delay(12);
  }
  analogWrite(ledPin, 0);
  delay(282);
}

Make the delay from 12 to.. 250 or 500 and see if you see anything that may give a clue.

That's what I'd do..

-jim lee