Code: Select all
// include neopixel featherwing lib
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// include SPI, MP3 and SD libraries
#include <SPI.h>
#include <SD.h>
#include <Adafruit_VS1053.h>
#include <IRrecv.h>
#include <IRutils.h>
int RECV_PIN = 4; //an IR detector/demodulatord is connected to GPIO pin 2
IRrecv irrecv(RECV_PIN);
// These are the pins used
#define VS1053_RESET -1 // VS1053 reset pin (not used!)
#define VS1053_CS 16 // VS1053 chip select pin (output)
#define VS1053_DCS 15 // VS1053 Data/command select pin (output)
#define CARDCS 2 // Card chip select pin
#define VS1053_DREQ 0 // VS1053 Data request, ideally an Interrupt pin
//define neopixel pin, number of leds and brightness
#define NUM_LEDS 32
#define PIN 15
#define BRIGHTNESS 10
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
Adafruit_VS1053_FilePlayer musicPlayer =
Adafruit_VS1053_FilePlayer(VS1053_RESET, VS1053_CS, VS1053_DCS, VS1053_DREQ, CARDCS);
// the name of what we're going to play
char foundname[20];
boolean isPaused = false;
uint8_t volume = 10;
int lastRemoteVal = 0;
byte keyCodeLSB; //Least Significant byte of key code from remote (command)
unsigned long previousMillis = 0; // will store time for last event occurred
long interval = 1000; // interval to use for IR
// comment out when using special effects
//int Count=32;
//boolean OnlyOne;
void setup() {
Serial.begin(115200);
Serial.println("\n\nAdafruit VS1053 Feather 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"));
musicPlayer.sineTest(0x44, 500); // Make a tone to indicate VS1053 is working
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(volume,volume);
// If DREQ is on an interrupt pin we can do background
// audio playing
// musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int
irrecv.enableIRIn(); // Start the receiver
Serial.println("Playing one Track");
// setup for neopixel
strip.setBrightness(BRIGHTNESS);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop() {
//unsigned long currentMillis = millis();
//if(currentMillis - previousMillis > interval)
//{
decode_results results;
if(digitalRead(VS1053_DREQ) && !musicPlayer.stopped() && !isPaused) {
musicPlayer.feedBuffer();
}
// look for a message!
if (irrecv.decode(&results)) {
//Serial.println(results.value, HEX); this gives an error of uint64_t call of overload is ambiguous
// This code is used to understand remote key presses
keyCodeLSB = byte(results.value);
Serial.print("\nThe Key Code LSB is: ");
Serial.println(keyCodeLSB, HEX);
irrecv.resume(); // Receive the next value
// handle repeat codes!
if (results.value == 0xFFFFFFFF) {
// only for vol+ or vol-
if ( (lastRemoteVal == 0xFD40BF) || (lastRemoteVal == 0xFD00FF))
results.value = lastRemoteVal;
} else {
lastRemoteVal = results.value;
}
if (results.value == 0xFD08F7) {
musicPlayer.stopPlaying();
Serial.println("playing track #1");
musicPlayer.startPlayingFile("track001.mp3");
//TwinkleRandom(20, 100, false);
}
if (results.value == 0xFD8877) {
musicPlayer.stopPlaying();
Serial.println("playing track #2");
musicPlayer.startPlayingFile("track002.mp3");
}
if (results.value == 0xFD48B7) {
musicPlayer.stopPlaying();
Serial.println("playing track #3");
musicPlayer.startPlayingFile("track003.mp3");
}
if (results.value == 0xFD28D7) {
musicPlayer.stopPlaying();
Serial.println("playing track #4");
musicPlayer.startPlayingFile("track004.mp3");
}
if (results.value == 0xFDA857) {
musicPlayer.stopPlaying();
Serial.println("playing track #5");
musicPlayer.startPlayingFile("track005.mp3");
}
if (results.value == 0xFD6897) {
musicPlayer.stopPlaying();
Serial.println("playing track #6");
musicPlayer.startPlayingFile("track006.mp3");
}
if (results.value == 0xFD18E7) {
musicPlayer.stopPlaying();
Serial.println("playing track #7");
musicPlayer.startPlayingFile("track007.mp3");
}
if (results.value == 0xFD9867) {
musicPlayer.stopPlaying();
Serial.println("playing track #8");
musicPlayer.startPlayingFile("track008.mp3");
}
if (results.value == 0xFD40BF) { //vol+
Serial.println("Vol+");
if (volume > 0) {
volume--;
musicPlayer.setVolume(volume,volume);
}
}
if (results.value == 0xFD00FF) { //vol-
Serial.println("Vol-");
if (volume < 100) {
volume++;
musicPlayer.setVolume(volume,volume);
}
}
if (results.value == 0xFD807F) { // playpause
Serial.println("Play/Pause");
isPaused = !isPaused; // toggle!
}
if (results.value == 0xFD609F) { // stop play
Serial.println("Stop Play");
musicPlayer.stopPlaying(); // stop music
}
}
delay(1);
//}
// Neopixel special effects selections
//alternateColor(strip.Color(255, 0, 0), strip.Color(0, 255, 0), 100);
//Twinkle(0xff, 0xff, 0xff, 15, 150, false);
//TwinkleRandom(20, 100, false);
//Sparkle(0xff, 0xff, 0xff, 75);
//Sparkle(random(255), random(255), random(255), 75);
//SnowSparkle(0x10, 0x10, 0x10, 20, 200);
//SnowSparkle(0x10, 0x10, 0x10, 20, random(100,1000));
}
boolean findFileStartingWith(char *start) {
File root;
root = SD.open("/");
root.rewindDirectory();
while (true) {
File entry = root.openNextFile();
if (! entry) {
return false;
}
String filename = entry.name();
Serial.print(filename);
if (entry.isDirectory()) {
Serial.println("/");
} else {
Serial.println();
if (filename.startsWith(start)) {
filename.toCharArray(foundname, 20);
entry.close();
root.close();
return true;
}
}
entry.close();
}
}
/// 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();
}
}
// Fill the dots one after the other with a color
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);
}
}
// 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) {
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);
}
// alternate between two colors on even and odd LEDs
void alternateColor(uint32_t c1, uint32_t c2, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
if(i%2 == 0) { // set even LED to color 1
strip.setPixelColor(i, c1);
} else { // set odd LED to color 2
strip.setPixelColor(i, c2);
}
}
strip.show(); // apply the colors
delay(wait);
for(uint16_t i=0; i<strip.numPixels(); i++) {
if(i%2 == 0) { // set even LED to color 2
strip.setPixelColor(i, c2);
} else { // set odd LED to color 1
strip.setPixelColor(i, c1);
}
}
strip.show(); // apply the colors
delay(wait);
}
void clearStrip(){
for(int i=0;i<NUM_LEDS;i++){
strip.setPixelColor(i, strip.Color(0, 0, 0)); //change RGB color value here
}
strip.show();
}
/*Extra stuff*/
void showStrip() {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.show();
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
FastLED.show();
#endif
}
void setPixel(int Pixel, byte red, byte green, byte blue) {
#ifdef ADAFRUIT_NEOPIXEL_H
// NeoPixel
strip.setPixelColor(Pixel, strip.Color(red, green, blue));
#endif
#ifndef ADAFRUIT_NEOPIXEL_H
// FastLED
leds[Pixel].r = red;
leds[Pixel].g = green;
leds[Pixel].b = blue;
#endif
}
void setAll(byte red, byte green, byte blue) {
for(int i = 0; i < NUM_LEDS; i++ ) {
setPixel(i, red, green, blue);
}
showStrip();
}
void Twinkle(byte red, byte green, byte blue, int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0,0,0);
for (int i=0; i<Count; i++) {
setPixel(random(NUM_LEDS),red,green,blue);
showStrip();
delay(SpeedDelay);
if(OnlyOne) {
setAll(0,0,0);
}
}
delay(SpeedDelay);
}
void TwinkleRandom(int Count, int SpeedDelay, boolean OnlyOne) {
setAll(0,0,0);
for (int i=0; i<Count; i++) {
setPixel(random(NUM_LEDS),random(0,255),random(0,255),random(0,255));
showStrip();
delay(SpeedDelay);
if(OnlyOne) {
setAll(0,0,0);
}
}
delay(SpeedDelay);
}
void Sparkle(byte red, byte green, byte blue, int SpeedDelay) {
int Pixel = random(NUM_LEDS);
setPixel(Pixel,red,green,blue);
showStrip();
delay(SpeedDelay);
setPixel(Pixel,0,0,0);
}
void SnowSparkle(byte red, byte green, byte blue, int SparkleDelay, int SpeedDelay) {
setAll(red,green,blue);
int Pixel = random(NUM_LEDS);
setPixel(Pixel,0xff,0xff,0xff);
showStrip();
delay(SparkleDelay);
setPixel(Pixel,red,green,blue);
showStrip();
delay(SpeedDelay);
}
void colorWipe2(byte red, byte green, byte blue, int SpeedDelay) {
for(uint16_t i=0; i<NUM_LEDS; i++) {
setPixel(i, red, green, blue);
showStrip();
delay(SpeedDelay);
}
}
I'm hoping I can play music while animating the neopixel.
Anyone who can shed light on this would be much appreciated. I'm still re-learning coding in Arduino so please forgive on this rough sketch...
Thank you!