Skip to content

Instantly share code, notes, and snippets.

@stefandz
Created April 20, 2015 11:38
Show Gist options
  • Save stefandz/4f7e4b875317cc0f491a to your computer and use it in GitHub Desktop.
Save stefandz/4f7e4b875317cc0f491a to your computer and use it in GitHub Desktop.
Bare Conductive Touch MP3 code with repeated play (as opposed to touch-again-to-stop) behaviour
/*******************************************************************************
Bare Conductive Compiler Error Handler
--------------------------------------
Compiler_Errors.h - avoids incorrect builds due to wrong environment
Bare Conductive code written by Stefan Dzisiewski-Smith and Peter Krige.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0
Unported License (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#ifndef COMPILER_ERRORS_H
#define COMPILER_ERRORS_H
// our automatic SFEMP3Shield library pin mapping only works with
// Arduino >= 1.5.6
#if ARDUINO < 156
#error Please upgrade your Arduino IDE to 1.5.6 or greater
#else
// for SFEMP3Shield pin mapping to work correctly Bare Conductive Touch Board
// must be selected in Tools -> Board
#ifndef ARDUINO_AVR_BARETOUCH
#error Please select "Bare Conductive Touch Board" in the Tools -> Board menu.
#endif
#endif
#endif // COMPILER_ERRORS_H
/*******************************************************************************
Bare Conductive Touch MP3 player (with repeated Play)
-----------------------------------------------------
Touch_MP3_Repeated_Play.ino - touch triggered MP3 playback with repeated play
Based on code by Jim Lindblom and plenty of inspiration from the Freescale
Semiconductor datasheets and application notes.
Bare Conductive code written by Stefan Dzisiewski-Smith and Peter Krige.
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0
Unported License (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
// compiler error handling
#include "Compiler_Errors.h"
// touch includes
#include <MPR121.h>
#include <Wire.h>
#define MPR121_ADDR 0x5C
#define MPR121_INT 4
// mp3 includes
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>
// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
// sd card instantiation
SdFat sd;
// define LED_BUILTIN for older versions of Arduino
#ifndef LED_BUILTIN
#define LED_BUILTIN 13
#endif
void setup(){
Serial.begin(57600);
pinMode(LED_BUILTIN, OUTPUT);
//while (!Serial) ; {} //uncomment when using the serial monitor
Serial.println("Bare Conductive Touch MP3 player");
if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
MPR121.setInterruptPin(MPR121_INT);
result = MP3player.begin();
MP3player.setVolume(10,10);
if(result != 0) {
Serial.print("Error code: ");
Serial.print(result);
Serial.println(" when trying to start MP3 player");
}
}
void loop(){
readTouchInputs();
}
void readTouchInputs(){
if(MPR121.touchStatusChanged()){
MPR121.updateTouchData();
// only make an action if we have one or fewer pins touched
// ignore multiple touches
if(MPR121.getNumTouches()<=1){
for (int i=0; i < 12; i++){ // Check which electrodes were pressed
if(MPR121.isNewTouch(i)){
//pin i was just touched
Serial.print("pin ");
Serial.print(i);
Serial.println(" was just touched");
digitalWrite(LED_BUILTIN, HIGH);
if(i<=lastPin && i>=firstPin){
if(MP3player.isPlaying()){
// if we're already playing a different track, stop that
// one and play the newly requested one
MP3player.stopTrack();
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
// don't forget to update lastPlayed - without it we don't
// have a history
lastPlayed = i;
} else {
// if we're playing nothing, play the requested track
// and update lastplayed
MP3player.playTrack(i-firstPin);
Serial.print("playing track ");
Serial.println(i-firstPin);
lastPlayed = i;
}
}
}else{
if(MPR121.isNewRelease(i)){
Serial.print("pin ");
Serial.print(i);
Serial.println(" is no longer being touched");
digitalWrite(LED_BUILTIN, LOW);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment