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.

223 lines
5.7KB

  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. int mode;
  45. int count=1;
  46. int a1=0, a2=0, a3=0;
  47. bool anybutton=false;
  48. bool sdcardinit=true;
  49. bool playsamples=false;
  50. void setup() {
  51. mode = EEPROM.read(400);
  52. AudioMemory(20);
  53. pinMode(0, INPUT_PULLUP);
  54. pinMode(1, INPUT_PULLUP);
  55. pinMode(2, INPUT_PULLUP);
  56. Serial.begin(115200);
  57. SPI.setMOSI(7);
  58. SPI.setSCK(14);
  59. sgtl5000_1.enable();
  60. sgtl5000_1.volume(0.5);
  61. sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  62. sgtl5000_1.micGain(36);
  63. mixer1.gain(0, 0);
  64. mixer1.gain(1, 0);
  65. mixer1.gain(2, 0);
  66. mixer1.gain(3, 0.4);
  67. mixer2.gain(0, 0);
  68. mixer2.gain(1, 0);
  69. mixer2.gain(2, 0);
  70. mixer2.gain(3, 0.4);
  71. waveform1.begin(WAVEFORM_SINE);
  72. delay(1000);
  73. button0.update();
  74. button1.update();
  75. button2.update();
  76. a1 = analogRead(A1);
  77. a2 = analogRead(A2);
  78. a3 = analogRead(A3);
  79. }
  80. void update() {
  81. static int state=0;
  82. button0.update();
  83. button1.update();
  84. button2.update();
  85. anybutton = false;
  86. if (button0.fallingEdge()) {
  87. anybutton = true;
  88. Serial.println("Button (pin 0) Press");
  89. if (playsamples) sample1.play(AudioSampleButton1);
  90. }
  91. if (button1.fallingEdge()) {
  92. anybutton = true;
  93. Serial.println("Button (pin 1) Press");
  94. if (playsamples) sample1.play(AudioSampleButton2);
  95. }
  96. if (button2.fallingEdge()) {
  97. anybutton = true;
  98. Serial.println("Button (pin 2) Press");
  99. if (playsamples) sample1.play(AudioSampleButton3);
  100. }
  101. if (button0.risingEdge()) {
  102. Serial.println("Button (pin 0) Release");
  103. }
  104. if (button1.risingEdge()) {
  105. Serial.println("Button (pin 1) Release");
  106. }
  107. if (button2.risingEdge()) {
  108. Serial.println("Button (pin 2) Release");
  109. }
  110. if (state == 0) {
  111. int a = analogRead(A1);
  112. if (a > a1 + 50 || a < a1 - 50) {
  113. Serial.print("Knob (pin A1) = ");
  114. Serial.println(a);
  115. if (playsamples && !sample1.isPlaying()) sample1.play(AudioSampleKnob1);
  116. a1 = a;
  117. }
  118. state = 1;
  119. } else if (state == 1) {
  120. int a = analogRead(A2);
  121. if (a > a2 + 50 || a < a2 - 50) {
  122. Serial.print("Knob (pin A2) = ");
  123. Serial.println(a);
  124. if (playsamples && !sample1.isPlaying()) sample1.play(AudioSampleKnob2);
  125. a2 = a;
  126. }
  127. state = 2;
  128. } else {
  129. int a = analogRead(A3);
  130. if (a > a3 + 50 || a < a3 - 50) {
  131. Serial.print("Knob (pin A3) = ");
  132. Serial.println(a);
  133. if (playsamples && !sample1.isPlaying()) sample1.play(AudioSampleKnob3);
  134. a3 = a;
  135. }
  136. state = 0;
  137. }
  138. }
  139. elapsedMillis msec=0;
  140. void loop() {
  141. update();
  142. // Test microphone
  143. if (mode == 255) {
  144. playsamples = true;
  145. mixer1.gain(0, 1.0);
  146. mixer2.gain(0, 1.0);
  147. if (anybutton) {
  148. mixer1.gain(0, 0);
  149. mixer2.gain(0, 0);
  150. if (sdcardinit) {
  151. if (!(SD.begin(10))) {
  152. while (1) {
  153. Serial.println("Unable to access the SD card");
  154. if (playsamples) sample1.play(AudioSampleNosdcard);
  155. delay(3500);
  156. }
  157. }
  158. sdcardinit = false;
  159. }
  160. mode = 123;
  161. }
  162. // Play WAV file (test SD card, sound quality)
  163. } else if (mode == 123) {
  164. mixer1.gain(1, 0.75);
  165. mixer2.gain(1, 0.75);
  166. if (playSdWav1.isPlaying() == false) {
  167. Serial.println("Start playing");
  168. playSdWav1.play("SDTEST2.WAV");
  169. delay(10); // wait for library to parse WAV info
  170. }
  171. if (anybutton) {
  172. playSdWav1.stop();
  173. mixer1.gain(1, 0);
  174. mixer2.gain(1, 0);
  175. mode = 45;
  176. EEPROM.write(400, mode);
  177. }
  178. // Beeping (test buttons & knobs)
  179. } else {
  180. mixer1.gain(2, 1.0);
  181. mixer2.gain(2, 1.0);
  182. if (mode == 45) {
  183. Serial.print("Beep #");
  184. Serial.println(count);
  185. count = count + 1;
  186. waveform1.frequency(440);
  187. waveform1.amplitude(0.35);
  188. msec = 0;
  189. mode = 46;
  190. } else if (mode == 46) {
  191. if (msec > 250) {
  192. waveform1.amplitude(0);
  193. msec = 0;
  194. mode = 47;
  195. }
  196. } else {
  197. if (msec > 1750) {
  198. mode = 45;
  199. }
  200. }
  201. if (button0.read() == LOW && button1.read() == LOW && button2.read() == LOW) {
  202. mixer1.gain(2, 0);
  203. mixer2.gain(2, 0);
  204. mode = 255;
  205. }
  206. }
  207. }