I’m working on a project in which as a magnetic switch is activated, music, lights, and a toy are all activated. They all turn off if the switch isn’t hit within a certain threshold of time. I’ve gotten this code to work with the lights, toy, and a potentiometer to change the threshold (dwell). However, once I’ve added the Sparkfun MP3 Shield code, nothing works and the DotStar LED strip just stays white. I’m using a Sparkfun RedBoard and a generic portable speaker with a jack. I’ve gotten the MP3 and DotStars to work fine with this same code and interrupt, but when I put them together they mess up.
Any advice or fixes will be much appreciated!
#include <Adafruit_DotStar.h>
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
#define NUMPIXELS 30
#define DATAPIN 11
#define CLOCKPIN 13
Adafruit_DotStar strip = Adafruit_DotStar(
NUMPIXELS, DATAPIN, CLOCKPIN, DOTSTAR_BRG);
const int magSensor = 3;
volatile int timer = 0;
const int toyPin = 12;
const int dwellPin = A0;
volatile int dwell;
#if defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_Timer1
#include <TimerOne.h>
#elif defined(USE_MP3_REFILL_MEANS) && USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer
#include <SimpleTimer.h>
#endif
SdFat sd;
SFEMP3Shield MP3player;
int8_t current_song = 1;
boolean began = false;
int head = 0, tail = -10; // Index of first 'on' and 'off' pixels
uint32_t color = 0xFF0000; // 'On' color (starts red)
void setup() {
Serial.begin(115200);
strip.begin();
strip.show();
if (!sd.begin(SD_SEL, SPI_FULL_SPEED)) sd.initErrorHalt();
// depending upon your SdCard environment, SPI_HAVE_SPEED may work better.
if (!sd.chdir("/")) sd.errorHalt("sd.chdir");
MP3player.begin();
pinMode(magSensor, INPUT);
pinMode(toyPin, OUTPUT);
digitalWrite(toyPin, LOW);
attachInterrupt(digitalPinToInterrupt(3), magCheck, LOW);
}
void loop() {
#if defined(USE_MP3_REFILL_MEANS) \
&& ( (USE_MP3_REFILL_MEANS == USE_MP3_SimpleTimer) \
|| (USE_MP3_REFILL_MEANS == USE_MP3_Polled) )
MP3player.available();
#endif
dwell = map(analogRead(dwellPin), 0, 662, 40, 5); //reset dwell in case pot has moved
while (timer > 0) {
lightsOn();
toyOn();
musicOn();
timer--;
delay(100);
}
if (timer == 0) {
lightsOff();
toyOff();
musicOff();
}
}
void magCheck() {
timer = dwell; // every time mag is LOW, update timer to keep going
}
void lightsOn() {
strip.setPixelColor(head, color); // 'On' pixel at head
strip.setPixelColor(tail, 0); // 'Off' pixel at tail
strip.show(); // Refresh strip
if (++head >= NUMPIXELS) { // Increment head index. Off end of strip?
head = 0; // Yes, reset head index to start
if ((color >>= 8) == 0) // Next color (R->G->B) ... past blue now?
color = 0xFF0000; // Yes, reset to red
}
if (++tail >= NUMPIXELS) tail = 0; // Increment, reset tail index
}
void lightsOff() {
for (uint8_t i = 0; i < NUMPIXELS; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
}
}
void toyOn() {
digitalWrite(toyPin, HIGH);
}
void toyOff() {
digitalWrite(toyPin, LOW);
}
void musicOn() {
if (MP3player.getState() == paused_playback) { //In the middle of song: resume track
MP3player.resumeMusic();
} else if (!MP3player.isPlaying() && !began) { //Before the first song: play track
MP3player.playTrack(current_song);
began = true;
} else if (!MP3player.isPlaying() && began) { //Between songs: move to next track and play
current_song++;
MP3player.playTrack(current_song);
}
}
void musicOff() {
MP3player.pauseMusic();
}