help combining two codes for RGB fade and sound

for my project i am trying to make a 5050 LED strip fade through all the colors( i have achieved this)
but combine it with LEDs reacting to sound from a sound sensor (i have achieved this). However i am having trouble combining these two separate codes.
Code for fade colors:

// Assign LED Output PWM Pins
int Red = 5;
int Green = 6;
int Blue = 3;

//Set Initial Values
int RedVal = 0;
int GreenVal = 0;
int BlueVal = 0;
int fade = 10;  // Delay Time

// Colour Value 1 as each colour intensity (0-255)
int RedVal1 = 186;
int GreenVal1 = 0;
int BlueVal1 = 255;

// Colour Value 2
int RedVal2 = 9;
int GreenVal2 = 239;
int BlueVal2 = 26;

//Colour Value 3
int RedVal3 = 255;
int GreenVal3 = 120;
int BlueVal3 = 0;

//Colour Value 4
int RedVal4 = 0;
int GreenVal4 = 255;
int BlueVal4 = 78;

//Set initial mode (Colour Value Mode) to Colour Value 1
int mode = 1;

void setup() {


 //----------------------- Assign outputs
pinMode(Red, OUTPUT);
  pinMode(Green, OUTPUT);
  pinMode(Blue, OUTPUT);

 //----------------------- Write Initial Values of 0 to outputs
   analogWrite(Red, RedVal);
   analogWrite(Green, GreenVal);
   analogWrite(Blue, BlueVal);

}

void loop() {

 while(mode == 1){
 
    if(RedVal < RedVal1){              // If RedVal is less than desired RedVal1
      RedVal ++;                       // increment RedVal
    } else if(RedVal > RedVal1){       // If RedVal is more than desired RedVal1
      RedVal --;                       // decrement RedVal
    } else if(RedVal == RedVal1){      // If RedVal is equal to desired RedVal1
      //Do Nothing 
    }
                                       // Repeated as above for BlueVal
    if(BlueVal < BlueVal1){
      BlueVal ++;
    } else if(BlueVal > BlueVal1){
      BlueVal --;
    } else if(BlueVal == BlueVal1){
      //Do Nothing
    }
                                       // Repeated as above for GreenVal
    if(GreenVal < GreenVal1){
      GreenVal ++;
    } else if (GreenVal > GreenVal1){
      GreenVal --;
    } else if (GreenVal == GreenVal1){
      // Do Nothing
    }
                                       // Now we have a new set of values, we write them to the PWM Pins.
    analogWrite(Red, RedVal);
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);
    delay(fade);                       // Implement a bit of delay to slow the change of colour down a bit


    }
    if(RedVal == RedVal1 && GreenVal == GreenVal1 && BlueVal == BlueVal1){ // If RedVal & GreenVal & BlueVal are all at the desired values
      delay(1500);                     // Delay to hold this colour for a little while
      mode = 2;                        // Change the mode to the next colour. Exiting this while loop and into the next one
    }

  while(mode == 2){

    
    if(RedVal < RedVal2){
      RedVal ++;                     
    } else if(RedVal > RedVal2){
      RedVal --;                     
    } else if(RedVal == RedVal2){
      //Do Nothing 
    }

    if(BlueVal < BlueVal2){
      BlueVal ++;
    } else if(BlueVal > BlueVal2){
      BlueVal --;
    } else if(BlueVal == BlueVal2){
      //Do Nothing
    }

    if(GreenVal < GreenVal2){
      GreenVal ++;
    } else if (GreenVal > GreenVal2){
      GreenVal --;
    } else if (GreenVal == GreenVal2){
      // Do Nothing
    }

    analogWrite(Red, RedVal);
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);
    delay(fade);

        

    if(RedVal == RedVal2 && GreenVal == GreenVal2 && BlueVal == BlueVal2){
      delay(1500);
      mode = 3;
     //break;
    }
  }

  while(mode == 3){

    
    if(RedVal < RedVal3){
      RedVal ++;
    } else if(RedVal > RedVal3){
      RedVal --;
    } else if(RedVal == RedVal3){
      //Do Nothing 
    }

    if(BlueVal < BlueVal3){
      BlueVal ++;
    } else if(BlueVal > BlueVal3){
      BlueVal --;
    } else if(BlueVal == BlueVal3){
      //Do Nothing
    }

    if(GreenVal < GreenVal3){
      GreenVal ++;
    } else if (GreenVal > GreenVal3){
      GreenVal --;
    } else if (GreenVal == GreenVal3){
      // Do Nothing
    }

    analogWrite(Red, RedVal);
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);
    delay(fade);

   

    if(RedVal == RedVal3 && GreenVal == GreenVal3 && BlueVal == BlueVal3){
      delay(1500);
      mode = 4;
      //break;
    }
  }

  while(mode == 4){
    
    if(RedVal < RedVal4){
      RedVal ++;
    } else if(RedVal > RedVal4){
      RedVal --;
    } else if(RedVal == RedVal4){
      //Do Nothing 
    }

    if(BlueVal < BlueVal4){
      BlueVal ++;
    } else if(BlueVal > BlueVal4){
      BlueVal --;
    } else if(BlueVal == BlueVal4){
      //Do Nothing
    }

    if(GreenVal < GreenVal4){
      GreenVal ++;
    } else if (GreenVal > GreenVal4){
      GreenVal --;
    } else if (GreenVal == GreenVal4){
      // Do Nothing
    }

    analogWrite(Red, RedVal);
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);
    delay(fade);



    if(RedVal == RedVal4 && GreenVal == GreenVal4 && BlueVal == BlueVal4){
      delay(1500);
      mode = 1;                                 // Set mode back to 1 to return to the original colour.
      //break;
    }
  }

}

