merging codes with if/else statements and getting lag

Hi there! Hope you can help me understand what i'm doing wrong...
I've merged 2 different codes together using an 'else' statement...one that fades the leds randomly and one that is sound-reactive. Both codes work well independently of each other, but once merged....I'm getting flickers in the led fade code. Can you help me understand how to merge these 2 codes together cleanly, so that the fading LEDs aren't flickering?
I used /////////" " to indicate whether the code underneath corresponds to the fade code or the sound-reactive code.

Thanks in advance for your help :slight_smile:

////////////// fade specs 

#include <LEDFader.h> 
#define LED_NUM 6
// 6 LEDs (perhaps 2 RGB LEDs)
LEDFader leds[LED_NUM] = {
  LEDFader(3),
  LEDFader(5),
  LEDFader(6),
  LEDFader(9),
  LEDFader(10),
  LEDFader(11)
};

////////////// sound reactive specs

const byte totalLeds=11;
int incomingAudio[30]={};
int averageAudio=0;
int ticker=1;
byte peak=1;

void setup(){
  for(byte i=0;i<=totalLeds;i++)
    pinMode(2+i, OUTPUT); // define LEDs
}

void loop(){

  ////////////////// fade code

   // Update all LEDs and start new fades if any are done
  for (byte i = 0; i < LED_NUM; i++) {
    LEDFader *led = &leds[i];
    led->update();

    // Set new fade
    if (led->is_fading() == false) {
      int duration = random(1000, 3000); // between 1 - 3 seconds

      // Up
      if (led->get_value() == 0) {
        byte color = random(100, 255);  //100,255
        led->fade(color, duration);
      }
      // Down
      else {
        led->fade(0, duration); //0
      }
    }
   // */ 
    
        else{
      
    
  
  ////////////////// sound reactive code
  
  
  incomingAudio[ticker%30]=analogRead(A5); // put in the mic reading at A5 into a spot in the array
  digitalWrite(2+peak,LOW); // turn off the peak LED (sort of "PWM" to make it dimmer than usual)
  if(ticker%30==0){ // read our averages every 30 'ticks'
    for(byte i=0;i<30;i++)averageAudio+=incomingAudio[i];
    averageAudio/=30; // put our average reading into this variable
    for(byte i=0;i<=totalLeds;i++){
      digitalWrite(2+i,LOW); // turn off all LEDs
    }
    for(float i=0;i<=totalLeds;i++){ // This loop basically turns on as many LEDs as there is 'loudness' into the mic
      if(i>averageAudio/90-2)break; // NOTE: tweak around with this line to change how the LEDs respond to different values
//       I've messed around with it and found that this seems to work well for 11 LEDs or so
      if(peak<i)peak=i; // This sets the 'peak' LED to the highest value it sees
      digitalWrite(2+i,HIGH);
    }
    averageAudio=0; // reset the average
    if(ticker>90*5){ticker=1;if(peak>0)peak--;} // this basically determines the speed at which the peak LED falls back down, and resets the ticker so it doesn't overflow
    digitalWrite(2+peak,HIGH);
  }
  ticker++;
}
//*/
      }
    }

You can try making each section of code its own function and then call the function. something like...

#include <LEDFader.h> 
#define LED_NUM 6
// 6 LEDs (perhaps 2 RGB LEDs)
LEDFader leds[LED_NUM] = {
  LEDFader(3),
  LEDFader(5),
  LEDFader(6),
  LEDFader(9),
  LEDFader(10),
  LEDFader(11)
};

////////////// sound reactive specs

const byte totalLeds=11;
int incomingAudio[30]={};
int averageAudio=0;
int ticker=1;
byte peak=1;



void ledfade()
{
// Update all LEDs and start new fades if any are done
  for (byte i = 0; i < LED_NUM; i++) {
    LEDFader *led = &leds[i];
    led->update();

    // Set new fade
    if (led->is_fading() == false) {
      int duration = random(1000, 3000); // between 1 - 3 seconds

      // Up
      if (led->get_value() == 0) {
        byte color = random(100, 255);  //100,255
        led->fade(color, duration);
      }
      // Down
      else {
        led->fade(0, duration); //0
      }
    }
   // */ 
    

void soundreact()
{
 incomingAudio[ticker%30]=analogRead(A5); // put in the mic reading at A5 into a spot in the array
  digitalWrite(2+peak,LOW); // turn off the peak LED (sort of "PWM" to make it dimmer than usual)
  if(ticker%30==0){ // read our averages every 30 'ticks'
    for(byte i=0;i<30;i++)averageAudio+=incomingAudio[i];
    averageAudio/=30; // put our average reading into this variable
    for(byte i=0;i<=totalLeds;i++){
      digitalWrite(2+i,LOW); // turn off all LEDs
    }
    for(float i=0;i<=totalLeds;i++){ // This loop basically turns on as many LEDs as there is 'loudness' into the mic
      if(i>averageAudio/90-2)break; // NOTE: tweak around with this line to change how the LEDs respond to different values
//       I've messed around with it and found that this seems to work well for 11 LEDs or so
      if(peak<i)peak=i; // This sets the 'peak' LED to the highest value it sees
      digitalWrite(2+i,HIGH);
    }
    averageAudio=0; // reset the average
    if(ticker>90*5){ticker=1;if(peak>0)peak--;} // this basically determines the speed at which the peak LED falls back down, and resets the ticker so it doesn't overflow
    digitalWrite(2+peak,HIGH);
  }
  ticker++;
}
//*/
      }
    }

void setup(){
  for(byte i=0;i<=totalLeds;i++)
    pinMode(2+i, OUTPUT); // define LEDs
}

void loop()
{

ledfade();
soundreact();
}

Doing things this way as helped me numerous times. Google "severalthingsatonce" and you will see more examples similar to what I pasted above.

Bill

You seem to have several unmatched comment delimiters. Your code is a mess.

It looks like you are trying to interleave the two actions without any sort of plan.

Have a look at how the code is organized in planning and implementing a program - especially the use of functions.

Notice that the functions are called from loop() and each function only does a little piece of its business on each call so as to make time available for the other functions.

That is also the principle of several things at a time

...R

yeah like the others are saying...

if /else means you do one or the other.

You need to do both at the same time, AND in the end of a display cycle control the duration.

The flicker/crap you are seeing is likely your sound work that flickers into life for a brief moment... It has no delays it seems. In fact you colud try to remove the if/else and run them back to back to see how they superpose.
I suspect since you pass delays to your LED functions that the main loop is rather slow repeating itself, so it might not be much better. Try to add a 100 ms delay to the main loop at the end to see what's going on.