Led Fade and Neopixel Remaining on After Sketch Question

I pretty new to programing the Arduino so sorry if these are super basic questions.

I am trying to make my LED's fade when I push a button at the beginning of my sketch. So I test the code separately and it works fine. When I insert it into my sketch though the lights just turn off when I press the button rather than fade. I'm sure I am doing something simple wrong but I'm not sure what. Any ideas?

Also I have 36 Neopixels hooked up to my sketch as well. These I want to fade at the end but I'm not sure how and when the sketch ends the neopixels stay on. Even if I upload a brand new sketch. Any idea why?

Thanks!

//MUSIC--------------------------------------------
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <Adafruit_NeoPixel.h>
// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

    Adafruit_VS1053_FilePlayer musicPlayer =
    // create breakout-example object!
    //Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
    // create shield-example object!
    Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);


//Start Button-----------------------------------------
const int buttonPin = 41;   // choose the input pin (for a pushbutton)
int buttonState = 0;

//Interior Light----------------------------------------
int led = 44;           // the pin that the Interior LED is attached to
int brightness = 0;
int fadeAmount = 5;
//Sillohette Lights:-----------------------------------

#define PIN 53
Adafruit_NeoPixel strip = Adafruit_NeoPixel(36, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
 
 //Sillohette Lights:-----------------------------------
 
 strip.begin();
  strip.show(); // Initialize all pixels to 'off'  
  
//Start Button Input :----------------------------------
 pinMode(buttonPin, INPUT_PULLUP);    // declare pushbutton as input

//Interior LED Setup:-----------------------------------
  pinMode(led, OUTPUT);
 
 


 //MUSIC----------------------------------------------
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Library Test");

  // initialise the music player
  if (! musicPlayer.begin()) { // initialise the music player
     Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
     while (1);
  }
  Serial.println(F("VS1053 found"));

 
  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");
  
  // list files
  printDirectory(SD.open("/"), 0);
  
  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(1,1);

  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));

}

void loop() {  
   
  //Interior LED
digitalWrite(led, 200);    
  Buttonloopcode();
  Musicloopcode();


} 
                    
 
 void Buttonloopcode() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    return;
  }
  else {
  
    }

  }


   
void Musicloopcode() {
  if (buttonState == LOW) {
      return;
  }
  else { 
    // set the brightness of pin 9:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);      
  }
   // Alternately, we can just play an entire file at once
  // This doesn't happen in the background, instead, the entire
  // file is played and the program will continue when it's done!
  musicPlayer.playFullFile("WedTop1.ogg");

  // Start playing a file, then we can do stuff while waiting for it to finish
  if (! musicPlayer.startPlayingFile("WedTop1.mp3")) {
    Serial.println("Could not open file WedTop1.mp3");
    while (1);
  }
  Serial.println(F("Started playing"));

  while (musicPlayer.playingMusic) {
  
 

    delay (43100);
    rainbow(200); 

  
  
  
      // file is now playing in the 'background' so now's a good time
    // to do something else like handling LEDs or buttons :)
    Serial.print(".");
  
return;
  }
}  
 

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<350; j++) {
  for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 250));
    }
    strip.show();
    delay(wait);
    
  
}
  }
 

  // change the brightness for next time through the loop:
//  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  //if (brightness == 0 || brightness == 255) {
 //   fadeAmount = -fadeAmount ; 
 // }     
  // wait for 30 milliseconds to see the dimming effect    
//  delay(30);                            
//}



//Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
 if(WheelPos < 255) {
  return strip.Color(WheelPos * 1, 100 - WheelPos * 1, 100);
  } else if(WheelPos < 80) {
   WheelPos -= 255;
   return strip.Color(255 - WheelPos * 1, 255, WheelPos * 1);
  } else {
   WheelPos -= 255;
   return strip.Color(255, WheelPos * 1, 255 - WheelPos * 1);
  }
}




/// File listing helper
void printDirectory(File dir, int numTabs) {
   while(true) {
     
     File entry =  dir.openNextFile();
     if (! entry) {
       // no more files
       //Serial.println("**nomorefiles**");
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
       // files have sizes, directories do not
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }


}
 void Buttonloopcode() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    return;
  }
  else {
  
    }

  }

