Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1.  
  2. /*******************************************************************************
  3.  
  4. Bare Conductive Touch MP3 player for the Interactive Wall Kit
  5. -------------------------------------------------------------
  6.  
  7. https://github.com/BareConductive/touch_mp3_iwk
  8.  
  9. Touch_MP3_IWK.ino - touch triggered MP3 playback
  10.  
  11. This is a slightly modified version of the Touch_MP3.ino
  12.  
  13. You need twelve MP3 files named TRACK000.mp3 to TRACK011.mp3 in the root of the
  14. microSD card.
  15.  
  16. When you touch electrode E0, TRACK000.mp3 will play. When you touch electrode
  17. E1, TRACK001.mp3 will play, and so on.
  18.  
  19. SD card
  20. TRACK000.mp3
  21. TRACK001.mp3
  22. TRACK002.mp3
  23. TRACK003.mp3
  24. TRACK004.mp3
  25. TRACK005.mp3
  26. TRACK006.mp3
  27. TRACK007.mp3
  28. TRACK008.mp3
  29. TRACK009.mp3
  30. TRACK010.mp3
  31. TRACK011.mp3
  32.  
  33. Based on code by Jim Lindblom and plenty of inspiration from the Freescale
  34. Semiconductor datasheets and application notes.
  35.  
  36. Bare Conductive code written by Stefan Dzisiewski-Smith and Peter Krige.
  37.  
  38. This work is licensed under a MIT license https://opensource.org/licenses/MIT
  39.  
  40. Copyright (c) 2019, Bare Conductive
  41.  
  42. Permission is hereby granted, free of charge, to any person obtaining a copy
  43. of this software and associated documentation files (the "Software"), to deal
  44. in the Software without restriction, including without limitation the rights
  45. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  46. copies of the Software, and to permit persons to whom the Software is
  47. furnished to do so, subject to the following conditions:
  48.  
  49. The above copyright notice and this permission notice shall be included in all
  50. copies or substantial portions of the Software.
  51.  
  52. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  53. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  54. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  55. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  56. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  57. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  58. SOFTWARE.
  59.  
  60. ///////////////////////////
  61.  
  62. SOUS-MARIN
  63.  
  64. Toutes les électrodes bananent du MP3
  65. +
  66. Les électrodes 0, 1, 2 bananent sur des rubans LEDS sur les sorties 0, 1, 10
  67. Les autres électrodes bananent du 5V pour ce que l'on veut (leds, relay,....)
  68.  
  69. *******************************************************************************/
  70.  
  71.  
  72. // neopixel includes
  73. #include <Adafruit_NeoPixel.h>
  74. int nb_ledstip_1 = 10; // nombre de leds sur la première partie du ruban de led
  75. int nb_ledstip_2 = 10; // nombre de leds sur la seconde partie du ruban de led
  76. int nb_ledstip_3 = 10; // nombre de leds sur la troisième partie du ruban de led
  77.  
  78. int NUMPIXELS = nb_ledstip_1 + nb_ledstip_2 + nb_ledstip_3 ;// nombre total de leds
  79.  
  80. int led_array[6]={0,nb_ledstip_1,nb_ledstip_1+1,nb_ledstip_2+nb_ledstip_1,nb_ledstip_2+nb_ledstip_1+1,NUMPIXELS};
  81.  
  82. int pin_led_strip = 10; //broche du ruban de led
  83.  
  84. Adafruit_NeoPixel strip0 = Adafruit_NeoPixel(NUMPIXELS, pin_led_strip, NEO_GRB + NEO_KHZ800);
  85.  
  86.  
  87.  
  88.  
  89. // touch includes
  90. #include <MPR121.h>
  91. #include <Wire.h>
  92. #define MPR121_ADDR 0x5C
  93. #define MPR121_INT 4
  94.  
  95. // mp3 includes
  96. #include <SPI.h>
  97. #include <SdFat.h>
  98. #include <FreeStack.h>
  99. #include <SFEMP3Shield.h>
  100.  
  101. // mp3 variables
  102. SFEMP3Shield MP3player;
  103. byte result;
  104. int lastPlayed = 0;
  105.  
  106. // mp3 behaviour defines
  107. #define REPLAY_MODE FALSE
  108. // #define REPLAY_MODE TRUE
  109. /*TRUE=toucher à plusieurs reprises rejoue la piste à chaque fois depuis le début.
  110. FALSE=appuyez plusieurs fois arrêtera la piste si elle est déjà jouer, ou jouer depuis le début s'il ne l'est pas.*/
  111.  
  112. // touch behaviour definitions
  113. #define firstPin 0
  114. #define lastPin 11
  115.  
  116. // sd card instantiation
  117. SdFat sd;
  118.  
  119.  
  120. void setup() {
  121. Serial.begin(57600);
  122. Serial.println("Bare Conductive Touch MP3 player");
  123.  
  124. if (!sd.begin(SD_SEL, SPI_HALF_SPEED)) sd.initErrorHalt();
  125.  
  126. if (!MPR121.begin(MPR121_ADDR)) Serial.println("error setting up MPR121");
  127. MPR121.setInterruptPin(MPR121_INT);
  128.  
  129. // valeur modifiable et valable pour l'ensemble des électrodes - Valeur initiale 40/20
  130. MPR121.setTouchThreshold(20);
  131. MPR121.setReleaseThreshold(10);
  132. /* il est également possible de définir certaines électrodes
  133. comme contact et les autres comme proximité.
  134. par exemple :
  135. Régler uniquement l'électrode 11 avec les valeurs de seuil respectives.
  136. MPR121.setTouchThreshold (11, 20);
  137. MPR121.setReleaseThreshold (11, 10);
  138. */
  139.  
  140. MPR121.setFFI( FFI_10 ); //modifier le nombre d'échantillons mesurés. Remplacer le filtre par l'une des valeurs suivantes FFI_6, FFI_10, FFI_18, FFI_34
  141. MPR121.setSFI( SFI_10 );
  142.  
  143.  
  144.  
  145. MPR121.setGlobalCDT( CDT_4US ); // reasonable for larger capacitances at the end of long cables
  146. MPR121.autoSetElectrodeCDC(); // autoset all electrode settings
  147.  
  148. // digitalWrite(LED_BUILTIN, LOW);
  149.  
  150. result = MP3player.begin();
  151. MP3player.setVolume(10, 10);
  152.  
  153. if (result != 0) {
  154. Serial.print("Error code: ");
  155. Serial.print(result);
  156. Serial.println(" when trying to start MP3 player");
  157. }
  158.  
  159. ///////////////////////////
  160.  
  161.  
  162. //Gestion des rubans LED : strip0, strip1, strip2
  163. strip0.begin();
  164. strip0.show(); // initialize all pixels to "off"
  165.  
  166.  
  167.  
  168. //TEST des 3 rubans lED dans le setup
  169. colorWipe(strip0.Color(0, 255, 0), 50,0); // premiere bande
  170. colorWipe(strip0.Color(0, 255, 0), 50,1); // seconde bande
  171. colorWipe(strip0.Color(0, 255, 0), 50,2); // troisieme bande
  172.  
  173.  
  174. }
  175.  
  176. void loop() {
  177. readTouchInputs();
  178. }
  179.  
  180. void colorWipe(uint32_t c, uint8_t wait,int num_strip) {
  181. for(uint16_t i=led_array[num_strip*2]; i<led_array[num_strip*2+1]; i++) {
  182. strip0.setPixelColor(i, c);
  183. strip0.show();
  184. delay(wait);
  185. }
  186. }
  187.  
  188. void theaterChaseRainbow(uint8_t wait) {
  189. for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
  190. for (int q=0; q < 3; q++) {
  191. for (uint16_t i=0; i < strip0.numPixels(); i=i+3) {
  192. strip0.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
  193. }
  194. strip0.show();
  195.  
  196. delay(wait);
  197.  
  198. for (uint16_t i=0; i < strip0.numPixels(); i=i+3) {
  199. strip0.setPixelColor(i+q, 0); //turn every third pixel off
  200. }
  201. }
  202. }
  203. }
  204.  
  205. // Input a value 0 to 255 to get a color value.
  206. // The colours are a transition r - g - b - back to r.
  207. uint32_t Wheel(byte WheelPos) {
  208. WheelPos = 255 - WheelPos;
  209. if(WheelPos < 85) {
  210. return strip0.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  211. }
  212. if(WheelPos < 170) {
  213. WheelPos -= 85;
  214. return strip0.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  215. }
  216. WheelPos -= 170;
  217. return strip0.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  218. }
  219.  
  220.  
  221.  
  222. void readTouchInputs() {
  223. if (MPR121.touchStatusChanged()) {
  224.  
  225. MPR121.updateTouchData();
  226.  
  227. // only make an action if we have one or fewer pins touched
  228. // ignore multiple touches
  229.  
  230. if (MPR121.getNumTouches() <= 1) {
  231. for (int i = 0; i < 12; i++) { // Check which electrodes were pressed
  232. if (MPR121.isNewTouch(i)) {
  233.  
  234. //pin i was just touched
  235. Serial.print("pin ");
  236. Serial.print(i);
  237. Serial.println(" was just touched");
  238. //digitalWrite(LED_BUILTIN, HIGH);
  239.  
  240. if (i <= lastPin && i >= firstPin) {
  241. if (MP3player.isPlaying()) {
  242. if (lastPlayed == i && !REPLAY_MODE) {
  243. // if we're already playing the requested track, stop it
  244. // (but only if we're not in REPLAY_MODE)
  245. MP3player.stopTrack();
  246. Serial.print("stopping track ");
  247. Serial.println(i - firstPin);
  248. // switch off the relevant LED output
  249. // digitalWrite(ledPins[lastPlayed], LOW);
  250.  
  251. } else {
  252. // if we're already playing a different track (or we're in
  253. // REPLAY_MODE), stop and play the newly requested one
  254. MP3player.stopTrack();
  255. MP3player.playTrack(i - firstPin);
  256. Serial.print("playing track ");
  257. Serial.println(i - firstPin);
  258.  
  259. // switch off the relevant LED output
  260. //digitalWrite(ledPins[lastPlayed], LOW);
  261.  
  262. // switch on the new LED output
  263. // digitalWrite(ledPins[i], HIGH);
  264.  
  265. // don't forget to update lastPlayed - without it we don't
  266. // have a history
  267. lastPlayed = i;
  268. }
  269. } else {
  270. // if we're playing nothing, play the requested track
  271. // and update lastplayed
  272. MP3player.playTrack(i - firstPin);
  273. Serial.print("playing track ");
  274. Serial.println(i - firstPin);
  275.  
  276. // switch on the new LED output
  277. // digitalWrite(ledPins[i], HIGH);
  278.  
  279. lastPlayed = i;
  280. }
  281. }
  282. } else {
  283. if (MPR121.isNewRelease(i)) {
  284. Serial.print("pin ");
  285. // Serial.print(i);
  286. Serial.println(" is no longer being touched");
  287. //digitalWrite(LED_BUILTIN, LOW);
  288. // digitalWrite(ledPins[lastPlayed], LOW);
  289. }
  290. }
  291. }
  292. }
  293. }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement