Digital display bug: sgmts. randomly lighting up?

I’m having an error with using a digital display that I just got. LED segments randomly light up and only turn off when are told so by the programming. Could you help me figure out what’s going on and what I can do to fix it?

This is the first thing I’ve done with my digital display so I’ve simply set it to cycle the segments and turn each on and off for a short time. I have each segment wired up to a digital output on my Duemilanove. The outputs are turned on and off using an array that has the order of output pins. A For loop runs through all the array elements (pins) and turns each on and off. This loops indefinitely.

The problem is that at the end of the whole cycle when there is a pause, some of the segments sometimes turn on even though they’re not supposed to. They return to normal when the sketch cycles over them and turns them off again. The occurrence of this bug seems random, as is which segments are turned on. It seems to happen every 5-10 cycles and 4 or less segments malfunction at one time. However, it is predictable at the beginning, where every time the cycle is first done (the Arduino is reset/powered up) the last 4 segments light up (except for the RHDP). I have figured out that these are pins 9-13 (I do not use pin 13 because it has the on board LED, but I mention it because it is affected; the LED does light up and stay on).

I don’t really think this is a hardware problem, but I really don’t have a clue. I was thinking it would be more likely to be software, so I posted here. But in any case, I am using Radio Shack’s LED digital display.

Thank you for any help!

Here’s my code:

/* Digital Number Display LED Cycle
This sketch is meant for a 7-segment digital number display.
(mine has one right hand decimal point (RHDP) that can also be lit up, making 8 pins)
It will cycle through the segments turning them on and off.

Setup: wire each lead of the display to a digital output and wire
the common cathode to ground. Insert the numbers of the outputs in the
PinArray in the order that you want to cycle the segments

Wiring notes for project on 12/28/09
(Anode letter, pin number, digital output pin number)


const int numSeg = 8;       //number of segments in the display (+ RHDP), also number of output pins used
const int pause = 100;      //ms that each segment is on and off
int PinArray[numSeg] = {8, 7, 4, 9, 10, 12, 11, 5};      //array of pins wired to display, in order of sequence

void setup()
   for(int i = 0; i <= numSeg; i++)    //Setting pins to output by cycling through the array with a For loop
      pinMode(PinArray[i], OUTPUT);
void loop()
   for(int i = 0; i <= numSeg; i++)          //cycling through PinArray with a For loop to turn each pin on and off with a delay inbetween
      digitalWrite(PinArray[i], HIGH);
      digitalWrite(PinArray[i], LOW);
    delay(500);        //pause for longer to signify end of loop, makes it easy to see when it starts again

The valid value for the indices into the PinArray are 0 through 7. You are trying to turn on and then off PinArray[8]. That array position does not exist.

Change this:

for(int i = 0; i <[glow]=[/glow] numSeg; i++)

to this:

for(int i = 0; i < numSeg; i++)

Oh, so array element “names/positions” are 0-based (however you would call that). That makes sense now; I think I remember that from elsewhere.

I changed it and it works.

Thanks much!