Mega controlling 12 LED patterns via buttons?

I would like to use an Arduino Mega to have 13 digital outputs (LED), 13 inputs (arcade-style buttons), and 1 analog input (potentiometer).

When a button is pressed, I would like to have a pattern of LEDs flash, then stop. Another button is pressed, another LED pattern is run and then stop.
The potentiometer be able to change the delay time between each LED in sequence.


Once this project is successful, I would like to add into the outputs of each of the LEDs, another wire which would push a higher voltage (24v) to a solenoid switch. In essence, the LEDs would become a status light for the solenoids.

Buttons:
13 buttons
Rules
OFF until pressed
RUN then STOP
Each button press will finish, even if other button is pressed
Delay of “n” ms in between button presses
One Button will be KILL SWITCH (interruptor), to stop everything, no delay.

Plus 1 Potentiometer
Analog (pin2)
Global ms spread (effects all patterns)
Between 30ms and 1500ms

Outputs:
13 total LEDs (later LED/higher voltage output)

Can the arduino (connected to a 9v battery), power all of the LEDs if they are tuned on at once(I’m almost perfectly sure it can)?

Is this possible?

ps. How can I add the solenoid switching (it could be at the regular 5v output) within the LED line?

Image is a (very starting) schematic of wiring (not inclusive of the total # of buttons, and not including the kill button).

Many thanks in advance!
Raffikki

Can the arduino (connected to a 9v battery), power all of the LEDs if they are tuned on at once(I’m almost perfectly sure it can)?

Is this possible?

ps. How can I add the solenoid switching (it could be at the regular 5v output) within the LED line?

A 9V battery is not the best choice, determine how much the HW uses under max load. Better use a wall wart.

The application is very well possible. Start small, one button, one led, read about Arrays in the reference section.

Better use a separate powersupply for the solenoids (connect the GND’s with the Arduino) to be save. In the end it depends on the spec of the solenoids and how much current is drawn.

byte button[12] = {1,2,3, ... };
byte led[12] = {15,16,17, ...};
byte stopButton = 13;

byte buttonState[12]; holds the last keypresses;

void setup()
{
  Serial.begin(115200); // for testing
  for (byte i =0; i< 12; i++) pinMode(button[i], INPUT);
  for (byte i =0; i< 12; i++) pinMode(led[i], OUPUT);
}

void loop()
{
  // READ BUTTONS
  for (byte i =0; i< 12; i++) 
  {
     buttonState[i] = digitalRead(button[i]);
  }
  // HANDLE STOP
  if (digitalRead(stopButton) == LOW)
  {
    allOff();
    return; // start loop again 
  }

  // TIME FOR ACTION?
  for (byte i =0; i< 13; i++) 
  {
     if ( buttonState[i] == HIGH)
     {
         execPattern(i);
     }
  }
}

void execPattern(int nr)
{
  switch (i)
  {
  case 1: patternOne();  // to be elaborated;
    break;
  case 2: patternTwo();
    break;
  // etc 
  default: allOff();  // to be elaborated;
     break;
  }
}


// DUMMIES 
void patternOne()
{
  Serial.println("patternOne");
  digitalWrite(led[0], HIGH);
  delay(1000);
  digitalWrite(led[0], LOW);
}

void patternTwo()
{
  Serial.println("patternTwo");
}

void allOff()
{
  Serial.println("off");
}

this should get you started; please note that I did not test/compile the above code

Hey, thanks much for the starter code, and the info about 9v’s being beneficial, I’ll look into a larger capacity battery, or a wall wort. This is for a Pyro pattern maker, and I hope to have some videos of this for you all when I’m done!

As I am sure you can guess, I’m a beginner at the arduino (although I am well-versed in low-voltage wiring, etc) and the C++ code. I have figured out the LED patterns, and now I’m trying to just get the patterns into the code, after which I will get some button code added in, then the potentiometer and the rest of the code (clean-up, etc.). Based on your sample, is this the correct way to build the patterns?

void execPattern(int nr)
{
  switch (i)
  {
  case 1: tulip_backLow();  // to be elaborated;
    break;
  case 2: S_pattern();
    break;
  // etc 
  default: allOff();  // to be elaborated;
     break;
  }
}


// Patterns_of_LEDS 
void tulip_backLow()
{
  Serial.println("tulip_backLow");
  digitalWrite(led[22], HIGH);
  delay(400);
  digitalWrite(led[22], LOW);
  digitalWrite(led[23,24], HIGH);
  delay(400);
  digitalWrite(led[23,24], LOW);  
  digitalWrite(led[30,34], HIGH);
  delay(400);
  digitalWrite(led[30,34], LOW);
  digitalWrite(led[31,33], HIGH);
  delay(400);
  digitalWrite(led[31,33], LOW);
  digitalWrite(led[32], HIGH);
  delay(400);
}

void S_pattern()
{
  Serial.println("S_pattern");
  digitalWrite(led[32], HIGH);
  delay(400);
  digitalWrite(led[32], LOW);
  digitalWrite(led[33], HIGH);
  delay(400);
  digitalWrite(led[33], LOW);  
  digitalWrite(led[34], HIGH);
  delay(400);
  digitalWrite(led[34], LOW);
  digitalWrite(led[32], HIGH);
  delay(400);
  digitalWrite(led[32], LOW);
  digitalWrite(led[31], HIGH);
  delay(400);
  digitalWrite(led[31], LOW);
  digitalWrite(led[30], HIGH);
  delay(400);
  digitalWrite(led[30], LOW);
  digitalWrite(led[27], HIGH);
  delay(400);
  digitalWrite(led[27], LOW);
  digitalWrite(led[28], HIGH);
  delay(400);
  digitalWrite(led[28], LOW);
  digitalWrite(led[29], HIGH);
  delay(400);
  digitalWrite(led[29], LOW);
    digitalWrite(led[24], HIGH);
  delay(400);
  digitalWrite(led[24], LOW);
   digitalWrite(led[27], HIGH);
  delay(400);
  digitalWrite(led[27], LOW);
   digitalWrite(led[26], HIGH);
  delay(400);
  digitalWrite(led[26], LOW);
   digitalWrite(led[25], HIGH);
  delay(400);
  digitalWrite(led[25], LOW);
   digitalWrite(led[24], HIGH);
  delay(400);
  digitalWrite(led[24], LOW);
   digitalWrite(led[23], HIGH);
  delay(400);
  digitalWrite(led[23], LOW);
   digitalWrite(led[22], HIGH);
  delay(400);
  digitalWrite(led[22], LOW);
}

Edit:
also curious if this makes correct usage of the code-style, as pins 22-34 are LEDs, and pins 40-46 are Buttons:

void setup() {
  for (int thisPin = 22; thisPin < 35; thisPin++)  {
    pinMode(thisPin, OUTPUT);   
  for (int thisPin = 40; thisPin < 47; thisPin++)  {
    pinMode(thisPin, INPUT);          
  }
    }
}