Putting together two codes; only one part works

Hi!

I have a problem with mixing together two sketches. They both work separately, the problem is that only one part of the code works when they are put together.
I am also attaching the two other codes separately just in case. Any help would be greatly appreciated!

Thanks!

Firefly code:

int value;
int pwmPin  = 3;                    // light connected to digital pin 11-- I just chose an initial value
//int ledpin2 = 9;
long time=0;
int period = 500;
int x = 0;
long blink_delay = 300;    // these must be declared as long due to the random() operation
long blink = 3;
long random_led = 55;
const byte pwmPins [] = {2, 3, 5, 6, 9, 10, 11};

void setup()
{           //nothing to setup
}

void loop()
{
  choose_firefly();
  fade();

  blink_delay = random(50, 500);
  delay(blink_delay);  
  blink = random(2, 12);  //Orginal number wasd (2, 5)
                          //Using 10 instead of 5 gives longer
                          //possible blink times
  
}
void fade()
{
  for(x=0; x<255;)
  {
    time = x;
    value = abs(-127+127*cos(4*PI/period*x));   //the -127 value shifts the cosine curve negative with a zero initial value; abs shifts everything positive
    analogWrite(pwmPin, value);           // sets the value (range from 0 to 255)
    delay(blink);
    x++;
  }
}

void choose_firefly()
  {
    pwmPin = pwmPins [random (0, 7)];
    
  }

NeoPixel/ LED strip code working with a sound sensor:

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 11

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
int threshold = 500
; //Change This
int volume;

void setup() {

Serial.begin(9600);

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}




void loop() {
  // Some example procedures showing how to display to the pixels:

 volume = analogRead(A0);
 
  if(volume>=threshold){
 
  
  colorWipe(strip.Color(90, 0, 140),  2); // Neuron Transmitter
  colorWipe(strip.Color(0, 0, 0), .5);
  }
}
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

Put together:

int value;
int pwmPin  = 3;                    // light connected to digital pin 11-- I just chose an initial value
//int ledpin2 = 9;
long time=0;
int period = 500;
int x = 0;
long blink_delay = 300;    // these must be declared as long due to the random() operation
long blink = 3;
long random_led = 55;
const byte pwmPins [] = {2, 3, 5, 6, 9, 10, 11};

#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 11

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);
int threshold = 500
; //Change This
int volume;

void setup() {

Serial.begin(9600);

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}




void loop() {

  choose_firefly();
  fade();

  blink_delay = random(50, 500);
  delay(blink_delay);  
  blink = random(2, 12);  //Orginal number wasd (2, 5)
                          //Using 10 instead of 5 gives longer
                          //possible blink times
  
}
void fade()
{
  for(x=0; x<255;)
  {
    time = x;
    value = abs(-127+127*cos(4*PI/period*x));   //the -127 value shifts the cosine curve negative with a zero initial value; abs shifts everything positive
    analogWrite(pwmPin, value);           // sets the value (range from 0 to 255)
    delay(blink);
    x++;
  }
}

void choose_firefly()
  {
    pwmPin = pwmPins [random (0, 7)];
    
  
  // Some example procedures showing how to display to the pixels:

 volume = analogRead(A0);
 
  if(volume>=threshold){
 
  
  colorWipe(strip.Color(90, 0, 140),  2); // Neuron Transmitter
  colorWipe(strip.Color(0, 0, 0), .5);
  }
}
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

So which do you want pin 11 to be? The pin for the neopixel strip?

#define PIN 11

Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

Or one of the random fade pins?

const byte pwmPins [] = {2, 3, 5, 6, 9, 10, 11};

You should also reconsider the use of the delay function. No other code is running during the delay. So the delays in one part will also delay the other part. Perhaps the "Blink Without Delay" style would be more appropriate for the timing.

Hi!

Thanks for replying. I want the NeoPixel at Pin 11. Your suggestion with the delay is good, I had no idea it works for both, but if I delete the delay, will I not have the random leds blink one after the other? The idea was to make them look like a starry night that fades gently in and out...

What do you suggest I do or how to rewrite the code? I am quite desperate to get this done as my deadline is coming soon...

Look at the Blink Without Delay example.

Hey, thanks for the help - it put me on the right track. Got the right code and it all works :)