What am I doing wrong?

I have a code that when you press button 1, leds go through a flash pattern. When you press button 2, the leds cut off. However, I have ran into a problem. For some reason, I have to press and hold button 1 for the pattern to flash. When I let go, the pattern freezes where it is in the sequence. I want to be able to just press the button and let go and it continue to go through the pattern. Any help would be appreciated!

Here is the code

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin[] = {2,3};     // the number of the pushbutton pins
      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
#define numberOfLights 16

byte pins[] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
byte pinState[numberOfLights];
long int changeTime[numberOfLights];
int flashRate[numberOfLights];
long flashChange;   // how often to change the flashing patterns

void setup() {
  // initialize the LED pin as an output:
 for(int i = 0; i< numberOfLights; i++) {
    pinMode(pins[i], OUTPUT);
    changeTime[i] = millis() + random(1000, 200);
    pinState[i] = LOW;
  }
  setFlashTime();

  // initialize the pushbutton pin as an input:
  for(int x=0; x<2; x++)
  {
    pinMode(buttonPin[x], INPUT); 
  }  
}

void loop(){
  // read the state of the pushbutton value:
  for(int x=0; x<2; x++)
  {
    buttonState = digitalRead(buttonPin[x]);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH && buttonPin[x] == 2) {    
      // turn LED on:   
      for(int i = 0; i < numberOfLights; i++) {
   if(changeTime[i] <= millis()) {
   pinState[i] = ~pinState[i];
   digitalWrite(pins[i], pinState[i]);
   changeTime[i] = millis() + flashRate[i];
   } 
  }
  if(flashChange <= millis()) setFlashTime();
    }
    
    
    if (buttonState == HIGH && buttonPin[x] == 3) {
      // turn LED off:
      for(int i = 0; i< numberOfLights; i++) {
    digitalWrite(pins[i], LOW);
    }
  }
}
}

void setFlashTime(){
  for(int i=0; i<numberOfLights; i++){
    flashRate[i] = random(100, 500);
  }
  flashChange = millis();  // next time to change pattern
}

You don't have anything in there to control the flashing, after you press button 1. That's why you have to keep your finger on the button.

You need to add code in your loop( ) function, which will do the light cycling during all the iterations of loop( ) where you are not pushing either of the buttons.

I'm new to Arduino and do not know exactly how to do that. I'm still learning

Have a look at planning and implementing a program.

I think you need to break your project into more separate functions and operate the flashing function depending on the state of a variable (the value in the variable) that is set by the push buttons.

The code in your loop() could be something like this

void loop() {
   readButtons();
   updateFlashSetting();
   flashLeds();
}

...R

Thanks! I'll give that a try.

shumatetg: I have a code that when you press button 1, leds go through a flash pattern. When you press button 2, the leds cut off. However, I have ran into a problem. For some reason, I have to press and hold button 1 for the pattern to flash. When I let go, the pattern freezes where it is in the sequence. I want to be able to just press the button and let go and it continue to go through the pattern. Any help would be appreciated!

Well, let's being by rephrasing your requirements.

The sketch is either running the flash pattern or not running the flash pattern.

When button one changes from low to high, the sketch should move into "running the flash pattern" state. When button two changes from low to high, the sketch should move into "not running the flash pattern" state. If the sketch is running the flash pattern, then each loop it should perform one step of the pattern.

I have given my codes several makeovers and still am coming up with the same problem. When I press button 1, the pattern flashes, but stops flashing if I let go of the button. I want to press it once and the flash pattern continues. Thanks everybody for your help in advance!

Here is the code

const unsigned long ledOnMillis = 100;
const unsigned long ledAbaseInterval = 300;
const unsigned long ledBbaseInterval = 500;
const unsigned long ledCbaseInterval = 200;
const unsigned long ledDbaseInterval = 400;
unsigned long ledAInterval = ledAbaseInterval;
unsigned long ledBInterval = ledBbaseInterval;
unsigned long ledCInterval = ledCbaseInterval;
unsigned long ledDInterval = ledDbaseInterval;
byte ledAstate = HIGH;
byte ledBstate = HIGH;
byte ledCstate = HIGH;
byte ledDstate = HIGH;
unsigned long prevLedAMillis;
unsigned long prevLedBMillis;
unsigned long prevLedCMillis;
unsigned long prevLedDMillis;
unsigned long currentMillis;
const byte ledApin = 12;
const byte ledBpin = 11;
const byte ledCpin = 10;
const byte ledDpin = 9;
int switch1Pin = 2;
int switch2Pin = 3;
int switch1State = 0;
int switch2State = 0;

void setup() {
  // put your setup code here, to run once:
pinMode(ledApin, OUTPUT);
pinMode(ledBpin, OUTPUT);
pinMode(ledCpin, OUTPUT);
pinMode(ledDpin, OUTPUT);
pinMode(switch1Pin, INPUT);
pinMode(switch2Pin, INPUT);
}



void loop() {

currentMillis = millis();  
switch1State = digitalRead(switch1Pin);
switch2State = digitalRead(switch2Pin);

  
  if (switch1State == HIGH)
  {
    
    flashLedA();
    flashLedB();
    flashLedC();
    flashLedD();
   
  }
  
  if (switch2State == HIGH)
  {
    digitalWrite(ledApin, LOW);
    digitalWrite(ledBpin, LOW);
    digitalWrite(ledCpin, LOW);
    digitalWrite(ledDpin, LOW);
  }
  }



void flashLedA() {
 if (currentMillis - prevLedAMillis >= ledAInterval) {
   prevLedAMillis += ledAInterval;
   ledAstate = ! ledAstate;
   if (ledAstate == HIGH) {
     ledAInterval = ledOnMillis;
   }
   else {
     ledAInterval = ledAbaseInterval;
   }
   digitalWrite(ledApin, ledAstate);
 }
}
void flashLedB() {
 if (currentMillis - prevLedBMillis >= ledBInterval) {
   prevLedBMillis += ledBInterval;
   ledBstate = ! ledBstate;
   if (ledBstate == HIGH) {
     ledBInterval = ledOnMillis;
   }
   else {
     ledBInterval = ledBbaseInterval;
   }
   digitalWrite(ledBpin, ledBstate);
 }
}
void flashLedC() {
 if (currentMillis - prevLedCMillis >= ledCInterval) {
   prevLedCMillis += ledCInterval;
   ledCstate = ! ledCstate;
   if (ledCstate == HIGH) {
     ledCInterval = ledOnMillis;
   }
   else {
     ledCInterval = ledCbaseInterval;
   }
   digitalWrite(ledCpin, ledCstate);
 }
}
void flashLedD() {
 if (currentMillis - prevLedDMillis >= ledDInterval) {
   prevLedDMillis += ledDInterval;
   ledDstate = ! ledDstate;
   if (ledDstate == HIGH) {
     ledDInterval = ledOnMillis;
   }
   else {
     ledDInterval = ledDbaseInterval;
   }
   digitalWrite(ledDpin, ledDstate);
 }
}

want to press it once and the flash pattern continues.

Forever?

Set a variable to true when the button is pressed. If the variable is true, flash stuff. If you push a different button make the variable false.

Why have you started a new Thread? I gave you advice almost identical to @LarryD’s in your other Thread.

Double posting just wastes time

…R

@shumatetg: Don't cross-post thanks.