Waveforms.ino 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Waveform Example - Create 2 waveforms with adjustable
  2. // frequency and phase
  3. //
  4. // This example is meant to be used with 3 buttons (pin 0,
  5. // 1, 2) and 2 knobs (pins 16/A2, 17/A3), which are present
  6. // on the audio tutorial kit.
  7. // https://www.pjrc.com/store/audio_tutorial_kit.html
  8. //
  9. // Use an oscilloscope to view the 2 waveforms.
  10. //
  11. // Button0 changes the waveform shape
  12. //
  13. // Knob A2 changes the frequency of both waveforms
  14. // You should see both waveforms "stretch" as you turn
  15. //
  16. // Knob A3 changes the phase of waveform #1
  17. // You should see the waveform shift horizontally
  18. //
  19. // This example code is in the public domain.
  20. #include <Audio.h>
  21. #include <Wire.h>
  22. #include <SPI.h>
  23. #include <SD.h>
  24. #include <SerialFlash.h>
  25. #include <Bounce.h>
  26. AudioSynthWaveform waveform1; //xy=171,84
  27. AudioSynthWaveform waveform2; //xy=178,148
  28. AudioOutputI2S i2s1; //xy=360,98
  29. AudioOutputAnalogStereo dacs1; //xy=372,173
  30. AudioConnection patchCord1(waveform1, 0, i2s1, 0);
  31. AudioConnection patchCord2(waveform1, 0, dacs1, 0);
  32. AudioConnection patchCord3(waveform2, 0, i2s1, 1);
  33. AudioConnection patchCord4(waveform2, 0, dacs1, 1);
  34. AudioControlSGTL5000 sgtl5000_1; //xy=239,232
  35. Bounce button0 = Bounce(0, 15);
  36. Bounce button1 = Bounce(1, 15);
  37. Bounce button2 = Bounce(2, 15);
  38. int current_waveform=0;
  39. extern const int16_t myWaveform[256]; // defined in myWaveform.ino
  40. void setup() {
  41. Serial.begin(9600);
  42. pinMode(0, INPUT_PULLUP);
  43. pinMode(1, INPUT_PULLUP);
  44. pinMode(2, INPUT_PULLUP);
  45. // Audio connections require memory to work. For more
  46. // detailed information, see the MemoryAndCpuUsage example
  47. AudioMemory(10);
  48. // Comment these out if not using the audio adaptor board.
  49. // This may wait forever if the SDA & SCL pins lack
  50. // pullup resistors
  51. sgtl5000_1.enable();
  52. sgtl5000_1.volume(0.8); // caution: very loud - use oscilloscope only!
  53. // Confirgure both to use "myWaveform" for WAVEFORM_ARBITRARY
  54. waveform1.arbitraryWaveform(myWaveform, 172.0);
  55. waveform2.arbitraryWaveform(myWaveform, 172.0);
  56. // configure both waveforms for 440 Hz and maximum amplitude
  57. waveform1.frequency(440);
  58. waveform2.frequency(440);
  59. waveform1.amplitude(1.0);
  60. waveform2.amplitude(1.0);
  61. current_waveform = WAVEFORM_TRIANGLE;
  62. waveform1.begin(current_waveform);
  63. }
  64. void loop() {
  65. // Read the buttons and knobs, scale knobs to 0-1.0
  66. button0.update();
  67. button1.update();
  68. button2.update();
  69. float knob_A2 = (float)analogRead(A2) / 1023.0;
  70. float knob_A3 = (float)analogRead(A3) / 1023.0;
  71. AudioNoInterrupts();
  72. // use Knob A2 to adjust the frequency of both waveforms
  73. waveform1.frequency(100.0 + knob_A2 * 900.0);
  74. waveform2.frequency(100.0 + knob_A2 * 900.0);
  75. // use Knob A3 to adjust the phase of only waveform #1
  76. waveform1.phase(knob_A3 * 360.0);
  77. AudioInterrupts();
  78. // Button 0 changes the waveform type
  79. if (button0.fallingEdge()) {
  80. switch (current_waveform) {
  81. case WAVEFORM_SINE:
  82. current_waveform = WAVEFORM_SAWTOOTH;
  83. Serial.println("Sawtooth");
  84. break;
  85. case WAVEFORM_SAWTOOTH:
  86. current_waveform = WAVEFORM_SAWTOOTH_REVERSE;
  87. Serial.println("Reverse Sawtooth");
  88. break;
  89. case WAVEFORM_SAWTOOTH_REVERSE:
  90. current_waveform = WAVEFORM_SQUARE;
  91. Serial.println("Square");
  92. break;
  93. case WAVEFORM_SQUARE:
  94. current_waveform = WAVEFORM_TRIANGLE;
  95. Serial.println("Triangle");
  96. break;
  97. case WAVEFORM_TRIANGLE:
  98. current_waveform = WAVEFORM_TRIANGLE_VARIABLE;
  99. Serial.println("Variable Triangle");
  100. break;
  101. case WAVEFORM_TRIANGLE_VARIABLE:
  102. current_waveform = WAVEFORM_ARBITRARY;
  103. Serial.println("Arbitary Waveform");
  104. break;
  105. case WAVEFORM_ARBITRARY:
  106. current_waveform = WAVEFORM_PULSE;
  107. Serial.println("Pulse");
  108. break;
  109. case WAVEFORM_PULSE:
  110. current_waveform = WAVEFORM_SAMPLE_HOLD;
  111. Serial.println("Sample & Hold");
  112. break;
  113. case WAVEFORM_SAMPLE_HOLD:
  114. current_waveform = WAVEFORM_SINE;
  115. Serial.println("Sine");
  116. break;
  117. }
  118. AudioNoInterrupts();
  119. waveform1.begin(current_waveform);
  120. waveform2.begin(WAVEFORM_SINE);
  121. AudioInterrupts();
  122. }
  123. }