How to combine two codes and make them work

So I am very new to arduino. I am making led lights for first responders and need some help with this. I have a code that uses two buttons. Click one and the led comes on, click the other and the led turns off. I also have a code to make several leds flash randomly. I am trying to insert sed code into the first code so when I press the first button, the leds use the flash code, and when i press the second button, everything cuts off. All the help I can get is much appreciated!

Button Code:

const int buttonPin[] = {2,3};     // the number of the pushbutton pins
const int ledPin =  12;      // the number of the LED pin

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

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // 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:   
      digitalWrite(ledPin, HIGH); 
    if (buttonState == HIGH && buttonPin[x] == 3) {
      // turn LED off:
      digitalWrite(ledPin, LOW);

Flash Code:

#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() {
  for(int i = 0; i< numberOfLights; i++) {
    pinMode(pins[i], OUTPUT);
    changeTime[i] = millis() + random(1000, 200);
    pinState[i] = LOW;

void loop() {
  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();

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

I have attached the files if that helps.

random_flash.ino (937 Bytes)

sketch_may08b.ino (972 Bytes)

Have a look at this merge code demo.


Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” button above the posting area (It looks like a scroll with < > inside it).

How to use this forum

I tried to fix it