Perfectly good LEDs fail to light.

Does anyone have any idea why, at random, only 2 LEDs light when 3 are supposed to?

The code randomly selects 3 out of 6 LEDs to light. When the code is debugged, all signs are that it is working as intended. The array is loaded as intended and all the LEDs are working. Every one of them lights at one point or another. However, without rhyme or reason, occasionally only 2 of the LEDs light up. 3 3 3 2 3 2 3 3 3 3 2 3 3 3 2 2 3 etc. What the heck is going on???

(LEDs are on a breadboard connected to pins 8,9,10,11,12,13 of an Arduino Uno)

  int ledSet[12] = {0,0,0,0,0,0,8,9,10,11,12,13};
  int xCount;
  unsigned long startTime;
  long x;

void setup() {
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode( 9, OUTPUT);
  pinMode( 8, OUTPUT);
  Serial.begin(9600);                             // *** Trouble Shooting ***
}

void loop() {

  for(int i = 0; i < 6; i++) { ledSet[i] = 0; }    // Zero out the array.
  xCount = 1;                                      // Reset the counter.
  startTime = millis() +1000;                      // Set timer.
  randomSeed(millis());                            // Randomly seed randomizer.

  while(xCount < 4) {
    x = random(0,6);
    if(ledSet[x] == 0) {                           // If we haven't already set this one...
      ledSet[x] = 1;
      xCount++ ;
    }
  }
  delay(startTime -millis());                      // Delay what's left of 1 second.

  startTime = millis() +1000;                      // Reset timer.
  do {
    String strText = " ";                          // *** Trouble Shooting ***
    for(int i = 0; i < 6; i++) {
      strText = strText +" " +ledSet[i];           // *** Trouble Shooting ***
      if(ledSet[i] == 1) {
        digitalWrite(ledSet[i +6], HIGH);
        delay(1);
        digitalWrite(ledSet[i +6], LOW);
      }
    }
    Serial.println(strText);                       // *** Trouble Shooting ***
  } while(millis() < startTime);                   // Delay what's left of 1 second.

}

Post a diagram showing how the LEDs are wired and the values of the current limiting resistors that you are using.

I think all 3 are lighting but there doing it at such a fast rate that they will be super dim.

I would have thought that you would clear all the leds using a for loop then set the leds selected to on then wait for one seconds and repeat

gpop1, the first thing I thought of… notice there is a delay() in the code between HIGH and LOW that negates that possibility. The ones that light are plenty bright, the ones that don’t, show no signs of life at all. (until they are lit later in the loop)

jremington, I’m not much of a diagrammer… suffice it to say that there is a 5v jumper from the digital out pins (8 thru 13) on the Arduino, to a 100k resistor, to the cathode of the LED (3v to 3.4v), to the ground to the Arduino.

Image attached.

change your delay(1) to delay(50). 1 is 1ms, so most of the time your LEDS will be off since you turn them on for 1ms only, then spend time doing other things.

afduinodlb, I have… see reply above. At a value of 5 the ones that light flicker. At 10 or higher the lit ones blink during the 1 second delay that is created by the “while(millis() < startTime)”. Regardless of whether the delay is 50ms or removed entirely, the LEDs act equally erratic.

The unlit LED (whichever one it is) changes. How do I know? Because every LED lights as one of the 2 or 3 on subsequent iterations.

The array ledSet at lines 39 - 41 contains three zeros and three ones, but occasionally only 2 LEDs light. ???

 if(ledSet[i] == 1) {
        digitalWrite(ledSet[i +6], HIGH);
        delay(1);
        digitalWrite(ledSet[i +6], LOW);
      }

Your code is quite strange. You only flash for a millisecond, and the only reason you can see the LEDs is that you are in a loop, turning them on and off rapidly.

... occasionally only 2 of the LEDs light up ...

How occasionally? Give a figure. Every minute? Every 10 seconds? I am testing your code and I haven't seen only two light up yet.

Your photo is rather blurry but it looks to me like some of the LEDs are not on the ground plane. The black wire from the Arduino looks like it is going into the red bus, but some of the LEDs are plugged into the blue one.

Good eyes Joe. The ground wire came out when I relocated it to take the picture. Stuck it back in the wrong hole.

Did you mean 100 ohm resistors or 100k?

100 ohm. Sorry.

From a 32 second run. (# of LEDs lit during each 1 second ON iteration)

3 3 2 3 2 3 3 3 3 2 3 3 3 2 2 3

And, if I change the xCount limit to <3, 1 or 2 LEDs get lit. If you change it to <5, 3 or 4 LEDs light (randomly).

P.S. Line 44, “Serial.println(strText);” prints numerous times during each ON iteration, so you have to scroll down to see the changes that occur when the array is reset.

MavenScout: Good eyes Joe. The ground wire came out when I relocated it to take the picture. Stuck it back in the wrong hole.

Yes, but are all the grounds together now (from the LEDs)?

I tested for 37 seconds (see video) and the 3 LEDs always came on.

http://gammon.com.au/movies/6leds.mov

P.S. Line 44, “Serial.println(strText);” prints numerous times during each ON iteration, so you have to scroll down to see the changes that occur when the array is reset.

Or you could craft your own conditional breakpoint, something like this.

//...

  do {    
        // *** Trouble Shooting ***
        String strText = " ";                          
        uint8_t checkSum = 0;
        
        for(int i = 0; i < 6; i++) {
            // *** Trouble Shooting ***
            strText = strText +" " +ledSet[i];           
            checkSum += ledSet[i];
            
            if(ledSet[i] == 1) {
                digitalWrite(ledSet[i +6], HIGH);
                delay(1);
                digitalWrite(ledSet[i +6], LOW);
            }
        }
    
        // *** Trouble Shooting ***
        if(checkSum != 3) {
            Serial.print "Error:"
            Serial.println(strText);
            while(1);                      
        }
    
    } while(millis() < startTime);                   // Delay what's left of 1 second.

//...

I hacked out the LED operations and what was left, has been running over 30 minutes without stopping.

BTW. Your random seed isn’t very random. The number of instructions processed by the Arduino, will be roughly the same, each time the seed assignment takes place. A slightly more random seed can be derived from an analogRead of a floating pin.

Thanks for the video Nick Gammon, that prompted me to break down my test board and reconstruct it with some new (different) parts, with mixed results. Which made it evident that the breadboard has seen one too many projects. How exasperating! Thanks all for your time and effort.