Bare Conductive mp3 Player

The Bare Conductive Touch Board is an Arduino based microcontroller board with built in capacitive electrodes, an SD card reader, a midi controller and the ability to decode MP3 files. The company also make conductive paint and sells this and the Touch Board in an attractive Starter Kit, which includes a portable speaker, jumper wires, stencils and pre-printed paper.

Painting the display

Accurate use of the paint is beyond my artistic skills, so I decided to make their example touch board using the renewable house stencil and coloured paper. I used a foam board for the backing.

On my first attempt I sprayed an adhesive to the back of the stencil, which just made the board sticky. For my second go I just stuck the stencil down with masking tape. I used the included brush to “stipple” the paint over the stencil. I sprayed a fixative over the paint after it dried to stop smudging due to wet fingers.

Copying the music tracks

The kit comes with a 512 MB micro SD card, but I replaced this with the maximum supported capacity of 2 GB from an old mobile phone.

The example Arduino code plays a single track for each touch electrode. My aim was to have a wall mounted MP3 player that would include functionality like play, pause, skip, randomise and volume controls. Whilst the root directory stores “relaxing” tracks I added a subdirectory for “action” tracks.

The MP3 player code only handles filenames of the format TRACKxxx.mp3 where xxx is of the form 000 to 999. I wrote a BASH script to rename the mp3 files in a directory.

#!/bin/bash
i=0
tracknum="000"
for file in *.mp3
do
    if [ $i -lt 10 ]
    then
        tracknum="00${i}"
    elif [ $i -lt 100 ]
    then
        tracknum="0${i}"
    else
        tracknum="$i"
    fi
    mv "$file" "TRACK${tracknum}.mp3"
    i=$((i+1))
done

Arduino code

Note that this code is very large. I get a warning about the lack of remaining memory available when I upload it to the Arduino. Not all electrodes are used due to the stencil leaving some disconnected.

/*******************************************************************************
 Allrite's Touch MP3 player
 ------------------------------
  E0 - Stop
  E1 - Play
  E2 - Pause
  E3 - 
  E4 - Skip backwards
  E5 - Action or root folder
  E6 -
  E7 - Skip forwards
  E8 - Volume Down
  E9 - Mute
  E10 - Volume Up
  E11 - Change Mode (Random/Continuous)
 
Files should be named TRACKxxx.mp3 where xxx is from 000 to 999. 
Additional files can be stored in a subdirectory named action.
Change the TRACKCOUNT value to match the number of tracks on the SD card.
  
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 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.
*******************************************************************************/
// 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 <FreeStack.h> 
#include <SFEMP3Shield.h>
// mp3 settings
#define TRACKCOUNT 115
#define MINVOLUME 65278
#define MAXVOLUME 15
#define OFF 0
#define ON 1
#define CONTPLAY 1
#define RANDPLAY 2
// mp3 variables
SFEMP3Shield MP3player;
byte result;
int lastPlayed = 0;
int currentTrack = 0;
int playedTracks[TRACKCOUNT];
long volume = 50;
int playMode = RANDPLAY;
int inct = 0;
int muted = OFF;
int action = OFF;
// mp3 behaviour defines
#define REPLAY_MODE TRUE  // By default, touching an electrode repeatedly will 
                          // play the track again from the start each time.
                          //
                          // If you set this to FALSE, repeatedly touching an 
                          // electrode will stop the track if it is already 
                          // playing, or play it from the start if it is not.
