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();
}
}