If the switch state is LOW, return. Otherwise, return. Hardly seems useful to have even looked.

You have far too many dependencies on global variables, and far too much useless code. Reading the switch state in one function, and using the state in another function is unnecessarily complex.

Your piss-poor indenting style makes what you have nearly impossible to follow. Use Tools + Auto Format to fix the indenting. Delete the useless functions/calls. Then, post the code again.

So here is the auto-formatted code. But for the most part I left in all the code that I had before.

As I said before I am new to this. I don't know what is extraneous code or not I don't know what are the proper ways of formatting said code is. All I know are the problems that are coming up, and a rough idea of what may, or may not, be causing them. That is why I am posting here. To find out what is going wrong and what can be done to fix it.

If my lack of programing knowledge angers or annoys you, then by all means move on and don't help me. But I won't be belittled each time I ask a question in a place who's sole purpose is to ask these very questions. So help me or not. That's fine either way.

//MUSIC--------------------------------------------
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <Adafruit_NeoPixel.h>
// These are the pins used for the breakout example
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
// These are the pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// These are common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
// create breakout-example object!
//Adafruit_VS1053_FilePlayer(BREAKOUT_RESET, BREAKOUT_CS, BREAKOUT_DCS, DREQ, CARDCS);
// create shield-example object!
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);


//Start Button-----------------------------------------
const int buttonPin = 41;   // choose the input pin (for a pushbutton)
int buttonState = 0;

//Interior Light----------------------------------------
int led = 44;           // the pin that the Interior LED is attached to
int brightness = 0;
int fadeAmount = 5;
//Sillohette Lights:-----------------------------------

#define PIN 53
Adafruit_NeoPixel strip = Adafruit_NeoPixel(36, PIN, NEO_GRB + NEO_KHZ800);

void setup() {

  //Sillohette Lights:-----------------------------------

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

  //Start Button Input :----------------------------------
  pinMode(buttonPin, INPUT_PULLUP);    // declare pushbutton as input

  //Interior LED Setup:-----------------------------------
  pinMode(led, OUTPUT);




  //MUSIC----------------------------------------------
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Library Test");

  // initialise the music player
  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
  Serial.println(F("VS1053 found"));


  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");

  // list files
  printDirectory(SD.open("/"), 0);

  // Set volume for left, right channels. lower numbers == louder volume!
  musicPlayer.setVolume(1,1);

  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));

}

void loop() {  

  //Interior LED
  digitalWrite(led, 200);    
  Buttonloopcode();
  Musicloopcode();


} 


void Buttonloopcode() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    return;
  }
  else {

  }

}



void Musicloopcode() {
  if (buttonState == LOW) {
    return;
  }
  else { 
    // set the brightness of pin 9:
    analogWrite(led, brightness);    

    // change the brightness for next time through the loop:
    brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade: 
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ; 
    }     
    // wait for 30 milliseconds to see the dimming effect    
    delay(30);      
  }
  // Alternately, we can just play an entire file at once
  // This doesn't happen in the background, instead, the entire
  // file is played and the program will continue when it's done!
  musicPlayer.playFullFile("WedTop1.ogg");

  // Start playing a file, then we can do stuff while waiting for it to finish
  if (! musicPlayer.startPlayingFile("WedTop1.mp3")) {
    Serial.println("Could not open file WedTop1.mp3");
    while (1);
  }
  Serial.println(F("Started playing"));

  while (musicPlayer.playingMusic) {



    delay (43100);
    rainbow(200); 




    // file is now playing in the 'background' so now's a good time
    // to do something else like handling LEDs or buttons :)
    Serial.print(".");

    return;
  }
}  


void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<350; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 250));
    }
    strip.show();
    delay(wait);


  }
}



//Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 255) {
    return strip.Color(WheelPos * 1, 100 - WheelPos * 1, 100);
  } 
  else if(WheelPos < 80) {
    WheelPos -= 255;
    return strip.Color(255 - WheelPos * 1, 255, WheelPos * 1);
  } 
  else {
    WheelPos -= 255;
    return strip.Color(255, WheelPos * 1, 255 - WheelPos * 1);
  }
}




