Code stop working after adding libraries

Hi guys here is a brief description of my project I am working on before I get into the problem.

A Love-O-Meter project (Not similar to the arduino example), using 2 pulse sensors to measure the heartbeat, 2 foils wrapped with conductive tape, an LED strip (for aesthetic purposes when the Love-O-Meter is not in use, cylon effect when the pulse sensor is measuring the heartbeat and to determine the score with the number of LEDs lighting up), the Bare Conductive TouchBoard with a speaker (For playing a certain song depending on the score).

The project works like this: 2 people will each place 1 of their hands on the foil and with their other free hand holding each other (forming a close circuit). 1 of their fingers will also have to be on the pulse sensor. The pulse sensor will then measure the heartbeat for a duration of 10s and the number of LEDs lighting up on the strip will depend on their average BPM.

However, everything is working except getting the song to be played. After including the MP3 and SD includes, the program storage space increased from ~50% to about ~90%. I have tried changing my datatypes, but it still doesnt work. Anyone has suggestions as to how I can free up more space?
EDIT: I have attached the code and also x-post from: http://forum.arduino.cc/index.php?topic=542386

LED_Functions.ino (1.79 KB)

Main_Code.ino (10.9 KB)

Post code here using code tags
What memory size are you taking about ? (SRAM or Flash)

J-M-L:
Post code here using code tags
What memory size are you taking about ? (SRAM or Flash)

I am unable to post the code here since its too big. I am not sure what you mean by memory size

You can attach files if they are more than 9000 characters

Can you post in full the message you get at the end of the compilation (when you say “the program storage space increased from ~50% to about ~90%.” Do you really mean flash memory? using 90% of it is not an issue. If this is SRAM then that leaves you with not a lot for dynamic memory allocation at run time and might be indeed an issue)

Also posted at:
https://arduino.stackexchange.com/q/51970
If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

J-M-L:
You can attach files if they are more than 9000 characters

Can you post in full the message you get at the end of the compilation (when you say “the program storage space increased from ~50% to about ~90%.” Do you really mean flash memory? using 90% of it is not an issue. If this is SRAM then that leaves you with not a lot for dynamic memory allocation at run time and might be indeed an issue)

I'm not sure, I was just copying what the compiler said. Sketch uses 26340 bytes (91%) of program storage space. Maximum is 28672 bytes.
Global variables use 1095 bytes (42%) of dynamic memory, leaving 1465 bytes for local variables. Maximum is 2560 bytes. And I have attached the 2 files above

It seems like this is an X-Y problem. The reported issue is that the Arduino IDE is saying 91% of program storage space is used. The real issue is that the program isn't working as expected, which has absolutely nothing to do with the amount of program storage space. So why don't you provide details about what the real problem is?

Camerutttt:
I'm not sure, I was just copying what the compiler said.

Sketch uses 26340 bytes (91%) of program storage space. Maximum is 28672 bytes.
Global variables use 1095 bytes (42%) of dynamic memory, leaving 1465 bytes for local variables. Maximum is 2560 bytes.

And I have attached the 2 files above

So you most likely don’t have a memory problem

pert:
It seems like this is an X-Y problem. The reported issue is that the Arduino IDE is saying 91% of program storage space is used. The real issue is that the program isn't working as expected, which has absolutely nothing to do with the amount of program storage space. So why don't you provide details about what the real problem is?

Firstly, the LED strip does not display the rainbow cycle. Instead the first LED just shows white, with the others turned off.
When I place both of my hands on the foil (forming a close circuit), the BPM will not be printed out as well, even though I waited for 20s.

What type of led strip? Are they putting crazy pressure on the cpu with interrupts? That would conflict easily with SPI or other interrupt based services you need

J-M-L:
What type of led strip? Are they putting crazy pressure on the cpu with interrupts? That would conflict easily with SPI or other interrupt based services you need

It is the WS2812B. How can I tell if they are putting crazy pressure on the cpu with interrupts?

They are :(. (you could read this)

APA102 would not

J-M-L:
They are :(. (you could read this)

APA102 would not

So you are saying that the #include SPI.h and the WS2812B is conflicting and therefore it causes my entire code to not run as expected?

No I’m saying that when you have code depending on proper timing and interrupts but have too many interrupts going on (interrupts are not interrupted) then you can get in trouble

Is the code working if you don’t play with the leds or pretend with the fastLED library (which I assume you use) that you have APA102 connected instead of your WS2812B?

J-M-L:
No I’m saying that when you have code depending on proper timing and interrupts but have too many interrupts going on (interrupts are not interrupted) then you can get in trouble

Is the code working if you don’t play with the leds or pretend with the fastLED library (which I assume you use) that you have APA102 connected instead of your WS2812B?

The code is working if either I exclude the MP3 or the LED. Unfortunately, I do not have the APA102. I only have the WS2812B. The only interrupts I may be using is the EVERY_N_MILLISECONDS function. The rest are through using if/else statements.

The WS2812B use interrupts heavily but you don’t see it
Same goes for the SD

I was suggesting pretending you have APA102 connected - sure the LED would not do anything (disconnect them) but the code would handle them. If that works that would be an indication for the challenge you have

J-M-L:
The WS2812B use interrupts heavily but you don’t see it
Same goes for the SD

I was suggesting pretending you have APA102 connected - sure the LED would not do anything (disconnect them) but the code would handle them. If that works that would be an indication for the challenge you have

I dont know if this is helpful, but I just found out that just by including the MP3 and SD libraries and variables along with the fastLED libraries and functions, everything works ok. All the effects work and the BPM measurement is working as well. It is when I add these 5 lines in the setup(), then the LED's effect will stop working:

if (!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
MP3player.begin();
MP3player.setBitRate(192);
MP3player.setVolume(10, 10);

J-M-L:
The WS2812B use interrupts heavily but you don’t see it
Same goes for the SD

I was suggesting pretending you have APA102 connected - sure the LED would not do anything (disconnect them) but the code would handle them. If that works that would be an indication for the challenge you have

Well from what you said, I commented out this line:

FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NO_OF_LEDS).setCorrection( TypicalLEDStrip );

And replaced it with this instead:

FastLED.addLeds<APA102,11,13,GRB>(leds, NO_OF_LEDS).setCorrection(TypicalLEDStrip);

But the same thing happens again. The LED strip effect doesnt work.

I pulled up my older version of the Love-O-Meter which doesnt use any sensors, but instead just outputs a random number as seen here:

//Random
#include "TrueRandom.h"
#include <TrueRandom.h>

//LED Strip
#include <Adafruit_NeoPixel.h>
#include "WS2812_Definitions.h"
#define LED 11
#define LED_COUNT 14

Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, LED, NEO_GRB + NEO_KHZ800);

// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4

// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;

// sd card instantiation
SdFat sd;

long randNumber;
long ledCount;

void setup() {
 Serial.begin(57600);
 //LEDs
 leds.begin();
 clearLEDs();
 leds.show();

 //Pins
 pinMode(A5, INPUT);
 //digitalWrite(A5,);
 pinMode(11, OUTPUT);

 //Setup MP3 shield
 if (!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
 if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
 MP3player.begin();
 MP3player.setBitRate(192);
 MP3player.setVolume(10, 10);
}

void loop() {
 //LEDs will ride a rainbow slide to show that the Like-O-Meter is currently idling
 for (int i = 0; i < LED_COUNT * 10; i++)
 {
   rainbow(i);
   delay(100);
   Serial.println(analogRead(A5));

   //If both copper boards are touched and the circuit is complete
   if (analogRead(A5) == 0)
     beginCalculation();
 }
}

//Function to print the compatibility and play certain music depending on the result
void beginCalculation()
{
 //Play calculation music and cylon the LED strip to show that it is calculating the compatibility
 MP3player.setVolume(40, 40);
 MP3player.playTrack(0);
 for (int i = 0; i < 2; i++)
 {
   cylon(INDIGO, 43);
 }

 //Print a random compatibility result
 randNumber = TrueRandom.random(1, 15); // print a random number from 1 to 15
 Serial.print("Random number is: ");
 Serial.println(randNumber);
 clearLEDs();
 int rainbowScale = 192 / LED_COUNT;

 for (int i = 0; i < randNumber; i++)
 {
   leds.setPixelColor(i, rainbowOrder((rainbowScale * i) % 192));
 }

 //Low Score
 if (randNumber <= 5)
 {
   MP3player.setVolume(30, 30);
   MP3player.playTrack(2);
 }
 //High Score
 else if (randNumber >= 10)
 {
   MP3player.setVolume(80, 80);
   MP3player.playTrack(1);
 }
 //Average Score
 else
 {
   MP3player.setVolume(50, 50);
   MP3player.playTrack(4);
 }
 leds.show();
 delay(8500);
}


void clearLEDs()
{
 for (int i = 0; i < LED_COUNT; i++)
 {
   leds.setPixelColor(i, 0);
 }
}

void cylon(unsigned long color, byte wait)
{
 // weight determines how much lighter the outer "eye" colors are
 const byte weight = 4;
 // It'll be easier to decrement each of these colors individually
 // so we'll split them out of the 24-bit color value
 byte red = (color & 0xFF0000) >> 16;
 byte green = (color & 0x00FF00) >> 8;
 byte blue = (color & 0x0000FF);

 // Start at closest LED, and move to the outside
 for (int i = 0; i <= LED_COUNT - 1; i++)
 {
   clearLEDs();
   leds.setPixelColor(i, red, green, blue);  // Set the bright middle eye
   // Now set two eyes to each side to get progressively dimmer
   for (int j = 1; j < 3; j++)
   {
     if (i - j >= 0)
       leds.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
     if (i - j <= LED_COUNT)
       leds.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
   }
   leds.show();  // Turn the LEDs on
   delay(wait);  // Delay for visibility
 }

 // Now we go back to where we came. Do the same thing.
 for (int i = LED_COUNT - 2; i >= 1; i--)
 {
   clearLEDs();
   leds.setPixelColor(i, red, green, blue);
   for (int j = 1; j < 3; j++)
   {
     if (i - j >= 0)
       leds.setPixelColor(i - j, red / (weight * j), green / (weight * j), blue / (weight * j));
     if (i - j <= LED_COUNT)
       leds.setPixelColor(i + j, red / (weight * j), green / (weight * j), blue / (weight * j));
   }

   leds.show();
   delay(wait);
 }
}

void rainbow(byte startPosition)
{
 // Need to scale our rainbow. We want a variety of colors, even if there
 // are just 10 or so pixels.
 int rainbowScale = 192 / LED_COUNT;

 // Next we setup each pixel with the right color
 for (int i = 0; i < LED_COUNT; i++)
 {
   // There are 192 total colors we can get out of the rainbowOrder function.
   // It'll return a color between red->orange->green->...->violet for 0-191.
   leds.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
 }
 // Finally, actually turn the LEDs on:
 leds.show();
}

// Input a value 0 to 191 to get a color value.
// The colors are a transition red->yellow->green->aqua->blue->fuchsia->red...
//  Adapted from Wheel function in the Adafruit_NeoPixel library example sketch
uint32_t rainbowOrder(byte position)
{
 // 6 total zones of color change:
 if (position < 31)  // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
 {
   return leds.Color(0xFF, position * 8, 0);
 }
 else if (position < 63)  // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
 {
   position -= 31;
   return leds.Color(0xFF - position * 8, 0xFF, 0);
 }
 else if (position < 95)  // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
 {
   position -= 63;
   return leds.Color(0, 0xFF, position * 8);
 }
 else if (position < 127)  // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
 {
   position -= 95;
   return leds.Color(0, 0xFF - position * 8, 0xFF);
 }
 else if (position < 159)  // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
 {
   position -= 127;
   return leds.Color(position * 8, 0, 0xFF);
 }
 else  //160 <position< 191   Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
 {
   position -= 159;
   return leds.Color(0xFF, 0x00, 0xFF - position * 8);
 }
}

// Cascades a single direction. One time.
void cascade(unsigned long color, byte direction, byte wait)
{
 if (direction == TOP_DOWN)
 {
   for (int i = 0; i < LED_COUNT; i++)
   {
     clearLEDs();  // Turn off all LEDs
     leds.setPixelColor(i, color);  // Set just this one
     leds.show();
     delay(wait);
   }
 }
 else
 {
   for (int i = LED_COUNT - 1; i >= 0; i--)
   {
     clearLEDs();
     leds.setPixelColor(i, color);
     leds.show();
     delay(wait);
   }
 }
}

The lights and music in this code used to work. However, when I ran the code again today, it didnt work anymore. The led strip just stopped functioning.