You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
6.2KB

  1. // Audio Tutorial Kit Tester
  2. //
  3. // http://www.pjrc.com/store/audio_tutorial_kit.html
  4. //
  5. // Easily test all the tutorial hardware by only listening and pressing buttons.
  6. // 1: listen for microphone (any button press moves to #2)
  7. // 2: listen for SD card playing (any button press moves to #2)
  8. // 3: test 3 buttons and 3 knobs (press all 3 buttons to go back to step #1)
  9. //
  10. // After test is completed, EEPROM storage is used to remember the hardware is good
  11. // Good hardware will begin at #3, which corresponds to the first tutorial example
  12. #include <Audio.h>
  13. #include <SD.h>
  14. #include <Bounce.h>
  15. #include <EEPROM.h>
  16. #include "AudioSampleButton1.h"
  17. #include "AudioSampleButton2.h"
  18. #include "AudioSampleButton3.h"
  19. #include "AudioSampleKnob1.h"
  20. #include "AudioSampleKnob2.h"
  21. #include "AudioSampleKnob3.h"
  22. #include "AudioSampleNosdcard.h"
  23. AudioInputI2S i2s1;
  24. AudioSynthWaveform waveform1;
  25. AudioPlaySdWav playSdWav1;
  26. AudioPlayMemory sample1;
  27. AudioMixer4 mixer1;
  28. AudioMixer4 mixer2;
  29. AudioOutputI2S i2s2;
  30. AudioConnection patchCord1(i2s1, 0, mixer1, 0);
  31. AudioConnection patchCord2(i2s1, 0, mixer2, 0);
  32. AudioConnection patchCord3(playSdWav1, 0, mixer1, 1);
  33. AudioConnection patchCord4(playSdWav1, 1, mixer2, 1);
  34. AudioConnection patchCord5(waveform1, 0, mixer1, 2);
  35. AudioConnection patchCord6(waveform1, 0, mixer2, 2);
  36. AudioConnection patchCord7(sample1, 0, mixer1, 3);
  37. AudioConnection patchCord8(sample1, 0, mixer2, 3);
  38. AudioConnection patchCord9(mixer1, 0, i2s2, 0);
  39. AudioConnection patchCordA(mixer2, 0, i2s2, 1);
  40. AudioControlSGTL5000 sgtl5000_1;
  41. Bounce button0 = Bounce(0, 15);
  42. Bounce button1 = Bounce(1, 15);
  43. Bounce button2 = Bounce(2, 15);
  44. // Use these with the Teensy Audio Shield
  45. #define SDCARD_CS_PIN 10
  46. #define SDCARD_MOSI_PIN 7
  47. #define SDCARD_SCK_PIN 14
  48. // Use these with the Teensy 3.5 & 3.6 SD card
  49. //#define SDCARD_CS_PIN BUILTIN_SDCARD
  50. //#define SDCARD_MOSI_PIN 11 // not actually used
  51. //#define SDCARD_SCK_PIN 13 // not actually used
  52. // Use these for the SD+Wiz820 or other adaptors
  53. //#define SDCARD_CS_PIN 4
  54. //#define SDCARD_MOSI_PIN 11
  55. //#define SDCARD_SCK_PIN 13
  56. int mode;
  57. int count=1;
  58. int a1=0, a2=0, a3=0;
  59. bool anybutton=false;
  60. bool sdcardinit=true;
  61. bool playsamples=false;
  62. void setup() {
  63. mode = EEPROM.read(400);
  64. AudioMemory(20);
  65. pinMode(0, INPUT_PULLUP);
  66. pinMode(1, INPUT_PULLUP);
  67. pinMode(2, INPUT_PULLUP);
  68. Serial.begin(115200);
  69. SPI.setMOSI(SDCARD_MOSI_PIN);
  70. SPI.setSCK(SDCARD_SCK_PIN);
  71. sgtl5000_1.enable();
  72. sgtl5000_1.volume(0.5);
  73. sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  74. sgtl5000_1.micGain(36);
  75. mixer1.gain(0, 0);
  76. mixer1.gain(1, 0);
  77. mixer1.gain(2, 0);
  78. mixer1.gain(3, 0.4);
  79. mixer2.gain(0, 0);
  80. mixer2.gain(1, 0);
  81. mixer2.gain(2, 0);
  82. mixer2.gain(3, 0.4);
  83. waveform1.begin(WAVEFORM_SINE);
  84. delay(1000);
  85. button0.update();
  86. button1.update();
  87. button2.update();
  88. a1 = analogRead(A1);
  89. a2 = analogRead(A2);
  90. a3 = analogRead(A3);
  91. }
  92. void update() {
  93. static int state=0;
  94. button0.update();
  95. button1.update();
  96. button2.update();
  97. anybutton = false;
  98. if (button0.fallingEdge()) {
  99. anybutton = true;
  100. Serial.println("Button (pin 0) Press");
  101. if (playsamples) sample1.play(AudioSampleButton1);
  102. }
  103. if (button1.fallingEdge()) {
  104. anybutton = true;
  105. Serial.println("Button (pin 1) Press");
  106. if (playsamples) sample1.play(AudioSampleButton2);
  107. }
  108. if (button2.fallingEdge()) {
  109. anybutton = true;
  110. Serial.println("Button (pin 2) Press");
  111. if (playsamples) sample1.play(AudioSampleButton3);
  112. }
  113. if (button0.risingEdge()) {
  114. Serial.println("Button (pin 0) Release");
  115. }
  116. if (button1.risingEdge()) {
  117. Serial.println("Button (pin 1) Release");
  118. }
  119. if (button2.risingEdge()) {
  120. Serial.println("Button (pin 2) Release");
  121. }
  122. if (state == 0) {
  123. int a = analogRead(A1);
  124. if (a > a1 + 50 || a < a1 - 50) {
  125. Serial.print("Knob (pin A1) = ");
  126. Serial.println(a);
  127. if (playsamples && !sample1.isPlaying()) sample1.play(AudioSampleKnob1);
  128. a1 = a;
  129. }
  130. state = 1;
  131. } else if (state == 1) {
  132. int a = analogRead(A2);
  133. if (a > a2 + 50 || a < a2 - 50) {
  134. Serial.print("Knob (pin A2) = ");
  135. Serial.println(a);
  136. if (playsamples && !sample1.isPlaying()) sample1.play(AudioSampleKnob2);
  137. a2 = a;
  138. }
  139. state = 2;
  140. } else {
  141. int a = analogRead(A3);
  142. if (a > a3 + 50 || a < a3 - 50) {
  143. Serial.print("Knob (pin A3) = ");
  144. Serial.println(a);
  145. if (playsamples && !sample1.isPlaying()) sample1.play(AudioSampleKnob3);
  146. a3 = a;
  147. }
  148. state = 0;
  149. }
  150. }
  151. elapsedMillis msec=0;
  152. void loop() {
  153. update();
  154. // Test microphone
  155. if (mode == 255) {
  156. playsamples = true;
  157. mixer1.gain(0, 1.0);
  158. mixer2.gain(0, 1.0);
  159. if (anybutton) {
  160. mixer1.gain(0, 0);
  161. mixer2.gain(0, 0);
  162. if (sdcardinit) {
  163. if (!(SD.begin(SDCARD_CS_PIN))) {
  164. while (1) {
  165. Serial.println("Unable to access the SD card");
  166. if (playsamples) sample1.play(AudioSampleNosdcard);
  167. delay(3500);
  168. }
  169. }
  170. sdcardinit = false;
  171. }
  172. mode = 123;
  173. }
  174. // Play WAV file (test SD card, sound quality)
  175. } else if (mode == 123) {
  176. mixer1.gain(1, 0.75);
  177. mixer2.gain(1, 0.75);
  178. if (playSdWav1.isPlaying() == false) {
  179. Serial.println("Start playing");
  180. playSdWav1.play("SDTEST2.WAV");
  181. delay(10); // wait for library to parse WAV info
  182. }
  183. if (anybutton) {
  184. playSdWav1.stop();
  185. mixer1.gain(1, 0);
  186. mixer2.gain(1, 0);
  187. mode = 45;
  188. EEPROM.write(400, mode);
  189. }
  190. // Beeping (test buttons & knobs)
  191. } else {
  192. mixer1.gain(2, 1.0);
  193. mixer2.gain(2, 1.0);
  194. if (mode == 45) {
  195. Serial.print("Beep #");
  196. Serial.println(count);
  197. count = count + 1;
  198. waveform1.frequency(440);
  199. waveform1.amplitude(0.35);
  200. msec = 0;
  201. mode = 46;
  202. } else if (mode == 46) {
  203. if (msec > 250) {
  204. waveform1.amplitude(0);
  205. msec = 0;
  206. mode = 47;
  207. }
  208. } else {
  209. if (msec > 1750) {
  210. mode = 45;
  211. }
  212. }
  213. if (button0.read() == LOW && button1.read() == LOW && button2.read() == LOW) {
  214. mixer1.gain(2, 0);
  215. mixer2.gain(2, 0);
  216. mode = 255;
  217. }
  218. }
  219. }