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.

пре 6 година
пре 6 година
пре 6 година
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* Play a flute sound when a button is pressed.
  2. Connect a pushbutton to pin 1 and pots to pins A2 & A3.
  3. The audio tutorial kit is the intended hardware:
  4. https://www.pjrc.com/store/audio_tutorial_kit.html
  5. Without pots connected, this program will play a very
  6. strange sound due to rapid random fluctuation of the
  7. pitch and volume!
  8. Requires Teensy 3.2 or higher.
  9. Requires Audio Shield: https://www.pjrc.com/store/teensy3_audio.html
  10. */
  11. #include <Bounce.h>
  12. #include <Audio.h>
  13. #include <Wire.h>
  14. #include <SPI.h>
  15. #include <SD.h>
  16. #include <SerialFlash.h>
  17. #include "Flute_100kbyte_samples.h"
  18. AudioSynthWavetable wavetable;
  19. AudioOutputI2S i2s1;
  20. AudioMixer4 mixer;
  21. AudioConnection patchCord1(wavetable, 0, mixer, 0);
  22. AudioConnection patchCord2(mixer, 0, i2s1, 0);
  23. AudioConnection patchCord3(mixer, 0, i2s1, 1);
  24. AudioControlSGTL5000 sgtl5000_1;
  25. // Bounce objects to read pushbuttons
  26. Bounce button1 = Bounce(1, 15); // 15 ms debounce time
  27. void setup() {
  28. Serial.begin(115200);
  29. pinMode(1, INPUT_PULLUP);
  30. AudioMemory(20);
  31. sgtl5000_1.enable();
  32. sgtl5000_1.volume(0.8);
  33. mixer.gain(0, 0.7);
  34. wavetable.setInstrument(Flute_100kbyte);
  35. wavetable.amplitude(1);
  36. }
  37. bool playing = false;
  38. void loop() {
  39. // Update all the button objects
  40. button1.update();
  41. //Read knob values
  42. int knob1 = analogRead(A3);
  43. int knob2 = analogRead(A2);
  44. //Get frequency and gain from knobs (Flute range is 261 to 2100 Hz)
  45. float freq = 261.0 + (float)knob1/1023.0 * (2100.0 - 261.0);
  46. float gain = (float)knob2/1023.0;
  47. //Set a low-limit to the gain
  48. if (gain < .05) gain = .05;
  49. if (button1.fallingEdge()) {
  50. if (playing) {
  51. playing = false;
  52. wavetable.stop();
  53. }
  54. else {
  55. playing = true;
  56. wavetable.playFrequency(freq);
  57. wavetable.amplitude(gain);
  58. }
  59. }
  60. wavetable.amplitude(gain);
  61. wavetable.setFrequency(freq);
  62. }