HELP! Cant combine two codes!

so i recently built a floating bed and wanted led lights under it. I bought a strip of RGB leds and programmed it to react to music using a homemade micro-controller and a arduino nano. This is the code i used:

int val = 0;
void setup() {
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  Serial.begin(9600);
  
}

void loop() {
 val = analogRead(0);
 if(val >= 20 && val < 66)
 {
    analogWrite(9, val * 3.5);
    analogWrite(10, 0);
    analogWrite(11, 0);
  }
   if (val >= 66 && val < 132) // glow green
    {
        analogWrite(9, 0);
        analogWrite(10, val * 2);
        analogWrite(11, 0);
    }
    if (val >= 132 && val < 198) // glow blue
    {
        analogWrite(9, 0);
        analogWrite(10, 0);
        analogWrite(11, val * 1.2);
    }
    if (val >= 198 && val < 264) // glow orange
    {
        analogWrite(9, val - 14);
        analogWrite(10, val - 14);
        analogWrite(11, 0);
    }
    if (val >= 264 && val < 330) // glow cyan
    {
        analogWrite(9, 0);
        analogWrite(10, val / 2 + 90);
        analogWrite(11, val / 2 + 90);
    }
    if (val >= 330) //glow purple
    {
        analogWrite(9, val / 3);
        analogWrite(10, 0);
        analogWrite(11, val / 3);
    }
    if (val < 20)
    {
     analogWrite(9, 0);
     analogWrite(10, 0);
     analogWrite(11, 0);
    }
    delay(100);

 Serial.println(val);


}

Next, I wiped that code and installed a code that allows for the lights the fade in and out randomly.
Here is that code:

#define NR_OF_LIGHTS 3 //defines variables for lights
int pins[NR_OF_LIGHTS] = { 9, 10, 11 }; 

int values[NR_OF_LIGHTS];
int steps[NR_OF_LIGHTS];

#define NR_OF_FADESTEPS 4  //defines variables for fade
int fadesteps[NR_OF_FADESTEPS] = { 192, 128, 64, 0 }; 
int fade_delay = 100; // millisec
int fade_cycles = 3000;

int nr_of_blinks = 2;   
int blink_delay = 400; //millisec

int effect = 1;

int randomlights[NR_OF_LIGHTS];
bool chosenlights[NR_OF_LIGHTS];


/**
*
*/
void setup() { 
  randomSeed(analogRead(1));
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   values[i] = (int)random(230) + 13; // start values between 'max min' and 'min max'
   steps[i] = (int)random(4) + 1; // steps between 1 and 4
 }
} 

/**
*
*/

void loop() { 

 for (int j = 0; j < fade_cycles; j++) {
   for (int i = 0; i < NR_OF_LIGHTS; i++) {
     fadingLight(i);  
   }
   delay(fade_delay);
 }

 if (effect == 1) {
   blinkAll();
   effect = 2;
 }
 else if (effect == 2) {
   runningLight(0);
   effect = 3;
 }
 else if (effect == 3) {
   runningLight(255);
   effect = 1;
 }
} 

/**
*
*/
void fadingLight(int i) {
 
 int minvalue = (NR_OF_FADESTEPS * abs(steps[i])) + 1;
 int maxvalue = 255 - minvalue;

 int fs = NR_OF_FADESTEPS;
 for (int j = 0; fs > 0; fs--, j++) {
   if (values[i] > fadesteps[j]) {
       break;
   }
 }
 values[i] += fs * steps[i];
 
 if (values[i] > maxvalue  ||  values[i] < minvalue) {
     steps[i] *= -1;
 }
 
 analogWrite(pins[i], values[i]);
}

/**
*
*/
void setAllLights(int value) {
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   analogWrite(pins[i], value);
 }
}

/**
*
*/
void blinkAll() {
 for (int i = 0; i < nr_of_blinks; i++) {
   setAllLights(255);
   delay(blink_delay);
   setAllLights(0);
   delay(blink_delay);
 }
}

/**
*
*/
void runningLight(int startvalue) {
 setAllLights(startvalue);
 for (int j = 0; j < 2; j++) {
   randomize();
   for (int i = 0; i < NR_OF_LIGHTS; i++) {
     analogWrite(pins[randomlights[i]], 255 - startvalue);
     delay(200);
     analogWrite(pins[randomlights[i]], startvalue);
   }
 }
}


/**
*
*/
void randomize() {
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   chosenlights[i] = false;
 }
 //first one always ok
 int r = (int)random(NR_OF_LIGHTS);
 randomlights[0] = r;
 chosenlights[r] = true;
 //next 4
 for (int i = 1; i < 5; i++) {
   while (true) {
     r = (int)random(NR_OF_LIGHTS);
     if (chosenlights[r] == false) {
       break;
     }
   }
   randomlights[i] = r;
   chosenlights[r] = true;
 }
 //last one  
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   if (chosenlights[i] == false) {
     randomlights[5] = i;
     break;
   }
 }
}

I have a switch and i would like to hook it up to digital pin 2. When it is on i want the lights to fade in and out (second code) and when it is off, react to music (first code). So naturally i did an If command statement. The resulting code goes as follows:

#define NR_OF_LIGHTS 3 //defines variables for lights
int pins[NR_OF_LIGHTS] = { 9, 10, 11 }; 

int values[NR_OF_LIGHTS];
int steps[NR_OF_LIGHTS];

#define NR_OF_FADESTEPS 4  //defines variables for fade
int fadesteps[NR_OF_FADESTEPS] = { 192, 128, 64, 0 }; 
int fade_delay = 100; // millisec
int fade_cycles = 3000;

int nr_of_blinks = 2;   
int blink_delay = 400; //millisec

int effect = 1;

int randomlights[NR_OF_LIGHTS];
bool chosenlights[NR_OF_LIGHTS];

char x = analogRead(3);
int val = 0;
int a = 0;
int z = 0;

/**
*
*/
void setup() { 
  randomSeed(analogRead(1));
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   values[i] = (int)random(230) + 13; // start values between 'max min' and 'min max'
   steps[i] = (int)random(4) + 1; // steps between 1 and 4
 }
   pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(2, INPUT);
  Serial.begin(9600);
 
} 

void loop() {
   z = digitalRead(2);
 if(z == 1){
  for (int j = 0; j < fade_cycles; j++) {
   for (int i = 0; i < NR_OF_LIGHTS; i++) {
     fadingLight(i);  
   }
   delay(fade_delay);
 }

 if (effect == 1) {
   blinkAll();
   effect = 2;
 }
 else if (effect == 2) {
   runningLight(0);
   effect = 3;
 }
 else if (effect == 3) {
   runningLight(255);
   effect = 1;
 }
 Serial.println(z);
}
 if(z == 0){
   val = analogRead(0);
 if(val >= 20 && val < 66)
 {
    analogWrite(9, val * 3.5);
    analogWrite(10, 0);
    analogWrite(11, 0);
  }
   if (val >= 66 && val < 132) // glow green
    {
        analogWrite(9, 0);
        analogWrite(10, val * 2);
        analogWrite(11, 0);
    }
    if (val >= 132 && val < 198) // glow blue
    {
        analogWrite(9, 0);
        analogWrite(10, 0);
        analogWrite(11, val * 1.2);
    }
    if (val >= 198 && val < 264) // glow orange
    {
        analogWrite(9, val - 14);
        analogWrite(10, val - 14);
        analogWrite(11, 0);
    }
    if (val >= 264 && val < 330) // glow cyan
    {
        analogWrite(9, 0);
        analogWrite(10, val / 2 + 90);
        analogWrite(11, val / 2 + 90);
    }
    if (val >= 330) //glow purple
    {
        analogWrite(9, val / 3);
        analogWrite(10, 0);
        analogWrite(11, val / 3);
    }
    if (val < 20)
    {
     analogWrite(9, 0);
     analogWrite(10, 0);
     analogWrite(11, 0);
    }
    delay(100);

 Serial.println(val);
 }
}
// *
// *
// *
void fadingLight(int i) {
 
 int minvalue = (NR_OF_FADESTEPS * abs(steps[i])) + 1;
 int maxvalue = 255 - minvalue;

 int fs = NR_OF_FADESTEPS;
 for (int j = 0; fs > 0; fs--, j++) {
   if (values[i] > fadesteps[j]) {
       break;
   }
 }
 values[i] += fs * steps[i];
 
 if (values[i] > maxvalue  ||  values[i] < minvalue) {
     steps[i] *= -1;
 }
 
 analogWrite(pins[i], values[i]);
}