/// File listing helper
void printDirectory(File dir, int numTabs) {
  while(true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      // no more files
      //Serial.println("**nomorefiles**");
      break;
    }
    for (uint8_t i=0; i<numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs+1);
    } 
    else {
      // files have sizes, directories do not
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }


}

MasonLev:
So here is the auto-formatted code. But for the most part I left in all the code that I had before.

As I said before I am new to this. I don't know what is extraneous code or not I don't know what are the proper ways of formatting said code is. All I know are the problems that are coming up, and a rough idea of what may, or may not, be causing them. That is why I am posting here. To find out what is going wrong and what can be done to fix it.

If my lack of programing knowledge angers or annoys you, then by all means move on and don't help me. But I won't be belittled each time I ask a question in a place who's sole purpose is to ask these very questions. So help me or not. That's fine either way.

they call it a programing language.

over-commenting sur-commentant is great for beginners est idéal pour les débutants, but painful to get through mais pénible à passer.. Imagine you are fluent in english Imaginez que vous êtes à l'aise en anglais and someone puts french translations et quelqu'un met traditions françaises between every sentence... entre chaque phrase ... très difficile à lire very hard to read.

taking a few moments to clean all that up, removing the comments and the empty lines really helps. SVP :blush:

BulldogLowell:

MasonLev:
So here is the auto-formatted code. But for the most part I left in all the code that I had before.

As I said before I am new to this. I don't know what is extraneous code or not I don't know what are the proper ways of formatting said code is. All I know are the problems that are coming up, and a rough idea of what may, or may not, be causing them. That is why I am posting here. To find out what is going wrong and what can be done to fix it.

If my lack of programing knowledge angers or annoys you, then by all means move on and don't help me. But I won't be belittled each time I ask a question in a place who's sole purpose is to ask these very questions. So help me or not. That's fine either way.

they call it a programing language.

over-commenting sur-commentant is great for beginners est idéal pour les débutants, but painful to get through mais pénible à passer.. Imagine you are fluent in english Imaginez que vous êtes à l'aise en anglais and someone puts french translations et quelqu'un met traditions françaises between every sentence... entre chaque phrase ... très difficile à lire very hard to read.

taking a few moments to clean all that up, removing the comments and the empty lines really helps. SVP :blush:

That is fair to say, but I still wouldn't belittle a class of people trying to learn English if they didn't get the concept of irony their first day.

so lets tidy that code up and get too it, mate!

Thanks Bulldog I really appreciate it. Here's the cleaned up code. I took out all the notes and all the code I know I'm not using.

//MUSIC--------------------------------------------
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>
#include <Adafruit_NeoPixel.h>
#define BREAKOUT_RESET  9      // VS1053 reset pin (output)
#define BREAKOUT_CS     10     // VS1053 chip select pin (output)
#define BREAKOUT_DCS    8      // VS1053 Data/command select pin (output)
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)
#define CARDCS 4     // Card chip select pin
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
//Sillohette Lights:-----------------------------------
#define PIN 53
Adafruit_NeoPixel strip = Adafruit_NeoPixel(36, PIN, NEO_GRB + NEO_KHZ800);

//Start Button-----------------------------------------
const int buttonPin = 41;   // choose the input pin (for a pushbutton)
int buttonState = 0;

//Interior Light----------------------------------------
int led = 44;           // the pin that the Interior LED is attached to
int brightness = 0;
int fadeAmount = 5;





void setup() {  //===================================================================

  //Sillohette Lights:-----------------------------------

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

  //Start Button Input :----------------------------------
  pinMode(buttonPin, INPUT_PULLUP);    // declare pushbutton as input

  //Interior LED Setup:-----------------------------------
  pinMode(led, OUTPUT);




  //MUSIC----------------------------------------------
  Serial.begin(9600);
  Serial.println("Adafruit VS1053 Library Test");

  if (! musicPlayer.begin()) { // initialise the music player
    Serial.println(F("Couldn't find VS1053, do you have the right pins defined?"));
    while (1);
  }
  Serial.println(F("VS1053 found"));


  if (!SD.begin(CARDCS)) {
    Serial.println(F("SD failed, or not present"));
    while (1);  // don't do anything more
  }
  Serial.println("SD OK!");

  printDirectory(SD.open("/"), 0);

  musicPlayer.setVolume(1,1);

  if (! musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT))
    Serial.println(F("DREQ pin is not an interrupt pin"));

}