// touch behaviour definitions
#define firstPin 0
#define lastPin 11
// sd card instantiation
SdFat sd;
SdFile file;
void setup(){  
  Serial.begin(57600);
  
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(LED, OUTPUT); // For displaying lights
   
  //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);
  MPR121.setTouchThreshold(40);
  MPR121.setReleaseThreshold(20);
  result = MP3player.begin();
  MP3player.setVolume(volume,volume);
 
  if(result != 0) {
    Serial.print("Error code: ");
    Serial.print(result);
    Serial.println(" when trying to start MP3 player");
  }
}
void loop(){
  if (playMode == OFF) {
    readTouchInputs();
  } else {
    playMusic();
  }
}
void playMusic() {
  int inct = 0;
  if (playMode != OFF ) {
    if (playMode == RANDPLAY) {
      randomSeed(analogRead(0));
      if (currentTrack == 0) {
        currentTrack = random(TRACKCOUNT);
      }
      MP3player.playTrack(currentTrack);
    }
    do {
      switch(playMode) {
        case RANDPLAY:
          if (!MP3player.isPlaying()) {
            MP3player.stopTrack();
            playedTracks[inct] = currentTrack;
            do
              currentTrack = random(TRACKCOUNT);
            while (checkPlayed(inct, currentTrack));
            inct++;
            MP3player.playTrack(currentTrack);
            Serial.print("Playing track: ");
            Serial.println(currentTrack);
          }
          break;
        case CONTPLAY:
          if (!MP3player.isPlaying()) {
            inct++;
            MP3player.stopTrack();
            currentTrack = inct;
            MP3player.playTrack(inct);
            Serial.print("Playing track: ");
            Serial.println(currentTrack);
          }
        default:
          break;
      }
      readTouchInputs();
    } while (inct < TRACKCOUNT);
    playMode = OFF;
  }
}
boolean checkPlayed(int i, int track) {
  boolean played = false;
  for (int j = 0; j < i + 1; j++) {
    if (playedTracks[j] == track) {
      played = true;
      break;
    }
  }
  return played; 
}
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);
            switch(i) {
              case 0:
                // Stop
                playMode = OFF;
                currentTrack = 0;
                MP3player.stopTrack();
                Serial.println("Stopped playing");
                break;
              case 1:
                // Play
                if (playMode == OFF) {
                  playMode = CONTPLAY;
                  MP3player.playTrack(currentTrack);
                  Serial.print("Playing track: ");
                  Serial.println(currentTrack);
                } else {
                  MP3player.resumeMusic();
                  Serial.print("Resume playing: ");
                  Serial.println(currentTrack);
                  
                }
                break;
              case 2:
                // Pause
                MP3player.pauseMusic();
                Serial.println("Paused music ");
                break;
              case 3:
                // Nothing mapped
                break;
              case 4:
                // Replay (skip back)
                MP3player.stopTrack();
                currentTrack = currentTrack - 1;
                if (playMode == RANDPLAY && inct > 0) {
                  currentTrack = playedTracks[inct - 1];
                }
                if (currentTrack < 0) {
                  currentTrack = 0;
                }
                MP3player.playTrack(currentTrack);
                Serial.print("Skipped to track: ");
                Serial.println(currentTrack);
                break; 
              case 5:
                // action directory
                if (action == OFF) {
                  if(!sd.chdir("action")){ // select our directory
                    Serial.println("error changing to action directory"); // error message if reqd.
                  } else {
                    MP3player.stopTrack();
                    action = ON;
                    currentTrack = 0;
                    inct = 0;
                    MP3player.playTrack(currentTrack);
                    Serial.print("Current track: ");
                    Serial.println( currentTrack );
                    Serial.println("action directory selected");    
                  }
                } else {
                  if(!sd.chdir()){ // select root
                    Serial.println("error changing to root directory"); // error message if reqd.
                  } else {
                    MP3player.stopTrack();
                    action = OFF;
                    currentTrack = 0;
                    inct = 0;
                    MP3player.playTrack(currentTrack);
                    Serial.print("Current track: ");
                    Serial.println( currentTrack );
                    Serial.println("Root directory selected");
                  }
                }
                break;
              case 6:
                // Nothing mapped
                break;
              case 7:
                // Skip forwards
                MP3player.stopTrack();
                currentTrack = currentTrack + 1;
                if (currentTrack == TRACKCOUNT) {
                  currentTrack = TRACKCOUNT - 1;
                }
                MP3player.playTrack(currentTrack);
                Serial.print("Skipped to track: ");
                Serial.println(currentTrack);
                break;
              case 8:
                 // Volume down
                if ( volume < MINVOLUME - 1) {
                  volume = volume + 1;
                } else {
                  volume = MINVOLUME; 
                }
                Serial.print("Volume: ");
                Serial.println(volume);
                MP3player.setVolume(volume,volume);
                break;
              case 9:
                 // Mute
                if (muted == OFF) {
                  MP3player.setVolume(MINVOLUME,MINVOLUME);
                  Serial.println("Muted volume");
                  muted = ON;
                } else {
                  muted = OFF;
                  MP3player.setVolume(volume, volume);
                  Serial.println("Unmuted volume");
                }
                break; 
              case 10:
                // Volume up
                if ( volume > MAXVOLUME + 1) {
                  volume = volume - 1;
                } else {
                  volume = MAXVOLUME; 
                }
                Serial.print("Volume: ");
                Serial.println(volume);
                MP3player.setVolume(volume,volume);
                break;
              case 11:
                // Change play mode
                MP3player.stopTrack();
                if (playMode == CONTPLAY) {
                  playMode = RANDPLAY;
                  randomSeed(analogRead(0));
                  currentTrack = random(TRACKCOUNT); 
                } else {
                  playMode = CONTPLAY;
                  currentTrack = 0;
                }
                
                Serial.print("Changed mode to: ");
                Serial.println(playMode);
                break;              
              default:
                break;
            }
        } else {
          if(MPR121.isNewRelease(i)){
            Serial.print("pin ");
            Serial.print(i);
            Serial.println(" is no longer being touched");
            digitalWrite(LED_BUILTIN, LOW);
         } 
        }
      }
    }
  }
}
Touch Board MP3 player in action

Submit a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s