Can anyone tell me, by looking at the below code, what I might need to do in order to get these 2 routines called within the "void loop" to play nicely together?
void loop(){
readTouchInputs();
readRawInputs();
}
Basically, I can do one call, or the other, but not both. I think there's a comflict with the MPR121.updateTouchData(); call and the MPR121.updateAll();
A little bit of background - - - I'm basically trying to modify Bareconductive's Touchboard to play mp3's when touched AND pass proximity-data back to Bareconductive's graphing software. The mp3 routine is the "readTouchInputs()" routine, and the "pass data to the graphing software" routine is the "readRawInputs" call. I'm finding that I can do one, or the other, but doing both is unreliable. From what I can tell, mp3's will play, but only about 10% of the time - - - I suspect that the loop is getting bogged down with the MPR121.updateAll() calls. Perhaps if there's some way to only perform the calls X number of times a second?
Any advice is greatly appreciated.
// 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();
readRawInputs();
}
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(lastPlayed==i){
// if we're already playing the requested track, stop it
MP3player.stopTrack();
Serial.print("stopping track ");
Serial.println(i-firstPin);
} else {
// 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);
}
}
}
}
}
}
void readRawInputs(){
int i;
MPR121.updateAll();
Serial.print("TOUCH: ");
for(i=0; i<13; i++){ // 13 touch values
Serial.print(MPR121.getTouchData(i), DEC);
if(i<12) Serial.print(" ");
}
Serial.println();
Serial.print("TTHS: ");
for(i=0; i<13; i++){ // 13 touch thresholds
Serial.print(MPR121.getTouchThreshold(i), DEC);
if(i<12) Serial.print(" ");
}
Serial.println();
Serial.print("RTHS: ");
for(i=0; i<13; i++){ // 13 release thresholds
Serial.print(MPR121.getReleaseThreshold(i), DEC);
if(i<12) Serial.print(" ");
}
Serial.println();
Serial.print("FDAT: ");
for(i=0; i<13; i++){ // 13 filtered values
Serial.print(MPR121.getFilteredData(i), DEC);
if(i<12) Serial.print(" ");
}
Serial.println();
Serial.print("BVAL: ");
for(i=0; i<13; i++){ // 13 baseline values
Serial.print(MPR121.getBaselineData(i), DEC);
if(i<12) Serial.print(" ");
}
Serial.println();
// the trigger and threshold values refer to the difference between
// the filtered data and the running baseline - see p13 of
// http://www.freescale.com/files/sensors/doc/data_sheet/MPR121.pdf
Serial.print("DIFF: ");
for(i=0; i<13; i++){ // 13 value pairs
Serial.print(MPR121.getBaselineData(i)-MPR121.getFilteredData(i), DEC);
if(i<12) Serial.print(" ");
}
Serial.println();
}