void loop() {  //===================================================================

  digitalWrite(led, 200);    

  Buttonloopcode();
  Musicloopcode();
} 

void Buttonloopcode() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == LOW) {
    return;
  }
  else {
    analogWrite(led, brightness);    
    brightness = brightness + fadeAmount;

    delay(4000);
    if (brightness == 0 || brightness == 255) {
      fadeAmount = -fadeAmount ; 
    }     
    delay(3000);      
  }

}

void Musicloopcode() {
  if (buttonState == LOW) {
    return;
  }
  else { 

    musicPlayer.playFullFile("WedTop1.ogg");

    if (! musicPlayer.startPlayingFile("WedTop1.mp3")) {
      Serial.println("Could not open file WedTop1.mp3");
      while (1);
    }
    Serial.println(F("Started playing"));

    while (musicPlayer.playingMusic) {
      delay (43100);
      rainbow(200); 
      Serial.print(".");

      return;
    }
  }  
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<350; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 250));
    }
    strip.show();
    delay(wait);


  }
}


uint32_t Wheel(byte WheelPos) {
  if(WheelPos < 255) {
    return strip.Color(WheelPos * 1, 100 - WheelPos * 1, 100);
  } 
  else if(WheelPos < 80) {
    WheelPos -= 255;
    return strip.Color(255 - WheelPos * 1, 255, WheelPos * 1);
  } 
  else {
    WheelPos -= 255;
    return strip.Color(255, WheelPos * 1, 255 - WheelPos * 1);
  }
}




void printDirectory(File dir, int numTabs) {
  while(true) {

    File entry =  dir.openNextFile();
    if (! entry) {
      break;
    }
    for (uint8_t i=0; i<numTabs; i++) {
      Serial.print('\t');
    }
    Serial.print(entry.name());
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs+1);
    } 
    else {
      Serial.print("\t\t");
      Serial.println(entry.size(), DEC);
    }
    entry.close();
  }


}

think about calling a function with a boolean toggle that is switched with the pushbutton:

like this, untested...

boolean fade = false;
int lastButtonState;
int buttonPin = 3;
int led = 13;

void setup()
{
  Serial.begin(115200);
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(led, OUTPUT);
}


void loop()
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH)
  {
    fade = !fade;
  }
  lastButtonState = buttonState;
  //
  fadeLed(fade);
}


void fadeLed(boolean fadeOn) 
{
  static unsigned long lastTime = millis();
  static int brightness = 0;
  static int fadeAmount = 5;
  if (fadeOn)
  {
    if (millis() - lastTime >= 25UL)
    {
      analogWrite(led, brightness); 
      //Serial.println(brightness);   
      brightness += fadeAmount;
      if (brightness == 0 || brightness == 255) fadeAmount = -fadeAmount;
      lastTime += 25UL;
    }         
  }
  else 
  {
    analogWrite(led, 0);
    //Serial.println("OFF");
  }
}

Got it. If I wanted to make it so the lights start on and then fade off and stay off for (x) seconds then fade back in would I:

-Change:

void loop()
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState == LOW && lastButtonState == HIGH)
  {
    fade = !fade;
}
  lastButtonState = buttonState;
  //
  fadeLed(fade);
}
  }

to

void loop()
{
  int buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH && lastButtonState == LOW)
  {
    fade = !fade;
}
delay(x);
  lastButtonState = buttonState;
  //
  fadeLed(fade);
}

we no-likey delay()

one time or over and over :

a) lower - wait - rise

b) lower wait rise wait lower wait rise wait......

Ah got it.

a) Lower-wait-rise.

That way when the sketch restarts it will look as if nothing had happened.

MasonLev:
Ah got it.

a) Lower-wait-rise.

That way when the sketch restarts it will look as if nothing had happened.

restarts?

What I mean is when it loops back to the beginning of the sketch.