code for LEDS turning on with sound:

int DO = 2; //Pin for Digital Output - DO
int DA = A0; // Pin for Analog Output - AO
int threshold = 237; //Set minimum threshold for LED lit
int sensorvalue = 0;

#define REDPIN 5  
#define GREENPIN 6
#define BLUEPIN 3

int redNow;
int blueNow;
int greenNow;
int redNew;
int blueNew;
int greenNew;


void setup() {
  //Serial.begin(9600);
pinMode(REDPIN, OUTPUT);
  pinMode(GREENPIN, OUTPUT);
  pinMode(BLUEPIN, OUTPUT);

 redNow = random(255);
  blueNow = random(255);
  greenNow = random(255);
  redNew = redNow;
  blueNew = blueNow;
  greenNew = greenNow;


  
}

void loop() {


  sensorvalue = analogRead(DA);  //Read the analog value
  //Serial.print("Analog: ");
  //Serial.print(sensorvalue);  //Print the analog value
  //Serial.print("  ");
  //Serial.print("Digital: ");
  //Serial.println(digitalRead(DO));  //Print the digital value
 
  if (sensorvalue >= threshold) { //Compare analog value with threshold
    analogWrite(BLUEPIN, blueNow);
    analogWrite(REDPIN, redNow);
    analogWrite(GREENPIN, greenNow);
    redNew = random(255);
    blueNew = random(255);
    greenNew = random(255);


            analogWrite(BLUEPIN, blueNew);
      analogWrite(REDPIN, redNew);
      analogWrite(GREENPIN, greenNew);
      delay(1);
    }
  }

has anyone done this before? or can guide me. I tried putting the sound code within the fade code withing every while mode 1,2,3,4 but it would just make the fade go from smoothly fading, to fading and flickering (but not very noticeable)

My goal is to: make the fade "stop" when the sound sensor reaches analog> threshold and continue when no sound is heard. OR make a random color pop out when the analog> threshold and then continue the fade when no sound is heard.

initial step might be to rewrite the fade color's code to eliminate the sequential while loops: essentially you have the same logic 4 times with different values.

The way to solve this is with arrays

Instead of

// Colour Value 1 as each colour intensity (0-255)
int RedVal1 = 186;
int GreenVal1 = 0;
int BlueVal1 = 255;

// Colour Value 2
int RedVal2 = 9;
int GreenVal2 = 239;
int BlueVal2 = 26;

//Colour Value 3
int RedVal3 = 255;
int GreenVal3 = 120;
int BlueVal3 = 0;

//Colour Value 4
int RedVal4 = 0;
int GreenVal4 = 255;
int BlueVal4 = 78;

You might write (keeping to 1-dimensional arrays for now)

int RedVals[]={186,9,255,0};
int GreenVals[]={0,239,120,255};
int BlueVals[]={255,26,0,78};

int mode = 0;  // arduino arrays are 0..3 rather than 1..4
//etc

void setup() {
  //etc
}

void loop() {
    if(RedVal < RedVals[mode]){              // If RedVal is less than desired RedVal1
      RedVal ++;                       // increment RedVal
    } else if(RedVal > RedVals[mode]){       // If RedVal is more than desired RedVal1
      RedVal --;                       // decrement RedVal
    } else if(RedVal == RedVals[mode]){      // If RedVal is equal to desired RedVal1
      //Do Nothing
    }
                                       // Repeated as above for BlueVal
    if(BlueVal < BlueVals[mode]){
      BlueVal ++;
    } else if(BlueVal > BlueVals[mode]){
      BlueVal --;
    } else if(BlueVal == BlueVals[mode]){
      //Do Nothing
    }
                                       // Repeated as above for GreenVal
    if(GreenVal < GreenVals[mode]){
      GreenVal ++;
    } else if (GreenVal > GreenVals[mode]){
      GreenVal --;
    } else if (GreenVal == GreenVals[mode]){
      // Do Nothing
    }
                                       // Now we have a new set of values, we write them to the PWM Pins.
    analogWrite(Red, RedVal);
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);
    delay(fade);                       // Implement a bit of delay to slow the change of colour down a bit


    }
    if(RedVal == RedVals[mode] && GreenVal == GreenVals[mode] && BlueVal == BlueVals[mode]){ // If RedVal & GreenVal & BlueVal are all at the desired values
      delay(1500);                     // Delay to hold this colour for a little while
      mode = mode +1 ;                        // Change the mode to the next colour.
      if (mode>3) {
         mode=0;
      }; 
   }

}

The above can be rewritten in even more compact fashion using a 2 dimensional array but we wont go there for now :slight_smile:

Then you need to read the "Blink without delay" example sketch to figure out how to make the above work without calling "delay()".

After that, adding the alternate codes will be much easier: you need to detect audio and change the values for RGB in a different way.

If you just want the pattern to stop when audio is above threshold you can just do

void loop() {
    audio=analogRead(pin);
    if (audio < threshold) {

      if(RedVal < RedVals[mode]){              // If RedVal is less than desired RedVal1
        RedVal ++;                       // increment RedVal
      } else if(RedVal > RedVals[mode]){       // If RedVal is more than desired RedVal1
        RedVal --;                       // decrement RedVal
      } else if(RedVal == RedVals[mode]){      // If RedVal is equal to desired RedVal1
        //Do Nothing
      }
      //etc etc
    } else {
      // do nothing here just to just freeze the pattern
    }
    analogWrite(Red, RedVal);
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);

}

yeah i was having a hard time with arrays so i didnt use one. but now that you explained it, it is way simpler. thank you maybe one day ill be able to work with 2d arrays :slight_smile: . i looked at the blink without delay but im confused about one particular line.

unsigned long currentMillis = millis();

what value is given to currentmillis? because on the next line it is doing currentmills- previousmills

millis() is a system function that returns the number of milliseconds since the arduino last rebooted. It will roll over and go back to zero every 50 days approx. See TimingRollover for how to deal with this.

Mostly its used, as you saw, to determine if a certain period has passed by comparing current to a previous value.

Have a look at this thread about 2 dimensional arrays

If you master it you can collapse the separate Red Green and Blue logic sections into a single section with another for loop around it.

i tried using a for loop to replace the delay(fade). the problem is now its fading super quick between all the colors like itll go through all 3 modes in 2 seconds, where as using the delay(fade) where fade=10 it would be nice and slow fade.

here is the code i used. i attempted to replace the delay value of 10 form the previous code to a for loop that loops 10 times. shouldnt it be the same? or what did i do wrong. i been at this code for hours trying to figure out how to do a similar effect to delay without using it.

    if(RedVal < RedVals[mode]){              // If RedVal is less than desired RedVal1
      RedVal ++;                       // increment RedVal
    } else if(RedVal > RedVals[mode]){       // If RedVal is more than desired RedVal1
      RedVal --;                       // decrement RedVal
    } else if(RedVal == RedVals[mode]){      // If RedVal is equal to desired RedVal1
      //Do Nothing
    }
                                       // Repeated as above for BlueVal
    if(BlueVal < BlueVals[mode]){
      BlueVal ++;
    } else if(BlueVal > BlueVals[mode]){
      BlueVal --;
    } else if(BlueVal == BlueVals[mode]){
      //Do Nothing
    }
                                       // Repeated as above for GreenVal
    if(GreenVal < GreenVals[mode]){
      GreenVal ++;
    } else if (GreenVal > GreenVals[mode]){
      GreenVal --;
    } else if (GreenVal == GreenVals[mode]){
      // Do Nothing
    }
    timenow=millis();
    timevalue=timenow;
   for (timenow = timevalue; timenow< timevalue+10;timenow++){// cycle for +10 millis so 10 milliseconds
    analogWrite(Red, RedVal);     // outputting same color for all 3 since its still in the same for loop
    analogWrite(Green, GreenVal);
    analogWrite(Blue, BlueVal);
   }

The arduino processor executes 16 million instructions per second so even 10 times through the loop is pretty fast, as you discovered.

The key (from the Blink without delay sketch) is to wrap your code in an if statement something like

void loop() {
  now=millis();
  if (now - previous >=interval) {
    if(RedVal < RedVals[mode]){
    // etc
    analogWrite(Blue, BlueVal);
    previous = now;
}
}

With this construct you can continually read inputs and react to changes but the output only changes after the programmed interval.

It may make make your code more readable if you move the code that calculates and writes a new RGB values to a function something like

void loop() {
  audio=analogRead(pin);
  now=millis();  
  if (audio < threshold) {
  
    if (now - fade_previous >=fade_interval) {
      RGBFade();  
      fade_previous = now;
    }
  } else {
   if (now - fade_pulse >=pulse_interval) {
      RGBPulse();  
      fade_pulse = now;
    }    
  }
}

void RGBFade()
{
  if (now - previous >=interval) {
    if(RedVal < RedVals[mode]){
    // etc
    analogWrite(Blue, BlueVal);

  }
}
void RGBPulse()
{
   // some other pattern
}