这篇文章来源于DevicePlus.com英语网站的翻译稿。Arduino平台的设计非常灵活,使用Uno等单个开发板就可以创建出无数项目。但是,专为执行特定任务而设计的专用板对某些项目来说尤其有用。为了展示这一点,我们将用LilyPad MP3(一种变体Arduino开发板)来构建一个音板。

变体开发板的工作原理

开源软件平台允许开发人员检查和修改他们的代码。同样,像Arduino这样的开源硬件平台允许第三方构建他们自己版本的兼容板。Arduino板的LilyPad系列就是这样的一种变体。该板专为电子纺织品设计,可以缝制到织物项目中。这使其成为服装和智能服装的理想选择。
lilypad1.jpg
LilyPad MP3是LilyPad系列的另一个子变体,包括一个内置SD卡读卡器、一个立体声音频放大器和3.5毫米耳机插孔,以及五个预编程过的触发器输入。无需任何编码设置,您最多可以连接五个触发器来播放预先录制的声音文件。如果您曾在迪士尼见过可以播放一组预先编写好的语句的星球大战冲锋队 ,那么可以告诉您LilyPad MP3的功能与其非常相似。
虽然LilyPad MP3可以像任何Arduino板那样编程,但是该项目的优点是您可以在不修改任何现有代码的情况下对音板进行设置。它可以向您证明为您的项目尽早选择合适的开发板有多么值得。使用普通的Arduino Uno需要有一个外部扩展板才能具有与LilyPad MP3相同的功能,而且体积会更大。由于大多数Arduino板都可以与大部分草图兼容,因此找到满足您需求的外形尺寸可以为您省去大量的时间和麻烦。


所需组件

对于本项目,我们将演示LilyPad MP3如何在面包板上工作的,其实就是让线路能够正常工作。首先,您需要一个LilyPad MP3。
您还需要1-5个按钮(只需要一个,但是LilyPad最多支持5个)、一个扬声器(或耳机)和3.5毫米电缆。
由于LilyPad旨在用于服装,您可以选择使用一些线和针将设备缝制到外套、衣服或任何其他您想要装上音板的地方。使用导电布线可以让您将电线直接编织到衣服的面料中。
lilypad2.jpg LilyPad MP3

代码


如上所述,LilyPad MP3上默认触发器草图的代码预装在了每块板上。您不需要上传此文件,但查看这些代码来了解工作原理会很有帮助。
  1. // “Trigger” example sketch for Lilypad MP3 Player
  2. // Mike Grusin, SparkFun Electronics
  3. // http://www.sparkfun.com
  4. // This sketch (which is preloaded onto the board by default),
  5. // will play a specific audio file when one of the five trigger
  6. // inputs (labeled T1 – T5) is momentarily grounded.
  7. // You can place up to five audio files on the micro-SD card.
  8. // These files should have the desired trigger number (1 to 5)
  9. // as the first character in the filename. The rest of the
  10. // filename can be anything you like. Long file names will work,
  11. // but will be translated into short 8.3 names. We recommend using
  12. // 8.3 format names without spaces, but the following characters
  13. // are OK: .$%’-_@~`!(){}^#&. The VS1053 can play a variety of
  14. // audio formats, see the datasheet for information.
  15. // By default, a new trigger will interrupt a playing file, except
  16. // itself. (In other words, a new trigger won’t restart an
  17. // already-playing file). You can easily change this behavior by
  18. // modifying the global variables “interrupt” and “interruptself”
  19. // below.
  20. // This sketch can output serial debugging information if desired
  21. // by changing the global variable “debugging” to true. Note that
  22. // this will take away trigger inputs 4 and 5, which are shared
  23. // with the TX and RX lines. You can keep these lines connected to
  24. // trigger switches and use the serial port as long as the triggers
  25. // are normally open (not grounded) and remain ungrounded while the
  26. // serial port is in use.
  27. // Uses the SdFat library by William Greiman, which is supplied
  28. // with this archive, or download from http://code.google.com/p/sdfatlib/
  29. // Uses the SFEMP3Shield library by Bill Porter, which is supplied
  30. // with this archive, or download from http://www.billporter.info/
  31. // License:
  32. // We use the “beerware” license for our firmware. You can do
  33. // ANYTHING you want with this code. If you like it, and we meet
  34. // someday, you can, but are under no obligation to, buy me a
  35. // (root) beer in return.
  36. // Have fun!
  37. // -your friends at SparkFun
  38. // Revision history:
  39. // 1.0 initial release MDG 2012/11/01
  40. // We’ll need a few libraries to access all this hardware!
  41. #include // To talk to the SD card and MP3 chip
  42. #include // SD card file system
  43. #include // MP3 decoder chip
  44. // Constants for the trigger input pins, which we’ll place
  45. // in an array for convenience:
  46. const int TRIG1 = A0;
  47. const int TRIG2 = A4;
  48. const int TRIG3 = A5;
  49. const int TRIG4 = 1;
  50. const int TRIG5 = 0;
  51. int trigger[5] = {TRIG1, TRIG2, TRIG3, TRIG4, TRIG5};
  52. // And a few outputs we’ll be using:
  53. const int ROT_LEDR = 10; // Red LED in rotary encoder (optional)
  54. const int EN_GPIO1 = A2; // Amp enable + MIDI/MP3 mode select
  55. const int SD_CS = 9; // Chip Select for SD card
  56. // Create library objects:
  57. SFEMP3Shield MP3player;
  58. SdFat sd;
  59. // Set debugging = true if you’d like status messages sent
  60. // to the serial port. Note that this will take over trigger
  61. // inputs 4 and 5. (You can leave triggers connected to 4 and 5
  62. // and still use the serial port, as long as you’re careful to
  63. // NOT ground the triggers while you’re using the serial port).
  64. boolean debugging = false;
  65. // Set interrupt = false if you would like a triggered file to
  66. // play all the way to the end. If this is set to true, new
  67. // triggers will stop the playing file and start a new one.
  68. boolean interrupt = true;
  69. // Set interruptself = true if you want the above rule to also
  70. // apply to the same trigger. In other words, if interrupt = true
  71. // and interruptself = false, subsequent triggers on the same
  72. // file will NOT start the file over. However, a different trigger
  73. // WILL stop the original file and start a new one.
  74. boolean interruptself = false;
  75. // We’ll store the five filenames as arrays of characters.
  76. // “Short” (8.3) filenames are used, followed by a null character.
  77. char filename[5][13];
  78. void setup()
  79. {
  80. int x, index;
  81. SdFile file;
  82. byte result;
  83. char tempfilename[13];
  84. // Set the five trigger pins as inputs, and turn on the
  85. // internal pullup resistors:
  86. for (x = 0; x <= 4; x++) { pinMode(trigger[x], INPUT); digitalWrite(trigger[x], HIGH); } // If serial port debugging is inconvenient, you can connect // a LED to the red channel of the rotary encoder to blink // startup error codes: pinMode(ROT_LEDR, OUTPUT); digitalWrite(ROT_LEDR, HIGH); // HIGH = off // The board uses a single I/O pin to select the // mode the MP3 chip will start up in (MP3 or MIDI), // and to enable/disable the amplifier chip: pinMode(EN_GPIO1, OUTPUT); digitalWrite(EN_GPIO1, LOW); // MP3 mode / amp off // If debugging is true, initialize the serial port: // (The ‘F’ stores constant strings in flash memory to save RAM) if (debugging) { Serial.begin(9600); Serial.println(F(“Lilypad MP3 Player trigger sketch”)); } // Initialize the SD card; SS = pin 9, half speed at first if (debugging) Serial.print(F(“initialize SD card… “)); result = sd.begin(SD_CS, SPI_HALF_SPEED); // 1 for success if (result != 1) // Problem initializing the SD card { if (debugging) Serial.print(F(“error, halting”)); errorBlink(1); // Halt forever, blink LED if present. } else if (debugging) Serial.println(F(“success!”)); // Start up the MP3 library if (debugging) Serial.print(F(“initialize MP3 chip… “)); result = MP3player.begin(); // 0 or 6 for success // Check the result, see the library readme for error codes. if ((result != 0) && (result != 6)) // Problem starting up { if (debugging) { Serial.print(F(“error code “)); Serial.print(result); Serial.print(F(“, halting.”)); } errorBlink(result); // Halt forever, blink red LED if present. } else if (debugging) Serial.println(F(“success!”)); // Now we’ll access the SD card to look for any (audio) files // starting with the characters ‘1’ to ‘5’: if (debugging) Serial.println(F(“reading root directory”)); // Start at the first file in root and step through all of them: sd.chdir(“/”, true); while (file.openNext(sd.vwd(), O_READ)) { // get filename file.getFilename(tempfilename); // Does the filename start with char ‘1’ through ‘5’? if (tempfilename[0] >= ‘1’ && tempfilename[0] <= ‘5’)
  87. {
  88. // Yes! subtract char ‘1’ to get an index of 0 through 4.
  89. index = tempfilename[0] – ‘1’;
  90. // Copy the data to our filename array.
  91. strcpy(filename[index], tempfilename);
  92. if (debugging) // Print out file number and name
  93. {
  94. Serial.print(F(“found a file with a leading “));
  95. Serial.print(index + 1);
  96. Serial.print(F(“: “));
  97. Serial.println(filename[index]);
  98. }
  99. }
  100. else if (debugging)
  101. {
  102. Serial.print(F(“found a file w/o a leading number: “));
  103. Serial.println(tempfilename);
  104. }
  105. file.close();
  106. }
  107. if (debugging)
  108. Serial.println(F(“done reading root directory”));
  109. if (debugging) // List all the files we saved:
  110. {
  111. for(x = 0; x <= 4; x++)
  112. {
  113. Serial.print(F(“trigger “));
  114. Serial.print(x + 1);
  115. Serial.print(F(“: “));
  116. Serial.println(filename[x]);
  117. }
  118. }
  119. // Set the VS1053 volume. 0 is loudest, 255 is lowest (off):
  120. MP3player.setVolume(10, 10);
  121. // Turn on the amplifier chip:
  122. digitalWrite(EN_GPIO1, HIGH);
  123. delay(2);
  124. }
  125. void loop()
  126. {
  127. int t; // current trigger
  128. static int last_t; // previous (playing) trigger
  129. int x;
  130. byte result;
  131. // Step through the trigger inputs, looking for LOW signals.
  132. // The internal pullup resistors will keep them HIGH when
  133. // there is no connection to the input.
  134. // If serial debugging is on, only check triggers 1-3,
  135. // otherwise check triggers 1-5.
  136. for(t = 1; t <= (debugging ? 3 : 5); t++)
  137. {
  138. // The trigger pins are stored in the inputs[] array.
  139. // Read the pin and check if it is LOW (triggered).
  140. if (digitalRead(trigger[t – 1]) == LOW)
  141. {
  142. // Wait for trigger to return high for a solid 50ms
  143. // (necessary to avoid switch bounce on T2 and T3
  144. // since we need those free for I2C control of the
  145. // amplifier)
  146. x = 0;
  147. while(x < 50)
  148. {
  149. if (digitalRead(trigger[t – 1]) == HIGH)
  150. x++;
  151. else
  152. x = 0;
  153. delay(1);
  154. }
  155. if (debugging)
  156. {
  157. Serial.print(F(“got trigger “));
  158. Serial.println(t);
  159. }
  160. // Do we have a valid filename for this trigger?
  161. // (Invalid filenames will have 0 as the first character)
  162. if (filename[t – 1][0] == 0)
  163. {
  164. if (debugging)
  165. Serial.println(F(“no file with that number”));
  166. }
  167. else // We do have a filename for this trigger!
  168. {
  169. // If a file is already playing, and we’ve chosen to
  170. // allow playback to be interrupted by a new trigger,
  171. // stop the playback before playing the new file.
  172. if (interrupt && MP3player.isPlaying() && ((t != last_t) || interruptself))
  173. {
  174. if (debugging)
  175. Serial.println(F(“stopping playback”));
  176. MP3player.stopTrack();
  177. }
  178. // Play the filename associated with the trigger number.
  179. // (If a file is already playing, this command will fail
  180. // with error #2).
  181. result = MP3player.playMP3(filename[t – 1]);
  182. if (result == 0) last_t = t; // Save playing trigger
  183. if(debugging)
  184. {
  185. if(result != 0)
  186. {
  187. Serial.print(F(“error “));
  188. Serial.print(result);
  189. Serial.print(F(” when trying to play track “));
  190. }
  191. else
  192. {
  193. Serial.print(F(“playing “));
  194. }
  195. Serial.println(filename[t – 1]);
  196. }
  197. }
  198. }
  199. }
  200. }
  201. void errorBlink(int blinks)
  202. {
  203. // The following function will blink the red LED in the rotary
  204. // encoder (optional) a given number of times and repeat forever.
  205. // This is so you can see any startup error codes without having
  206. // to use the serial monitor window.
  207. int x;
  208. while(true) // Loop forever
  209. {
  210. for (x = 0; x < blinks; x++) // Blink the given number of times
  211. {
  212. digitalWrite(ROT_LEDR, LOW); // Turn LED ON
  213. delay(250);
  214. digitalWrite(ROT_LEDR, HIGH); // Turn LED OFF
  215. delay(250);
  216. }
  217. delay(1500); // Longer pause between blink-groups
  218. }
  219. }
虽然逐行看下来代码实在太多了(而且很多都在本项目中用不到),但是有几个关键点值得强调。首先,有一些函数是Bill Porter的MP3播放器库的一部分。这些函数通过VS1053B解码芯片控制音频文件。这些独特的函数包括:
setVolume(): 该函数可以调节输出音量的级别。
isPlaying(): 该函数可用于确定当前是否正在播放MP3(可用于中断或停止播放)。
stopTrack(): 可以停止当前正在播放的曲目。
playMP3(): 该函数用于播放SD卡中的文件。默认草图设置为播放与输入所连接触发器匹配的文件。
要在LilyPad上设置音轨,您最多可以将五个音频文件添加到微SD卡中。尽管LilyPad的名称是MP3,但是它支持多种音频文件格式。您可以在此处找到所支持的格式列表。您可以随意命名文件,只要文件名以1到5之间的数字开头即可。
默认草图将播放与激活的触发器对应的音频文件。触发器1(在LilyPad上标记为T1)将激活以1开头的音频文件。触发器2将播放以2开头的文件,以此类推。只要您正确命名音频文件,就不必对代码进行任何处理。

接线

将所有组件连接到LilyPad很简单。将微SD卡插入微SD卡槽,然后将扬声器或一副耳机插入耳机插孔。如果您想将扬声器直接连接到开发板,LilyPad还具有用于左右扬声器的正负极触点。
接下来,将按钮连接到您要使用的每个触发器。按钮的一侧连接到GND,而另一侧连接到您要激活的触发器触点。LilyPad上的触发器触点中有一个大圆形触点和一个紧邻大触点的较小触点。您可以将电线焊接到较小的触点,或将电线缠绕在较大的触点上。
按下按钮,此时应该会触发音频文件的播放。确认接线正常后,您就可以尝试使用导电布线将接线缝入织物中了。

来源:ROHM