Advertisement
Guest User

Touch MP3 + LED exemple

a guest
Jan 20th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1.  
  2. /*******************************************************************************
  3.  
  4. Bare Conductive Touch MP3 player
  5. ------------------------------
  6.  
  7. Touch_MP3.ino - touch triggered MP3 playback
  8.  
  9. You need twelve MP3 files named TRACK000.mp3 to TRACK011.mp3 in the root of the
  10. microSD card.
  11.  
  12. When you touch electrode E0, TRACK000.mp3 will play. When you touch electrode
  13. E1, TRACK001.mp3 will play, and so on.
  14.  
  15. SD card
  16. TRACK000.mp3
  17. TRACK001.mp3
  18. TRACK002.mp3
  19. TRACK003.mp3
  20. TRACK004.mp3
  21. TRACK005.mp3
  22. TRACK006.mp3
  23. TRACK007.mp3
  24. TRACK008.mp3
  25. TRACK009.mp3
  26. TRACK010.mp3
  27. TRACK011.mp3
  28.  
  29. Based on code by Jim Lindblom and plenty of inspiration from the Freescale
  30. Semiconductor datasheets and application notes.
  31.  
  32. Bare Conductive code written by Stefan Dzisiewski-Smith and Peter Krige.
  33.  
  34. This work is licensed under a MIT license https://opensource.org/licenses/MIT
  35.  
  36. Copyright (c) 2016, Bare Conductive
  37.  
  38. Permission is hereby granted, free of charge, to any person obtaining a copy
  39. of this software and associated documentation files (the "Software"), to deal
  40. in the Software without restriction, including without limitation the rights
  41. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  42. copies of the Software, and to permit persons to whom the Software is
  43. furnished to do so, subject to the following conditions:
  44.  
  45. The above copyright notice and this permission notice shall be included in all
  46. copies or substantial portions of the Software.
  47.  
  48. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  49. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  50. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  51. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  52. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  53. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  54. SOFTWARE.
  55.  
  56. *******************************************************************************/
  57.  
  58. // compiler error handling
  59. #include "Compiler_Errors.h"
  60.  
  61. // touch includes
  62. #include <MPR121.h>
  63. #include <Wire.h>
  64. #define MPR121_ADDR 0x5C
  65. #define MPR121_INT 4
  66.  
  67. // mp3 includes
  68. #include <SPI.h>
  69. #include <SdFat.h>
  70. #include <FreeStack.h>
  71. #include <SFEMP3Shield.h>
  72.  
  73. // mp3 variables
  74. SFEMP3Shield MP3player;
  75. byte result;
  76. int lastPlayed = 0;
  77.  
  78. // mp3 behaviour defines
  79. #define REPLAY_MODE FALSE // By default, touching an electrode repeatedly will
  80. // play the track again from the start each time.
  81. //
  82. // If you set this to FALSE, repeatedly touching an
  83. // electrode will stop the track if it is already
  84. // playing, or play it from the start if it is not.
  85.  
  86. // touch behaviour definitions
  87. #define firstPin 0
  88. #define lastPin 11
  89.  
  90. // sd card instantiation
  91. SdFat sd;
  92.  
  93. // LED pins
  94. // maps electrode 0 to digital 0, electrode 2 to digital 1, electrode 3 to digital 10 and so on...
  95. // A0..A5 are the analogue input pins, used as digital outputs in this example
  96. const int ledPins[12] = {0, 1, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5};
  97.  
  98. void setup(){
  99. Serial.begin(57600);
  100.  
  101. //while (!Serial) ; {} //uncomment when using the serial monitor
  102. Serial.println("Bare Conductive Touch MP3 player");
  103.  
  104. if(!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
  105.  
  106. if(!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
  107. MPR121.setInterruptPin(MPR121_INT);
  108.  
  109. MPR121.setTouchThreshold(40);
  110. MPR121.setReleaseThreshold(20);
  111.  
  112. result = MP3player.begin();
  113. MP3player.setVolume(10,10);
  114.  
  115. if(result != 0) {
  116. Serial.print("Error code: ");
  117. Serial.print(result);
  118. Serial.println(" when trying to start MP3 player");
  119. }
  120.  
  121. for(int i=firstPin; i<=lastPin; i++){
  122. pinMode(ledPins[i], OUTPUT);
  123. digitalWrite(ledPins[i], LOW);
  124. }
  125.  
  126. }
  127.  
  128. void loop(){
  129. readTouchInputs();
  130. checkTrackFinished();
  131. }
  132.  
  133.  
  134. void readTouchInputs(){
  135. if(MPR121.touchStatusChanged()){
  136.  
  137. MPR121.updateTouchData();
  138.  
  139. // only make an action if we have one or fewer pins touched
  140. // ignore multiple touches
  141.  
  142. if(MPR121.getNumTouches()<=1){
  143. for (int i=0; i < 12; i++){ // Check which electrodes were pressed
  144. if(MPR121.isNewTouch(i)){
  145.  
  146. //pin i was just touched
  147. Serial.print("pin ");
  148. Serial.print(i);
  149. Serial.println(" was just touched");
  150.  
  151. if(i<=lastPin && i>=firstPin){
  152. if(MP3player.isPlaying()){
  153. if(lastPlayed==i && !REPLAY_MODE){
  154. // if we're already playing the requested track, stop it
  155. // (but only if we're in REPLAY_MODE)
  156. MP3player.stopTrack();
  157. Serial.print("stopping track ");
  158. Serial.println(i-firstPin);
  159. // switch off the relevant LED output
  160. digitalWrite(ledPins[lastPlayed], LOW);
  161. } else {
  162. // if we're already playing a different track (or we're in
  163. // REPLAY_MODE), stop and play the newly requested one
  164. MP3player.stopTrack();
  165. MP3player.playTrack(i-firstPin);
  166. Serial.print("playing track ");
  167. Serial.println(i-firstPin);
  168.  
  169. // switch off the relevant LED output
  170. digitalWrite(ledPins[lastPlayed], LOW);
  171.  
  172. // switch on the new LED output
  173. digitalWrite(ledPins[i], HIGH);
  174.  
  175. // don't forget to update lastPlayed - without it we don't
  176. // have a history
  177. lastPlayed = i;
  178. }
  179. } else {
  180. // if we're playing nothing, play the requested track
  181. // and update lastplayed
  182. MP3player.playTrack(i-firstPin);
  183. Serial.print("playing track ");
  184. Serial.println(i-firstPin);
  185.  
  186. // switch on the new LED output
  187. digitalWrite(ledPins[i], HIGH);
  188.  
  189. lastPlayed = i;
  190. }
  191. }
  192. }else{
  193. if(MPR121.isNewRelease(i)){
  194. Serial.print("pin ");
  195. Serial.print(i);
  196. Serial.println(" is no longer being touched");
  197. }
  198. }
  199. }
  200. }
  201. }
  202. }
  203.  
  204. void checkTrackFinished(){
  205. if(!MP3player.isPlaying()){
  206. digitalWrite(ledPins[lastPlayed], LOW);
  207. }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement