Code works fine with individual component, but won't work when integrated

So right now I’m trying to create a device that’ll use a Arduino Mega, servo, LEDs, Bluetooth, and Audio. I got each component to work by itself but now I’m facing some issues on integrating everything together, specifically from the LEDs.

For the LEDs, I plan to have like 6 LEDs stationary (not doing anything) and have 3 LEDs do a new pattern every 10 seconds (blinking, and pulsing). Their respective functions are at the very bottom of the code.

When I run this code by itself with just the Arduino and LEDs, the 3 LEDs work perfectly, but when I put everything together, the 3 LEDs dont blink or pulse the way they’re supposed to (It blinks but not like it was originally, it has weak brightness so you can barely tell it blinks and doesn’t even pulse, since the brightness is constant once it reaches the pulse function).

I’m using a 2A 5V wall wart, and I know power isn’t of concern since I disconnected all of the components from the breadboard and the LEDs still wont work.

and yes I am aware the code is a bit messy, I plan to refine this a bit after I get everything working

Code that doesn’t work when integrated with other components

//Servos
#include <Servo.h>
Servo myservo;  // create servo object to control a servo
//Servos

//Bluetooth Audio Setup
#include <DFRobotDFPlayerMini.h>//Audio player library
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX on arduino
DFRobotDFPlayerMini myMP3;//Sets up audio
int dataIn;
//Bluetooth Audio Setup

//LED Setup
#include "FastLED.h"
#define DATA_PIN    7
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    19
CRGB leds[NUM_LEDS];
int fadeAmount = 10;  // Set the amount to fade for Pattern2
int brightness = 0; //Set brightness variable for Pattern2
//LED Setup

#define BRIGHTNESS          10
#define FRAMES_PER_SECOND  120

void setup() { //Setup LED, audio, and bluetooth

  myservo.attach(8,600,2300);  // ServoPinSetup:(pin, min, max) Red goes to live, brown goes to ground, yellow goes to PWM Pin 8

  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);

  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);

  Serial.begin(9600); //Bluetooth connected via hardware serial
  mySerial.begin(9600); //Music module

  myMP3.begin(mySerial); //Automatically plays audio when initiated
  myMP3.play(2);
  myMP3.volume(15);
      
}

// List of patterns to cycle through.  Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { blinking, pulsing};
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current

  
void loop()  //Loops LED Patterns, checks bluetooth input for audio, and changes servos direction
{
  // Call the current pattern function once, updating the 'leds' array
  gPatterns[gCurrentPatternNumber]();

  // send the 'leds' array out to the actual LED strip
  FastLED.show();  
  // insert a delay to keep the framerate modest
  FastLED.delay(1000/FRAMES_PER_SECOND);  

  // do some periodic updates
  EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  
  //Once connected to Bluetooth, change song if directed so
  if(Serial.available()){
    dataIn=Serial.read();
      Serial.println("data read");

  
  if (dataIn== 1) {
  delay(1000);
  Serial.println("Wait 1 sec");
  myMP3.playMp3Folder(1);
  delay(50);
  Serial.println("Play 1");
  delay(50);
    myMP3.volume(15); 
    delay(50);
  Serial.println("Volume Up");

  }

}

myservo.write(0);  // tell servo to go to a particular angle
  delay(1000);
  
  myservo.write(90);              
  delay(3000); 
  
  myservo.write(135);              
  delay(5000);
  
  myservo.write(180);              
  delay(4000);    
}

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))

void nextPattern()
{
  // add one to the current pattern number, and wrap around at the end
  gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}

void blinking() //LED Pattern1
{
for (int i=0; i<=2; i++)
   {
      leds[i] = CRGB(100, 0, 0);
      FastLED.show();
      leds[i].fadeLightBy( 100 ); 
      delay(100);
   }
}


void pulsing() //LED Pattern2
{
   for(int j = 0; j <=2; j++ )
   {
   leds[j].setRGB(100,0,0);  
                             
   leds[j].fadeLightBy(brightness);
  }
  FastLED.show();
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if(brightness == 50 || brightness == 200)
  {
    fadeAmount = -fadeAmount ;
  }  
  delay(30);  // This delay sets speed of the fade. 
 
}

Code that works but only with the LED.

#include "FastLED.h"
 
#define DATA_PIN    7
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    19
CRGB leds[NUM_LEDS];
int fadeAmount = 26;  // Set the amount to fade I usually do 5, 10, 15, 20, 25 etc even up to 255.
int brightness = 0;
 
 
#define BRIGHTNESS          10
#define FRAMES_PER_SECOND  120
 
void setup() {
  delay(3000); // 3 second delay for recovery
 
  // tell FastLED about the LED strip configuration
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  //FastLED.addLeds<LED_TYPE,DATA_PIN,CLK_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
 
  // set master brightness control
  FastLED.setBrightness(BRIGHTNESS);
}
 
 
// List of patterns to cycle through.  Each is defined as a separate function below.
typedef void (*SimplePatternList[])();
SimplePatternList gPatterns = { blinking, pulsing};
 
uint8_t gCurrentPatternNumber = 0; // Index number of which pattern is current
 
void loop()
{
 
  // Call the current pattern function once, updating the 'leds' array
  gPatterns[gCurrentPatternNumber]();
 
  // send the 'leds' array out to the actual LED strip
  FastLED.show();  
  // insert a delay to keep the framerate modest
  FastLED.delay(1000/FRAMES_PER_SECOND);
 
  // do some periodic updates
  EVERY_N_SECONDS( 10 ) { nextPattern(); } // change patterns periodically
  for (int i=3; i<=4; i++){ //2 Back Blue Lights
      leds[i] = CRGB(0, 0, 255);
      FastLED.setBrightness(5);
      FastLED.show();
      }
      for (int i=5; i<=7; i++){ //3 front lights
      leds[i] = CRGB(100, 100, 100);
      FastLED.setBrightness(5);
      FastLED.show();
      }
      for (int i=8; i<=10; i++){ //Front red light
      leds[i] = CRGB(100, 0, 0);
      FastLED.setBrightness(5);
      FastLED.show();
      }
}
 
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
 
void nextPattern()
{
  // add one to the current pattern number, and wrap around at the end
  gCurrentPatternNumber = (gCurrentPatternNumber + 1) % ARRAY_SIZE( gPatterns);
}
 
void blinking()
{
for (int i=0; i<=2; i++)
   {
      leds[i] = CRGB(100, 0, 0);
      FastLED.show();
      leds[i].fadeLightBy( 100 );
      delay(100);
 
   }
}
 
void pulsing()
{
   for(int i = 0; i <=2; i++ )
   {
   leds[i].setRGB(100,0,0);  
                             
   leds[i].fadeLightBy(brightness);
  }
  FastLED.show();
  brightness = brightness + fadeAmount;
  // reverse the direction of the fading at the ends of the fade:
  if(brightness == 50 || brightness == 200)
  {
    fadeAmount = -fadeAmount ;
  }  
  delay(30);  // This delay sets speed of the fade. I usually do from 5-75 but you can always go higher.
 
}

I’m just looking for a way to debug this on my own since I’m not sure where to start.

Your loop() is this:

void loop()
{
  ...
  FastLED.delay(8);
  ...
  delay(1000);
  ...
  delay(3000);
  ...
  delay(5000);
  ...
  delay(4000);

  // Are the delay() in blinking and pulsing also executed every loop() ?
}

For the pattern, the FastLED wants to delay for 8 milliseconds. However, the loop() waits 13008 milliseconds. That is 1626 times more :wink:

To combine multiple tasks, millis() is required instead of delay().
To use millis(), you have to rewrite the sketch.

The Arduino Mega 2560 has 3 spare serial ports. Use those instead of SoftwareSerial. The SoftwareSerial influences everything else (in a bad way).

A small servo motor might require 0.5A peak current when it starts to move. The 5V pin of the Arduino may not be used for that. Can you draw a picture of how everything is connected ?

If you have the 5V wall wart plugged into the Mega's barrel jack, it won't work, that needs at least 7 - 12V.

Koepel:
Your loop() is this:

void loop()

{
  …
  FastLED.delay(8);
  …
  delay(1000);
  …
  delay(3000);
  …
  delay(5000);
  …
  delay(4000);

// Are the delay() in blinking and pulsing also executed every loop() ?
}




For the pattern, the FastLED wants to delay for 8 milliseconds. However, the loop() waits 13008 milliseconds. That is 1626 times more ;)

To combine multiple tasks, millis() is required instead of delay().
To use millis(), you have to rewrite the sketch.

The Arduino Mega 2560 has 3 spare serial ports. Use those instead of SoftwareSerial. The SoftwareSerial influences everything else (in a bad way).

A small servo motor might require 0.5A peak current when it starts to move. The 5V pin of the Arduino may not be used for that. Can you draw a picture of how everything is connected ?

Hmm where did you find FastLED.delay(8) ? I think you were looking at the servo code with the delay() but I’m not sure where you got the 8 millisecond delay. You’re suggesting to rewrite the entire sketch using millis() rather delay() ? I’ll try that.

Looking around it seemed to use the other serial ports I’ll need a RS485 or something similar, so I’ll plan to use that later on.

Oh I’m not connecting it to the 5V pin on the Arduino. I’m using a 5V 2A wall wart and connected it to the breadboard power buses (with a 1000 micro farad capacitor too). I can still give you the sketch of the drawings yet, I haven’t finished that part yet.

This is in the loop():

// insert a delay to keep the framerate modest
FastLED.delay(1000/FRAMES_PER_SECOND);