/**
*
*/
void setAllLights(int value) {
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   analogWrite(pins[i], value);
 }
}

/**
*
*/
void blinkAll() {
 for (int i = 0; i < nr_of_blinks; i++) {
   setAllLights(255);
   delay(blink_delay);
   setAllLights(0);
   delay(blink_delay);
 }
}

/**
*
*/
void runningLight(int startvalue) {
 setAllLights(startvalue);
 for (int j = 0; j < 2; j++) {
   randomize();
   for (int i = 0; i < NR_OF_LIGHTS; i++) {
     analogWrite(pins[randomlights[i]], 255 - startvalue);
     delay(200);
     analogWrite(pins[randomlights[i]], startvalue);
   }
 }
}


/**
*
*/
void randomize() {
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   chosenlights[i] = false;
 }
 //first one always ok
 int r = (int)random(NR_OF_LIGHTS);
 randomlights[0] = r;
 chosenlights[r] = true;
 //next 4
 for (int i = 1; i < 5; i++) {
   while (true) {
     r = (int)random(NR_OF_LIGHTS);
     if (chosenlights[r] == false) {
       break;
     }
   }
   randomlights[i] = r;
   chosenlights[r] = true;
 }
 //last one  
 for (int i = 0; i < NR_OF_LIGHTS; i++) {
   if (chosenlights[i] == false) {
     randomlights[5] = i;
     break;
   }
 }
}

When i ran that code, only the code that faded the lights in and out appeared whether or not the switch was on. Do i need to do my fading code a different way, call each file separately inside of a main code(attaching files), or something else? Any help would be appreciated

music_red_dimmer.ino (1.15 KB)

fading_lights.ino (2.54 KB)

music_dimmer_and_fader.ino (3.79 KB)

How is the switch wired?

.

Attach code using the </> icon on the left side of the posting menu.
Put your sketch between the code tags [code][/code]

LarryD:
How is the switch wired?

.

It is a simple toggle switch. It is most like s1. +5v goes to switch input then the output goes to digital pin 2 and the GND goes to GND.

Also thanks i was wondering how to do that.

It is a simple toggle switch. It is most like s1. +5v goes to switch input then the output goes to digital pin 2 and the GND goes to GND.

The ground of what? Where is the resistor in your description?

It does not sound to me like your switch is wired correctly. Draw a picture and post it.

PaulS:
The ground of what? Where is the resistor in your description?

It does not sound to me like your switch is wired correctly. Draw a picture and post it.

it looks a lot like this:

except it is a toggle switch instead of a pushbutton

If you have a volt meter, measure the voltage on pin 2.
Press and release the switch.
You should see a swing between 5 and 0 volts.

.

LarryD:
If you have a volt meter, measure the voltage on pin 2.
Press and release the switch.
You should see a swing between 5 and 0 volts.

.

it does
i believe it has to do with my program

With the power off, the resistance from pin 2 to GND should be 10k (brown black orange = 10k)

Put some debug serial print statements at strategic spots to prove sections of code are being reached when the switch is toggled.
ex:
Serial.println("I got this far");

LarryD:
With the power off, the resistance from pin 2 to GND should be 10k (brown black orange = 10k)

Put some debug serial print statements at strategic spots to prove sections of code are being reached when the switch is toggled.
ex:
Serial.println("I got this far");

okay i will try that. Thank you. Also, my resistor is 10k.