LED Candle Effect Question

Hi everyone. I am very new to the Arduino world and have been doing some various practice projects either virtually via Tinkercad or physically with an Arduino Uno R3. I came across a bit of code from a YouTube video where the person in the video created a candle flickering effect. I have my R3 connected to my breadboard and I am using 220 ohm resistors for each LED. When the sketch runs on the r3 i get the flickering like he does in the video however its only one led thats flickering out of the three and id like all 3 to flicker at the same time with the same random pattern. Based off the code it seems they should be doing this but maybe I am missing something and was hoping someone could point out why only one led would be flickering and the other two are staying on solid. Specifically the LED connected to pin 11 is the one flickering and pins 12 and 13 are staying solid. My code is below. I appreciate any help you can provide.

void setup()
{
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  analogWrite(11, random(150) + 170);
  analogWrite(12, random(150) + 170);
  analogWrite(13, random(150) + 170);
  delay(random(175));
}

Which board are you using that has 12 and 13 as PWM pins?

See here

Wow I completely overlooked the fact that the pins I had them in weren’t PWM. Once I switched them they are all blinking now, thank you. They still aren’t all following the same random blink pattern simultaneously though. They are all blinking randomly however at different intervals. I’d like for the random blinks to sync up across all of the bulbs. How would I go about accomplishing that without having all of the bulbs on the same rail of the breadboard?

Do you mean you should be writing the same random number to all three LEDs?

yes exactly. Basically what I am trying to accomplish is the look of a light that is flickering sort of like you would see in a horror movie if that makes sense.

No, I don’t watch horror movies, but I imagine candle flames are more yellow than white.

correct. Its more the effect I am going for than the color at this point. I think the random effect that I have working now fits what I want but I want to be able to control several LED's as you mentioned where the same random number is being written to each of the LED's at the same time so they achieve the same blinking pattern across all LED's simultaneously. With them flashing how they are now where each LED is getting a different random number than the others they don't flicker in unison and therefore look more like a candle, which based on the code I found in the YouTube video makes sense, but I wanted to try to build off of that to get more of a shorting light effect. If you've ever seen what a fluorescent bulb does when the ballast running it is going bad that's the kind of flicker I am going for.

So,how does writing the same random number to each LED look now?

thats the thing i am not sure how to approach making them flicker together and receive the same random number so they are all flickering the same. right now there are instances where one is on and another is off then they may be both on at the same time or one dims a bit and the other blinks at a faster rate. I want all of them to blink at the same rate so the effect is uniform across all of them led's. I did find something doing a bit of googling that it isnt possible to apply analogWrite to multiple pins at the same time. Is that true?

So, assign the random number to a variable, and analogWrite the variable to all the LEDs.

Edit: I’ve assumed you’re using an RGB LED.
I’m now not sure how I assumed that.

got it. That makes sense. Going to give that a try. Thanks.

void loop() {

   int rNum = random(150) + 170);
  analogWrite(11, rNum);
  analogWrite(12, rNum)
  analogWrite(13, rNum)
  delay(random(175));
}

Like this?

-jim lee

jimLee:

void loop() {

int rNum = random(150) + 170);
analogWrite(11, rNum);
analogWrite(12, rNum)
analogWrite(13, rNum)
delay(random(175));
}


Yes, but with less 12 and 13

Like this?

-jim lee

Got it. I didnt get quite the same code but I have it working now using this code. Thanks for the advice, I really appreciate it.

int ledFlicker;
  
void setup()
{ 
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
}

void loop() {
  ledFlicker = random(150) + 170;
  analogWrite(9, ledFlicker);
  analogWrite(10, ledFlicker);
  analogWrite(11, ledFlicker);
  delay(random(150));
}

I don't want to get in the way of you learning Arduino coding. But, why not just use flickering LEDs?

void candlesEffect() {

  const short FLAME_PWM_RANGE = 2 * LED_MAX_PWM / 3;
  const short FLAME_SPEED = 400;
  const byte pins[] = {LEDS_LEFT_PIN, LEDS_RIGHT_PIN};

  static short targetPwm[sizeof(pins)];
  static short currentPwm[sizeof(pins)];
  static unsigned long prevMicros[sizeof(pins)];

  for (int i = 0; i < sizeof(pins); i++) {
    if (micros() - prevMicros[i] > FLAME_SPEED) {
      prevMicros[i] = micros();
      if (currentPwm[i] == targetPwm[i]) {
        targetPwm[i] = random(LED_MAX_PWM - FLAME_PWM_RANGE, LED_MAX_PWM);
      }
      currentPwm[i] += targetPwm[i] < currentPwm[i] ? -1 : 1;
      analogWrite(pins[i], currentPwm[i]);
    }
  }
}

from SunriseClock/SunriseClock.ino at main · jandrassy/SunriseClock · GitHub

He wants them to all flicker together. Like a string of lights on a failing circuit.

-jim lee