Created
June 30, 2015 03:04
-
-
Save robseward/33dc4d03bb77a2339cb8 to your computer and use it in GitHub Desktop.
Current sensing music player
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*************************************************** | |
This code takes input from a non-ivasive current sensor connected to a lamp. | |
When the lamp is turned on it plays a track on the VS1053 mp3 player (see | |
link below). The tracks advance each time the lamp is turned on and if the | |
lamp is left on tracks loop. | |
- Music files must be labeled track001.mp3, track002.mp3, etc. | |
- NUM_TRACKS must be set to the number of tracks on the SD card | |
Much of this code based off of Limor Fried's VS1053 sample code | |
Adafruit VS1053 Codec Breakout | |
----> https://www.adafruit.com/products/1381 | |
Non invasive current sensor: | |
----> https://www.sparkfun.com/products/11005 | |
2015 Rob Seward | |
****************************************************/ | |
// include SPI, MP3 and SD libraries | |
#include <SPI.h> | |
#include <Adafruit_VS1053.h> | |
#include <SD.h> | |
// define the pins used | |
//#define CLK 13 // SPI Clock, shared with SD card | |
//#define MISO 12 // Input data, from VS1053/SD card | |
//#define MOSI 11 // Output data, to VS1053/SD card | |
// Connect CLK, MISO and MOSI to hardware SPI pins. | |
// See http://arduino.cc/en/Reference/SPI "Connections" | |
// 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); | |
#define SENSOR_PIN A0 | |
#define NUM_TRACKS 13 | |
void setup() { | |
Serial.begin(9600); | |
Serial.println("Lamp 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 | |
} | |
// Set volume for left, right channels. lower numbers == louder volume! | |
musicPlayer.setVolume(1,1); | |
// Timer interrupts are not suggested, better to use DREQ interrupt! | |
//musicPlayer.useInterrupt(VS1053_FILEPLAYER_TIMER0_INT); // timer int | |
// If DREQ is on an interrupt pin (on uno, #2 or #3) we can do background | |
// audio playing | |
musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT); // DREQ int | |
pinMode(SENSOR_PIN, INPUT); | |
// Play another file in the background, REQUIRES interrupts! | |
//Serial.println(F("Playing track 001")); | |
//musicPlayer.startPlayingFile("track001.mp3"); | |
} | |
int trackCounter = 1; | |
bool previousLampOn = false; | |
void loop() { | |
char trackName[8]; | |
sprintf(trackName, "track%03d.mp3", trackCounter); | |
bool lampOn = isCurrentDetected(); | |
if (lampOn != previousLampOn){ | |
if (lampOn){ | |
musicPlayer.startPlayingFile(trackName); | |
advanceTrack(); | |
Serial.println(trackName); | |
} | |
else if (!lampOn){ | |
musicPlayer.stopPlaying(); | |
} | |
} | |
if (lampOn && !musicPlayer.playingMusic) { | |
musicPlayer.startPlayingFile(trackName); | |
advanceTrack(); | |
Serial.println(trackName); | |
} | |
previousLampOn = lampOn; | |
// print out the value you read: | |
//Serial.println(sensorValue); | |
delay(1); // delay in between reads for stability | |
} | |
void advanceTrack(){ | |
trackCounter = (trackCounter % NUM_TRACKS) + 1; | |
} | |
bool highVoltageDetected = false; | |
int flatVoltageCounter = 0; | |
bool isCurrentDetected(){ | |
int counterThreshold = 50; | |
if (flatVoltageCounter > counterThreshold) { | |
flatVoltageCounter = 0; | |
highVoltageDetected = false; | |
} | |
int sensorValue = analogRead(SENSOR_PIN); | |
int voltageThreshold = 50; | |
if (sensorValue > voltageThreshold) { | |
highVoltageDetected = true; | |
flatVoltageCounter = 0 ; | |
} | |
else { | |
flatVoltageCounter++; | |
} | |
// Serial.println(highVoltageDetected, DEC); | |
return highVoltageDetected; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment