Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.79 KB | None | 0 0
  1. #include <MozziGuts.h>
  2. #include <Oscil.h>
  3. #include <LiquidCrystal.h>
  4. #include <mozzi_fixmath.h>
  5. #include <mozzi_midi.h>
  6. #include <RollingAverage.h>
  7. #include <ADSR.h>
  8.  
  9. /*
  10. AUDIO INCLUDES
  11. */
  12.  
  13. #include <tables/sin2048_int8.h>
  14. #include <tables/cos2048_int8.h>
  15. #include <tables/square_no_alias_2048_int8.h>
  16. #include <tables/triangle_hermes_2048_int8.h>
  17.  
  18. /*
  19. DEFINES
  20. */
  21.  
  22. #define CONTROL_RATE 128
  23.  
  24. /*
  25. PIN DECLARATIONS
  26. */
  27.  
  28. // LCD
  29. const int rsPin = 53;
  30. const int ePin = 52;
  31. const int d4Pin = 43;
  32. const int d5Pin = 42;
  33. const int d6Pin = 41;
  34. const int d7Pin = 40;
  35.  
  36. // JOY
  37. const int joyXPin = A8;
  38. const int joyYPin = A9;
  39. const int joySWPin = 3;
  40.  
  41. // LASERS
  42.  
  43. // PHOTORESISTORS
  44. const int photoResistorPin1 = A10;
  45. const int photoResistorPin2 = A11;
  46. const int photoResistorPin3 = A6;
  47. const int photoResistorPin4 = A7;
  48.  
  49. // DISTANCE SENSOR
  50. const int trigPin = 2;
  51. const int echoPin = 4;
  52.  
  53. // TESTING ONLY
  54. // BUTTONS
  55. const int buttonPin1 = 7;
  56. const int buttonPin2 = 8;
  57. const int buttonPin3 = 9;
  58. const int buttonPin4 = 6;
  59.  
  60. // SOUND PARAMETERS
  61.  
  62. int ATTACK = 10;
  63. int DECAY = 128;
  64. int SUSTAIN = 128;
  65. int RELEASE = 0;
  66. int ATTACK_LEVEL = 255;
  67. int DECAY_LEVEL = 200;
  68. int octaveShift = 24 ;
  69. int gain = 255;
  70.  
  71. /*
  72. GLOBALS
  73. */
  74. LiquidCrystal lcd(rsPin, ePin, d4Pin, d5Pin, d6Pin, d7Pin);
  75.  
  76. int xAxis, yAxis, sw;
  77.  
  78. /*
  79. AUDIO
  80. */
  81.  
  82. Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SQUARE_NO_ALIAS_2048_DATA);
  83. Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin2(SQUARE_NO_ALIAS_2048_DATA);
  84. Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin3(SQUARE_NO_ALIAS_2048_DATA);
  85. Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin4(SQUARE_NO_ALIAS_2048_DATA);
  86. Oscil <SQUARE_NO_ALIAS_2048_NUM_CELLS, AUDIO_RATE> aSquare(SQUARE_NO_ALIAS_2048_DATA);
  87. Oscil <COS2048_NUM_CELLS, AUDIO_RATE> aCos(COS2048_DATA);
  88.  
  89. ADSR<CONTROL_RATE, AUDIO_RATE> env;
  90.  
  91. RollingAverage <int, 16> distAvg;
  92.  
  93.  
  94. int midiTable[5][4] = {
  95. 69, 72, 76, 79,
  96. 71, 74, 77, 81,
  97. 72, 76, 79, 83,
  98. 74, 77, 81, 84,
  99. 76, 79, 83, 86
  100. };
  101.  
  102. float freqTable[5][4];
  103.  
  104. /*
  105. * DIST SENSOR
  106. */
  107.  
  108. int duration;
  109. int dist;
  110.  
  111.  
  112.  
  113. byte currentGain = 0;
  114. byte currentGain1 = 0;
  115. byte currentGain2 = 0;
  116. byte currentGain3 = 0;
  117. byte currentGain4 = 0;
  118.  
  119. /*
  120. * BUTTONS
  121. */
  122.  
  123. int buttonValue1;
  124. int buttonValue2;
  125. int buttonValue3;
  126. int buttonValue4;
  127.  
  128. /*
  129. MENU
  130. */
  131. unsigned int cursorPosition = 0;
  132. bool menuChanged = false;
  133. const int noLevels = 7;
  134.  
  135. const char menuStrings[noLevels][16] = {
  136. "Guitarduino",
  137. "Gain",
  138. "Attack",
  139. "Decay",
  140. "Sustain",
  141. "Release",
  142. "Octave"
  143. };
  144.  
  145. int* menuLevels[noLevels] = {
  146. NULL,
  147. &gain,
  148. &ATTACK,
  149. &DECAY,
  150. &SUSTAIN,
  151. &RELEASE,
  152. &octaveShift
  153. };
  154.  
  155. unsigned int mozziStartDelay = 100;
  156. unsigned long lastMenuUpdate;
  157. bool mozziStopped = false;
  158.  
  159.  
  160.  
  161. void printMenu() {
  162. lcd.clear();
  163. lcd.setCursor(0, 0);
  164. lcd.print(menuStrings[cursorPosition]);
  165.  
  166. if (cursorPosition) {
  167. lcd.setCursor(8, 0);
  168.  
  169. lcd.print(*menuLevels[cursorPosition]);
  170. }
  171.  
  172. lcd.setCursor(0, 1);
  173. lcd.print(menuStrings[cursorPosition + 1]);
  174.  
  175. if (cursorPosition + 1) {
  176. lcd.setCursor(8, 1);
  177. lcd.print(*menuLevels[cursorPosition + 1]);
  178. }
  179.  
  180. lcd.setCursor(0, 0);
  181.  
  182. lastMenuUpdate = millis();
  183.  
  184. // env.setTimes(2000, DECAY, SUSTAIN, 255);
  185. }
  186.  
  187. bool xDown = true;
  188. bool yDown = true;
  189.  
  190. const int lowThreshold = 200;
  191. const int highThreshold = 800;
  192.  
  193. void controlMenu() {
  194. if (xAxis > highThreshold && cursorPosition && !xDown) {
  195. (*(menuLevels[cursorPosition]))++;
  196. menuChanged = true;
  197. xDown = true;
  198. }
  199. else if (xAxis < lowThreshold && cursorPosition && !xDown) {
  200. (*(menuLevels[cursorPosition]))--;
  201. menuChanged = true;
  202. xDown = true;
  203. }
  204. else if (xAxis > lowThreshold && xAxis < highThreshold) {
  205. xDown = false;
  206. }
  207.  
  208. if (yAxis > highThreshold && !yDown) {
  209. cursorPosition++;
  210. if (cursorPosition >= noLevels) {
  211. cursorPosition = 0;
  212. }
  213. menuChanged = true;
  214. yDown = true;
  215. }
  216. else if (yAxis < lowThreshold && !yDown) {
  217. cursorPosition--;
  218. if (cursorPosition < 0) {
  219. cursorPosition = noLevels - 1;
  220. }
  221. menuChanged = true;
  222. yDown = true;
  223. }
  224. else if (yAxis > lowThreshold && yAxis < highThreshold) {
  225. yDown = false;
  226. }
  227. }
  228.  
  229. bool noteStopped = true;
  230.  
  231.  
  232.  
  233. void setup() {
  234. startMozzi(CONTROL_RATE);
  235.  
  236. // PINS
  237.  
  238. pinMode(trigPin, OUTPUT);
  239. pinMode(echoPin, INPUT);
  240.  
  241. pinMode(joyXPin, INPUT);
  242. pinMode(joyYPin, INPUT);
  243. pinMode(joySWPin, INPUT_PULLUP);
  244.  
  245. pinMode(photoResistorPin1, INPUT);
  246. pinMode(photoResistorPin2, INPUT);
  247. pinMode(photoResistorPin3, INPUT);
  248. pinMode(photoResistorPin4, INPUT);
  249.  
  250. // TESTING ONLY
  251. // pinMode(buttonPin1, INPUT_PULLUP);
  252. // pinMode(buttonPin2, INPUT_PULLUP);
  253. // pinMode(buttonPin3, INPUT_PULLUP);
  254. // pinMode(buttonPin4, INPUT_PULLUP);
  255.  
  256. // LCD
  257. lcd.begin(16, 2);
  258.  
  259. lcd.cursor();
  260.  
  261. printMenu();
  262.  
  263. aSin.setFreq(440);
  264. aSquare.setFreq(440);
  265.  
  266. // CALCULATE MIDI -> FREQ
  267. for (int i = 0; i < 5; i++) {
  268. for (int j = 0; j < 4; j++) {
  269. freqTable[i][j] = mtof(midiTable[i][j] - octaveShift);
  270. }
  271. }
  272.  
  273. env.setLevels(64, 52, 50, 0);
  274. env.setTimes(50, DECAY, 3000, 300);
  275. // env.setReleaseTime(2000);
  276. // env.setAttackTime(1000);
  277. // env.setSustainTime(500);
  278. // env.setReleaseTime(3000);
  279. }
  280.  
  281.  
  282.  
  283. void updateControl() {
  284. digitalWrite(trigPin, HIGH);
  285. delayMicroseconds(100);
  286. digitalWrite(trigPin, LOW);
  287. duration = pulseIn(echoPin, HIGH);
  288. dist = (duration/2) / 29.1;
  289.  
  290. // Serial.println(dist);
  291.  
  292. buttonValue1 = mozziAnalogRead(photoResistorPin1);
  293. buttonValue2 = mozziAnalogRead(photoResistorPin2);
  294. buttonValue3 = mozziAnalogRead(photoResistorPin3);
  295. buttonValue4 = mozziAnalogRead(photoResistorPin4);
  296.  
  297. env.update();
  298.  
  299. Serial.println(buttonValue3);
  300.  
  301. if (buttonValue1 < 850 && noteStopped) {
  302. env.noteOn();
  303. currentGain = 255;
  304. noteStopped = false;
  305.  
  306. }
  307.  
  308. if (buttonValue2 < 850 && noteStopped) {
  309. env.noteOn();
  310. currentGain = 255;
  311. noteStopped = false;
  312. }
  313.  
  314. // if (buttonValue3 < 850 && noteStopped) {
  315. // env.noteOn();
  316. // currentGain = 255;
  317. // noteStopped = false;
  318. // }
  319.  
  320. if (buttonValue4 < 600 && noteStopped) {
  321. env.noteOn();
  322. currentGain = 255;
  323. noteStopped = false;
  324. }
  325.  
  326. if (buttonValue1 > 850 && buttonValue2 > 850 && buttonValue4 > 600) {
  327. env.noteOff();
  328. noteStopped = true;
  329.  
  330.  
  331. }
  332.  
  333. if (currentGain > 3) {
  334. currentGain -= 4;
  335. }
  336. else {
  337. currentGain = 0;
  338. }
  339.  
  340. xAxis = mozziAnalogRead(joyXPin);
  341. yAxis = mozziAnalogRead(joyYPin);
  342. sw = digitalRead(joySWPin);
  343.  
  344. dist = distAvg.next(dist);
  345.  
  346. if (dist <= 4 && (env.playing() || noteStopped)) {
  347. // ROOT A4
  348. aSin.setFreq(freqTable[4][0]);
  349. aSquare.setFreq(freqTable[0][0]);
  350.  
  351. aSin2.setFreq(freqTable[4][1]);
  352. aSin3.setFreq(freqTable[4][2]);
  353. aSin4.setFreq(freqTable[4][3]);
  354. }
  355. else if (dist <= 7 && (env.playing() || noteStopped)) {
  356. // ROOT B4
  357. aSin.setFreq(freqTable[3][0]);
  358. aSquare.setFreq(freqTable[1][0]);
  359.  
  360. aSin2.setFreq(freqTable[3][1]);
  361. aSin3.setFreq(freqTable[3][2]);
  362. aSin4.setFreq(freqTable[3][3]);
  363. }
  364. else if (dist <= 10 && (env.playing() || noteStopped)) {
  365. // ROOT C4
  366. aSin.setFreq(freqTable[2][0]);
  367. aSquare.setFreq(freqTable[2][0]);
  368.  
  369. aSin2.setFreq(freqTable[2][1]);
  370. aSin3.setFreq(freqTable[2][2]);
  371. aSin4.setFreq(freqTable[2][3]);
  372. }
  373. else if (dist <= 13 && (env.playing() || noteStopped)) {
  374. // ROOT D4
  375. aSin.setFreq(freqTable[1][0]);
  376. aSquare.setFreq(freqTable[3][0]);
  377.  
  378. aSin2.setFreq(freqTable[1][1]);
  379. aSin3.setFreq(freqTable[1][2]);
  380. aSin4.setFreq(freqTable[1][3]);
  381. }
  382. else if (dist <= 16 && (env.playing() || noteStopped)) {
  383. // ROOT E4
  384. aSin.setFreq(freqTable[0][0]);
  385. aSquare.setFreq(freqTable[4][0]);
  386.  
  387. aSin2.setFreq(freqTable[0][1]);
  388. aSin3.setFreq(freqTable[0][2]);
  389. aSin4.setFreq(freqTable[0][3]);
  390. }
  391.  
  392.  
  393.  
  394.  
  395. }
  396.  
  397. int updateAudio() {
  398. // return ((int) aSin.next() * currentGain1) >> 8;
  399. // return (((int) aSin.next()) * currentGain1)>>8;
  400. // return (3*((long)aSin.next() * currentGain1 + aSin2.next() * currentGain2 +((aSin3.next() * currentGain3)>>1)
  401. // +((aSin4.next() * currentGain4)>>2)) >>3)>>8;
  402.  
  403. int currentSample = 0;
  404.  
  405. if (buttonValue1 < 850) {
  406. currentSample += aSin.next();
  407. }
  408. else if (buttonValue2 < 850) {
  409. currentSample += aSin2.next();
  410. }
  411. else if (buttonValue3 < 850) {
  412. currentSample += aSin3.next();
  413. }
  414. else if (buttonValue4 < 850) {
  415. currentSample += aSin4.next();
  416. }
  417.  
  418. currentSample *= env.next();
  419.  
  420. return (((int) currentSample>>6));
  421. }
  422.  
  423. void loop() {
  424.  
  425.  
  426. controlMenu();
  427.  
  428. if (menuChanged) {
  429. stopMozzi();
  430. mozziStopped = true;
  431. printMenu();
  432. menuChanged = false;
  433. startMozzi(CONTROL_RATE);
  434. }
  435.  
  436. audioHook();
  437. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement