How to turn on x number of LEDs at once, for 3x3 array?

I made a homemade 3x3 LED array and each LED is attached to a Digital output on the Arduino Nano I have (D2-D10).

I originally wrote a program for it that, for every image I displayed (be it a plus, a x, a spiral, whatever) it would turn each LED on and off in a specific order. It worked but it used a MASSIVE amount of space (less than 10 graphics totaling less than 20 seconds used up 20% of the 32kb of memory!) On top of that, finding any errors in the code was a nightmare.

I was looking to make it much more efficient, and the general idea I am getting is that I need the For Loop, but no matter how many videos I watch and tutorials I read I can't get my head wrapped around it.

Say, for example, I wanted to make a plus pattern appear, then disappear 500ms later.

The plus pattern would use LEDs 2, 5, 6, 7, 9 (D3, D6, D7, D8, D9 pins).

How would I go about this?

Any advice would be appreciated.

Here's what I have so far. I have it set up to turn on when I push the push button.
I copied the first For Loop from another code I found on the internet, but I don't really "understand it"... I just changed the array number from the original 6 to 9 which is my number of LEDs.

int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};
int pinCount = 9;          
 
void setup() {
  for (int thisPin = 0; thisPin < pinCount; thisPin++)  {
    pinMode(ledPins[thisPin], OUTPUT);   
}
    pinMode (11, INPUT);
}

void loop() {

}

Where is the reset of the code? Where is the push button part and where are you turning the LEDs on an off?

You should either have a second array with a 1 or 0 in each element to represent an LED is on or off. Or you can use a single integer and use it to turn the LEDs on or off by setting or clearing the bits, then pushing those bits out to the LEDs.

HazardsMind:
Where is the reset of the code? Where is the push button part and where are you turning the LEDs on an off?

You should either have a second array with a 1 or 0 in each element to represent an LED is on or off. Or you can use a single integer and use it to turn the LEDs on or off by setting or clearing the bits, then pushing those bits out to the LEDs.

I deleted the old code. It basically looked like this (this is for a edge spiral where it lights up all the leds on the outside one at a time)

digitalWrite (2, HIGH);
delay(50);
digitalWrite (2, LOW);
delay(50);
digitalWrite (3, HIGH);
delay(50);
digitalWrite (3, LOW);
delay(50);
digitalWrite (4, HIGH);
delay(50);
digitalWrite (4, LOW);
delay(50);
digitalWrite (7, HIGH);
delay(50);
digitalWrite (7, LOW);
delay(50);
digitalWrite (10, HIGH);
delay(50);
digitalWrite (10, LOW);
delay(50);
digitalWrite (9, HIGH);
delay(50);
digitalWrite (9, LOW);
delay(50);
digitalWrite (8, HIGH);
delay(50);
digitalWrite (8, LOW);
delay(50);
digitalWrite (5, HIGH);
delay(50);
digitalWrite (5, LOW);
delay(50);
digitalWrite (2, HIGH);
delay(50);
digitalWrite (2, LOW);
delay(50);

So yeah... very long and clunky... easy to make a mistake and not notice... and huge wast of memory :frowning:
And that is ONE graphic... out of 10 or 20 of them :confused:

Delta_G:
What don't you understand about that for loop?

How to do it again. I just copied and pasted another code. Then I changed the names of the variables. But I don't really understand how they work, I just understand that they do. I can't recreate them without looking at one that's doing exactly what I want to do, or very close.

If you are having a difficult time understanding what that for loop is doing then I strongly suggest you go back and learn the basic. I'm not making fun of you or anything, but if you want us to help you, then you need to first know what we are talking about. You need to know how the software works, what methods (functions) are available to you, how they work and how to use them.

The Arduino IDE comes with plenty of good basic examples that will help you get started and learn what everything does. There is also the Arduino Reference page Arduino - Home that will help you too.

See you when you get back.

Try this, but no guarantees that it will work as I don't have the hardware to test it.

const byte ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10};  //list of LED pins
const byte plusPins[] = {1, 4, 5, 6, 7};  //index to LED pins to draw a plus
byte state = HIGH;

void setup()
{
  Serial.begin(115200);
  for (int x = 0; x < 9; x++)
  {
    pinMode(ledPins[x], OUTPUT);
  }
}

void loop()
{
  for (int p = 0; p < 5; p++)  
  {
    digitalWrite(ledPins[plusPins[p]], state);  //turn on the plus pins
  }
  delay(100);
  state = !state;
}