Skip to content

Instantly share code, notes, and snippets.

@commak
Created March 23, 2015 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save commak/9bcf6ab6ab9231d10c05 to your computer and use it in GitHub Desktop.
Save commak/9bcf6ab6ab9231d10c05 to your computer and use it in GitHub Desktop.
Active wearable project
#include <SFE_TPA2016D2.h>
//Kama Kaczmarczyk
//#2483626
//ActiveWearableProject
//Prof Kate Hartman
//Code adapted from 'Trigger' by Mike Grusin and Steffanie Chaffin
// "Trigger" example sketch for Lilypad MP3 Player
// Mike Grusin, SparkFun Electronics
// http://www.sparkfun.com
// This sketch (which is preloaded onto the board by default),
// will play a specific audio file when one of the five trigger
// inputs (labeled T1 - T5) is momentarily grounded.
// You can place up to five audio files on the micro-SD card.
// These files should have the desired trigger number (1 to 5)
// as the first character in the filename. The rest of the
// filename can be anything you like. Long file names will work,
// but will be translated into short 8.3 names. We recommend using
// 8.3 format names without spaces, but the following characters
// are OK: .$%'-_@~`!(){}^#&. The VS1053 can play a variety of
// audio formats, see the datasheet for information.
// By default, a new trigger will interrupt a playing file, except
// itself. (In other words, a new trigger won't restart an
// already-playing file). You can easily change this behavior by
// modifying the global variables "interrupt" and "interruptself"
// below.
// This sketch can output serial debugging information if desired
// by changing the global variable "debugging" to true. Note that
// this will take away trigger inputs 4 and 5, which are shared
// with the TX and RX lines. You can keep these lines connected to
// trigger switches and use the serial port as long as the triggers
// are normally open (not grounded) and remain ungrounded while the
// serial port is in use.
// Uses the SdFat library by William Greiman, which is supplied
// with this archive, or download from http://code.google.com/p/sdfatlib/
// Uses the SFEMP3Shield library by Bill Porter, which is supplied
// with this archive, or download from http://www.billporter.info/
// License:
// We use the "beerware" license for our firmware. You can do
// ANYTHING you want with this code. If you like it, and we meet
// someday, you can, but are under no obligation to, buy me a
// (root) beer in return.
// Have fun!
// -your friends at SparkFun
// Revision history:
// 1.0 initial release MDG 2012/11/01
// We'll need a few libraries to access all this hardware!
#include <SPI.h> // To talk to the SD card and MP3 chip
#include <SdFat.h> // SD card file system
#include <SFEMP3Shield.h> // MP3 decoder chip
#include <Wire.h> // to talk to the Amplifier chip
#include <SFE_TPA2016D2.h>
// Constants for the trigger input pins, which we'll place
// in an array for convenience:
const int TRIG1 = A4;
int trigger = TRIG1;
const int tPin = A5;
// And a few outputs we'll be using:
const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
const int SD_CS = 9; // Chip Select for SD card
// Create library objects:
SFEMP3Shield MP3player;
SdFat sd;
// Set debugging = true if you'd like status messages sent
// to the serial port. Note that this will take over trigger
// inputs 4 and 5. (You can leave triggers connected to 4 and 5
// and still use the serial port, as long as you're careful to
// NOT ground the triggers while you're using the serial port).
boolean debugging = true;
// Set interrupt = false if you would like a triggered file to
// play all the way to the end. If this is set to true, new
// triggers will stop the playing file and start a new one.
boolean interrupt = true;
// Set interruptself = true if you want the above rule to also
// apply to the same trigger. In other words, if interrupt = true
// and interruptself = false, subsequent triggers on the same
// file will NOT start the file over. However, a different trigger
// WILL stop the original file and start a new one.
boolean interruptself = true;
// We'll store the five filenames as arrays of characters.
// "Short" (8.3) filenames are used, followed by a null character.
char filename[5][13];
SFE_TPA2016D2 amp;
//Analog read pins for Accelerometer
// The x-direction is the only one we will use
const int xPin = A0;
void setup()
{
int x = 0;
int index;
SdFile file;
byte result;
char tempfilename[13];
// Set the trigger pin as inputs, and turn on the
// internal pullup resistors:
pinMode(trigger,INPUT);
digitalWrite(trigger,HIGH);
// The board uses a single I/O pin to select the
// mode the MP3 chip will start up in (MP3 or MIDI),
// and to enable/disable the amplifier chip:
pinMode(EN_GPIO1,OUTPUT);
digitalWrite(EN_GPIO1,LOW); // MP3 mode / amp off
// Turn on the Wire (I2C) library (amplifier control)
Wire.begin();
// If debugging is true, initialize the serial port:
// (The 'F' stores constant strings in flash memory to save RAM)
if (debugging)
{
Serial.begin(9600);
Serial.println(F("Lilypad MP3 Player trigger sketch"));
}
// Initialize the SD card; SS = pin 9, half speed at first
if (debugging) Serial.print(F("initialize SD card... "));
result = sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success
if (result != 1) // Problem initializing the SD card
{
if (debugging) Serial.print(F("error, halting"));
}
else
if (debugging) Serial.println(F("success!"));
// Start up the MP3 library
if (debugging) Serial.print(F("initialize MP3 chip... "));
result = MP3player.begin(); // 0 or 6 for success
// Check the result, see the library readme for error codes.
if ((result != 0) && (result != 6)) // Problem starting up
{
if (debugging)
{
Serial.print(F("error code "));
Serial.print(result);
Serial.print(F(", halting."));
}
}
else
if (debugging) Serial.println(F("success!"));
// Now we'll access the SD card to look for any (audio) files
// starting with the characters '1' to '5':
if (debugging) Serial.println(F("reading root directory"));
// Start at the first file in root and step through all of them:
sd.chdir("/",true);
while (file.openNext(sd.vwd(),O_READ))
{
// get filename
file.getFilename(tempfilename);
// Does the filename start with char '1' through '5'?
if (tempfilename[0] >= '1' && tempfilename[0] <= '5')
{
// Yes! subtract char '1' to get an index of 0 through 4.
index = tempfilename[0] - '1';
// Copy the data to our filename array.
strcpy(filename[index],tempfilename);
if (debugging) // Print out file number and name
{
Serial.print(F("found a file with a leading "));
Serial.print(index+1);
Serial.print(F(": "));
Serial.println(filename[index]);
}
}
else
if (debugging)
{
Serial.print(F("found a file w/o a leading number: "));
Serial.println(tempfilename);
}
file.close();
}
if (debugging)
Serial.println(F("done reading root directory"));
if (debugging) // List all the files we saved:
{
for(x = 0; x <= 4; x++)
{
Serial.print(F("trigger "));
Serial.print(x+1);
Serial.print(F(": "));
Serial.println(filename[x]);
}
}
}
void loop()
{
int t = 0; // current trigger
static int last_t; // previous (playing) trigger
int x;
int numOfSongs = 3;
byte result;
boolean run = true;
int vol;
int xAcc;
int count;
int acc;
int set;
int zero = 800;
set = 60;
MP3player.setVolume(set,set);
while(run)
{
//if the MP3 player isn't playing a song start one
if(MP3player.isPlaying() == 0)
{
MP3player.playMP3(filename[t]);
last_t = t; // Save playing trigger
t = t+1;
}
//if the next butten is pushed and there is a song left in the loop
//start playing, otherwise restart the loop
if(digitalRead(trigger) == LOW && t<numOfSongs)
{
// Wait for trigger to return high for a solid 50ms
x = 0;
while(x < 50)
{
if (digitalRead(trigger) == HIGH)
x++;
else
x = 0;
delay(1);
}
if (debugging)
{
Serial.print(F("got trigger "));
Serial.println(t+1);
}
// Do we have a valid filename for this trigger?
// (Invalid filenames will have 0 as the first character)
if (filename[t][0] == 0)
{
t = -1;
if (debugging)
{
Serial.println(F("no file with that number"));
}
}
else // We do have a filename for this trigger!
{
if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself))
{
if (debugging)
Serial.println(F("stopping playback"));
MP3player.stopTrack();
}
// Play the filename associated with the trigger number.
// (If a file is already playing, this command will fail
// with error #2).
MP3player.playMP3(filename[t]);
last_t = t; // Save playing trigger
if(debugging)
{
if(result != 0)
{
Serial.print(F("error "));
Serial.print(result);
Serial.print(F(" when trying to play track "));
}
else
{
Serial.print(F("playing "));
}
Serial.println(filename[t]);
}
}
t = t+1;
}
else
{
if (t == 3)
run = false;
}
//This part controls the volume. If the Train pin
// is down it will play a default volume. If the
//Train pin is tripped it will count the amount of time the
//amount of time of negitive acceleration in the arm movement
// more threshold can be defined but the transistion becomes shakey
int train = digitalRead(tPin);
if(train == 0) //if sound control is turned on
{
xAcc = analogRead(A0);
acc = zero-xAcc;
count = 0;
while (acc < 0)//see how long acc is neg
{
count = count +1;
xAcc = analogRead(A0);
acc = zero-xAcc;
}
vol = count;//time = vol
count = 0;
Serial.println(vol);
delay(100);
if(vol>300)
{
if(vol<=700)
{
set = 40;
}
if(vol>700)
{
set = 60;
}
}
MP3player.setVolume(set,set);
}
else //no sound control
{
set = 60;
MP3player.setVolume(set,set);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment