Advertisement
Guest User

Mymidicontroller

a guest
Oct 17th, 2012
2,619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.02 KB | None | 0 0
  1. #include <AnalogMuxDeMux.h>
  2. AnalogMux amux(11, 12, 13, A0); // Define Mux
  3. AnalogMux amux1(8, 9, 10, A1);
  4. AnalogMux amux2(5, 6, 7, A2);
  5. #define NO_PINS 8 // Define number of Mux pins
  6. int Deck1PitchfaderLED = 4;
  7. int StartUpLED = 3;
  8. int Deck2PitchfaderLED = 2;
  9. // Define variables for readings and last states MUX1
  10. int Deck2PBM = 0;
  11. int Deck2PBMState = 0;
  12. int Deck1Pause = 0;
  13. int Deck1PauseState = 0;
  14. int Deck1Pitchfader = 0;
  15. int Deck1PitchfaderState = 0;
  16. int Deck2PBP = 0;
  17. int Deck2PBPState = 0;
  18. int Deck1Volumefader = 0;
  19. int Deck1VolumefaderState = 0;
  20. int Deck1Filter= 0;
  21. int Deck1FilterState = 0;
  22. int Deck1EQLow = 0;
  23. int Deck1EQLowState = 0;
  24. int Deck1EQMid = 0;
  25. int Deck1EQMidState = 0;
  26. // Define variables for readings and last states MUX2
  27. int Deck1Play = 0;
  28. int Deck1PlayState = 0;
  29. int Deck1Cue = 0;
  30. int Deck1CueState = 0;
  31. int Deck2Pitchfader = 0;
  32. int Deck2PitchfaderState = 0;
  33. int Deck2Pause = 0;
  34. int Deck2PauseState= 0;
  35. int Deck2Volumefader = 0;
  36. int Deck2VolumefaderState = 0;
  37. int Deck2EQLow = 0;
  38. int Deck2EQLowState = 0;
  39. int Deck2EQMid = 0;
  40. int Deck2EQMidState = 0;
  41. int Deck1EQHigh = 0;
  42. int Deck1EQHighState = 0;
  43. // Define variables for readings and last states MUX3
  44. int Deck2Play = 0;
  45. int Deck2PlayState = 0;
  46. int Deck1PBM = 0;
  47. int Deck1PBMState = 0;
  48. int Deck2Filter = 0;
  49. int Deck2FilterState = 0;
  50. int Deck1PBP = 0;
  51. int Deck1PBPState = 0;
  52. int Deck2Cue = 0;
  53. int Deck2CueState = 0;
  54. int MasterVolumefader = 0;
  55. int MasterVolumefaderState = 0;
  56. int Deck2EQHigh = 0;
  57. int Deck2EQHighState = 0;
  58. int Crossfader = 0;
  59. int CrossfaderState = 0;
  60.  
  61. // MIDI events
  62. byte CC=176;
  63. byte ON=144;
  64. byte OFF=128;
  65. // Controller numbers
  66. byte CN0=1;
  67. byte CN1=2;
  68. byte CN2=3;
  69. byte CN3=4;
  70. byte CN4=5;
  71. byte CN5=6;
  72. byte CN6=7;
  73. byte CN7=8;
  74. byte CN8=9;
  75. byte CN9=10;
  76. byte CN10=11;
  77. byte CN11=12;
  78. byte CN12=13;
  79. byte CN13=14;
  80. byte CN14=15;
  81. byte CN15=16;
  82. byte CN16=17;
  83. byte CN17=18;
  84. byte CN18=19;
  85. byte CN19=20;
  86. byte CN20=21;
  87. byte CN21=22;
  88. byte CN22=23;
  89. byte CN23=24;
  90. // Midi Status byte for buttons
  91. byte on=127;
  92. byte off=0;
  93.  
  94. void setup()
  95. {
  96. pinMode (13, OUTPUT); // Selector pin1 Mux1
  97. pinMode (12, OUTPUT); // Selector pin2 Mux1
  98. pinMode (11, OUTPUT); // Selector pin3 Mux1
  99. pinMode (10, OUTPUT); // Selector pin1 Mux2
  100. pinMode (9, OUTPUT); // Selector pin2 Mux2
  101. pinMode (8, OUTPUT); // Selector pin3 Mux2
  102. pinMode (7, OUTPUT); // Selector pin1 Mux3
  103. pinMode (6, OUTPUT); // Selector pin2 Mux3
  104. pinMode (5, OUTPUT); // Selector pin3 Mux3
  105. pinMode (Deck1PitchfaderLED, OUTPUT);
  106. pinMode (StartUpLED, OUTPUT);
  107. pinMode (Deck2PitchfaderLED, OUTPUT);
  108. pinMode (A0, INPUT); // Mux Common OUTPUT
  109. pinMode (A1, INPUT);
  110. pinMode (A2, INPUT);
  111.  
  112. //Start the Startup led blinking pattern
  113.  
  114. digitalWrite (Deck1PitchfaderLED, HIGH);
  115. digitalWrite (Deck2PitchfaderLED, HIGH);
  116. delay(300);
  117. digitalWrite (Deck1PitchfaderLED, LOW);
  118. digitalWrite (Deck2PitchfaderLED, LOW);
  119. delay(300);
  120. digitalWrite (Deck1PitchfaderLED, HIGH);
  121. digitalWrite (Deck2PitchfaderLED, HIGH);
  122. for (int i=0; i<=255; i++)
  123. {
  124. delay(2);
  125. analogWrite(StartUpLED, i);
  126. }
  127. digitalWrite(StartUpLED, LOW);
  128. digitalWrite (Deck1PitchfaderLED, LOW);
  129. digitalWrite (Deck2PitchfaderLED, LOW);
  130. delay(300);
  131. digitalWrite (Deck1PitchfaderLED, HIGH);
  132. digitalWrite (Deck2PitchfaderLED, HIGH);
  133. digitalWrite (StartUpLED, HIGH);
  134. delay(1500);
  135. digitalWrite (Deck1PitchfaderLED, LOW);
  136. digitalWrite (Deck2PitchfaderLED, LOW);
  137. digitalWrite (StartUpLED, LOW);
  138.  
  139. Serial.begin(31250);
  140. }
  141.  
  142. void loop()
  143. {
  144. digitalWrite(StartUpLED, HIGH);
  145. // Read the values from the pots and buttons from MUX1
  146. int Deck2PBM = amux.AnalogRead (0) / 511;
  147. //delay(2);
  148. int Deck1Pause = amux.AnalogRead(1) / 511;
  149. //delay(2);
  150. int Deck1Pitchfader = amux.AnalogRead(2) / 8;
  151. //delay(2);
  152. int Deck2PBP = amux.AnalogRead(3) / 511;
  153. //delay(2);
  154. int Deck1Volumefader = amux.AnalogRead(4) / 8;
  155. //delay(2);
  156. int Deck1Filter = amux.AnalogRead(5) / 8;
  157. //delay(2);
  158. int Deck1EQLow = amux.AnalogRead(6) / 8;
  159. //delay(2);
  160. int Deck1EQMid = amux.AnalogRead(7) / 8;
  161.  
  162. // Check to see if Mux1pin0 has changed values and send midi event - button on
  163.  
  164. if (Deck2PBM != Deck2PBMState)
  165. {
  166. if (Deck2PBM == 2)
  167. {
  168. Serial.write(ON);
  169. Serial.write(CN0);
  170. Serial.write(on);
  171. }
  172. }
  173.  
  174. // Check to see if button is released
  175.  
  176. if (Deck2PBM == 0 && Deck2PBMState == 2)
  177. {
  178. Serial.write(OFF);
  179. Serial.write(CN0);
  180. Serial.write(off);
  181. }
  182. Deck2PBMState=Deck2PBM;
  183.  
  184. // Check to see if Mux1pin1 has changed values
  185.  
  186. if (Deck1Pause != Deck1PauseState)
  187. {
  188. if (Deck1Pause == 2)
  189. {
  190. Serial.write(ON);
  191. Serial.write(CN1);
  192. Serial.write(on);
  193. }
  194. }
  195.  
  196. if (Deck1Pause == 0 && Deck1PauseState == 2)
  197. {
  198. Serial.write(OFF);
  199. Serial.write(CN1);
  200. Serial.write(off);
  201. }
  202. Deck1PauseState=Deck1Pause;
  203.  
  204. // Check to see if Mux1pin2 has changed values
  205.  
  206.  
  207. if (abs(Deck1Pitchfader - Deck1PitchfaderState) >=2)
  208. {
  209. Serial.write(CC);
  210. Serial.write(CN2);
  211. Serial.write(Deck1Pitchfader);
  212. Deck1PitchfaderState = Deck1Pitchfader;
  213. }
  214.  
  215. //If pitchfader is centered light LED
  216. if (Deck1Pitchfader == 64)
  217. {
  218. digitalWrite(Deck1PitchfaderLED, HIGH);
  219. }
  220. else
  221. {
  222. digitalWrite(Deck1PitchfaderLED,LOW);
  223. }
  224. // Check to see if Mux1pin3 has changed values
  225.  
  226. if (Deck2PBP != Deck2PBPState)
  227. {
  228. if (Deck2PBP == 2)
  229. {
  230. Serial.write(ON);
  231. Serial.write(CN3);
  232. Serial.write(on);
  233. }
  234. }
  235.  
  236. // Check to see if button is released
  237.  
  238. if (Deck2PBP == 0 && Deck2PBPState == 2)
  239. {
  240. Serial.write(OFF);
  241. Serial.write(CN3);
  242. Serial.write(off);
  243. }
  244. Deck2PBPState=Deck2PBP;
  245.  
  246. // Check to see if Mux1pin4 has changed its values
  247.  
  248. if (abs(Deck1Volumefader - Deck1VolumefaderState) >= 2)
  249. {
  250. Serial.write(CC);
  251. Serial.write(CN4);
  252. Serial.write(Deck1Volumefader);
  253. Deck1VolumefaderState=Deck1Volumefader;
  254. }
  255.  
  256. // Check to see if Mux1pin5 has changed its values
  257.  
  258. if (abs(Deck1Filter - Deck1FilterState) >= 2)
  259. {
  260. Serial.write(CC);
  261. Serial.write(CN5);
  262. Serial.write(Deck1Filter);
  263. Deck1FilterState=Deck1Filter;
  264. }
  265.  
  266. // Check to see if Mux1pin6 has changed its values
  267.  
  268. if (abs(Deck1EQLow - Deck1EQLowState) >= 2)
  269. {
  270. Serial.write(CC);
  271. Serial.write(CN6);
  272. Serial.write(Deck1EQLow);
  273. Deck1EQLowState=Deck1EQLow;
  274. }
  275.  
  276. // Check to see if Mux1pin7 has changed its values
  277.  
  278. if (abs(Deck1EQMid - Deck1EQMidState) >= 2)
  279. {
  280. Serial.write(CC);
  281. Serial.write(CN7);
  282. Serial.write(Deck1EQMid);
  283. Deck1EQMidState=Deck1EQMid;
  284. }
  285. // END OF MUX1
  286.  
  287. // Read the values from the pots and buttons from MUX2
  288. int Deck1Play = amux1.AnalogRead (0)/ 511;
  289. //delay(2);
  290. int Deck1Cue = amux1.AnalogRead(1)/511;
  291. //delay(2);
  292. int Deck2Pitchfader = amux1.AnalogRead(2) / 8;
  293. //delay(2);
  294. int Deck2Pause = amux1.AnalogRead(3)/511;
  295. //delay(2);
  296. int Deck2Volumefader = amux1.AnalogRead(4) / 8;
  297. //delay(2);
  298. int Deck2EQLow = amux1.AnalogRead(5) / 8;
  299. //delay(2);
  300. int Deck2EQMid = amux1.AnalogRead(6) / 8;
  301. //delay(2);
  302. int Deck1EQHigh = amux1.AnalogRead(7) / 8;
  303.  
  304. // Check to see if Mux2pin0 has changed values and send midi event - button on
  305.  
  306. if (Deck1Play != Deck1PlayState)
  307. {
  308. if (Deck1Play == 1)
  309. {
  310. Serial.write(ON);
  311. Serial.write(CN8);
  312. Serial.write(on);
  313. }
  314. }
  315.  
  316. // Check to see if button is released
  317.  
  318. if (Deck1Play == 0 && Deck1PlayState == 1)
  319. {
  320. Serial.write(OFF);
  321. Serial.write(CN8);
  322. Serial.write(off);
  323. }
  324. Deck1PlayState=Deck1Play;
  325.  
  326. // Check to see if Mux2pin1 has changed values
  327.  
  328. if (Deck1Cue != Deck1CueState)
  329. {
  330. if (Deck1Cue == 1)
  331. {
  332. Serial.write(ON);
  333. Serial.write(CN9);
  334. Serial.write(on);
  335. }
  336. }
  337.  
  338. if (Deck1Cue == 0 && Deck1CueState == 1)
  339. {
  340. Serial.write(OFF);
  341. Serial.write(CN9);
  342. Serial.write(off);
  343. }
  344. Deck1CueState=Deck1Cue;
  345.  
  346. // Check to see if Mux2pin2 has changed values
  347.  
  348. if (abs(Deck2Pitchfader - Deck2PitchfaderState) >=2)
  349. {
  350. Serial.write(CC);
  351. Serial.write(CN10);
  352. Serial.write(Deck2Pitchfader);
  353. Deck2PitchfaderState = Deck2Pitchfader;
  354. }
  355.  
  356. //If pitchfader2 is centered light up led
  357.  
  358. if (Deck2Pitchfader == 64)
  359. {
  360. digitalWrite(Deck2PitchfaderLED, HIGH);
  361. }
  362. else
  363. {
  364. digitalWrite(Deck2PitchfaderLED,LOW);
  365. }
  366.  
  367. // Check to see if Mux2pin3 has changed values
  368.  
  369. if (Deck2Pause != Deck2PauseState)
  370. {
  371. if (Deck2Pause == 2)
  372. {
  373. Serial.write(ON);
  374. Serial.write(CN11);
  375. Serial.write(on);
  376. }
  377. }
  378.  
  379. // Check to see if button is released
  380.  
  381. if (Deck2Pause == 0 && Deck2PauseState == 2)
  382. {
  383. Serial.write(OFF);
  384. Serial.write(CN11);
  385. Serial.write(off);
  386. }
  387. Deck2PauseState=Deck2Pause;
  388.  
  389. // Check to see if Mux2pin4 has changed its values
  390.  
  391. if (abs(Deck2Volumefader - Deck2VolumefaderState) >= 2)
  392. {
  393. Serial.write(CC);
  394. Serial.write(CN12);
  395. Serial.write(Deck2Volumefader);
  396. Deck2VolumefaderState=Deck2Volumefader;
  397. }
  398.  
  399. // Check to see if Mux2pin5 has changed its values
  400.  
  401. if (abs(Deck2EQLow - Deck2EQLowState) >= 2)
  402. {
  403. Serial.write(CC);
  404. Serial.write(CN13);
  405. Serial.write(Deck2EQLow);
  406. Deck2EQLowState=Deck2EQLow;
  407. }
  408.  
  409. // Check to see if Mux2pin6 has changed its values
  410.  
  411. if (abs(Deck2EQMid - Deck2EQMidState) >= 2)
  412. {
  413. Serial.write(CC);
  414. Serial.write(CN14);
  415. Serial.write(Deck2EQMid);
  416. Deck2EQMidState=Deck2EQMid;
  417. }
  418.  
  419. // Check to see if Mux2pin7 has changed its values
  420.  
  421. if (abs(Deck1EQHigh - Deck1EQHighState) >= 2)
  422. {
  423. Serial.write(CC);
  424. Serial.write(CN15);
  425. Serial.write(Deck1EQHigh);
  426. Deck1EQHighState=Deck1EQHigh;
  427. }
  428.  
  429. // END OF MUX2
  430.  
  431. // Read the values from the pots and buttons from MUX3
  432. int Deck2Play = amux2.AnalogRead (0)/511;
  433. //delay(2);
  434. int Deck1PBM = amux2.AnalogRead(1)/511;
  435. //delay(2);
  436. int Deck2Cue = amux2.AnalogRead(2)/511;
  437. //delay(2);
  438. int Deck1PBP = amux2.AnalogRead(3)/511;
  439. //delay(2);
  440. int Deck2Filter = amux2.AnalogRead(4) / 8;
  441. //delay(2);
  442. int MasterVolumefader = amux2.AnalogRead(5) / 8;
  443. //delay(2);
  444. int Deck2EQHigh = amux2.AnalogRead(6) / 8;
  445. //delay(2);
  446. int Crossfader = amux2.AnalogRead(7) / 8;
  447.  
  448. // Check to see if Mux3pin0 has changed values and send midi event - button on
  449.  
  450. if (Deck2Play != Deck2PlayState)
  451. {
  452. if (Deck2Play == 1)
  453. {
  454. Serial.write(ON);
  455. Serial.write(CN16);
  456. Serial.write(on);
  457. }
  458. }
  459.  
  460. // Check to see if button is released
  461.  
  462. if (Deck2Play == 0 && Deck2PlayState == 1)
  463. {
  464. Serial.write(OFF);
  465. Serial.write(CN16);
  466. Serial.write(off);
  467. }
  468. Deck2PlayState=Deck2Play;
  469.  
  470. // Check to see if Mux3pin1 has changed values
  471.  
  472. if (Deck1PBM != Deck1PBMState)
  473. {
  474. if (Deck1PBM == 1)
  475. {
  476. Serial.write(ON);
  477. Serial.write(CN17);
  478. Serial.write(on);
  479. }
  480. }
  481.  
  482. if (Deck1PBM == 0 && Deck1PBMState == 1)
  483. {
  484. Serial.write(OFF);
  485. Serial.write(CN17);
  486. Serial.write(off);
  487. }
  488. Deck1PBMState=Deck1PBM;
  489.  
  490. // Check to see if Mux3pin2 has changed values
  491.  
  492. if (Deck2Cue != Deck2CueState)
  493. {
  494. if (Deck2Cue == 1)
  495. {
  496. Serial.write(ON);
  497. Serial.write(CN18);
  498. Serial.write(on);
  499. }
  500. }
  501.  
  502. if (Deck2Cue == 0 && Deck2CueState == 1)
  503. {
  504. Serial.write(OFF);
  505. Serial.write(CN18);
  506. Serial.write(off);
  507. }
  508. Deck2CueState=Deck2Cue;
  509.  
  510. // Check to see if Mux3pin3 has changed values
  511.  
  512. if (Deck1PBP != Deck1PBPState)
  513. {
  514. if (Deck1PBP == 1)
  515. {
  516. Serial.write(ON);
  517. Serial.write(CN19);
  518. Serial.write(on);
  519. }
  520. }
  521.  
  522. // Check to see if button is released
  523.  
  524. if (Deck1PBP == 0 && Deck1PBPState == 1)
  525. {
  526. Serial.write(OFF);
  527. Serial.write(CN19);
  528. Serial.write(off);
  529. }
  530. Deck1PBPState=Deck1PBP;
  531.  
  532. // Check to see if Mux3pin4 has changed its values
  533.  
  534. if (abs(Deck2Filter - Deck2FilterState) >= 2)
  535. {
  536. Serial.write(CC);
  537. Serial.write(CN20);
  538. Serial.write(Deck2Filter);
  539. Deck2FilterState=Deck2Filter;
  540. }
  541.  
  542. // Check to see if Mux3pin5 has changed its values
  543.  
  544. if (abs(MasterVolumefader - MasterVolumefaderState) >= 2)
  545. {
  546. Serial.write(CC);
  547. Serial.write(CN21);
  548. Serial.write(MasterVolumefader);
  549. MasterVolumefaderState=MasterVolumefader;
  550. }
  551.  
  552. // Check to see if Mux3pin6 has changed its values
  553.  
  554. if (abs(Deck2EQHigh - Deck2EQHighState) >= 2)
  555. {
  556. Serial.write(CC);
  557. Serial.write(CN22);
  558. Serial.write(Deck2EQHigh);
  559. Deck2EQHighState=Deck2EQHigh;
  560. }
  561.  
  562. // Check to see if Mux3pin7 has changed its values
  563.  
  564. if (abs(Crossfader - CrossfaderState) >= 2)
  565. {
  566. Serial.write(CC);
  567. Serial.write(CN23);
  568. Serial.write(Crossfader);
  569. CrossfaderState=Crossfader;
  570. }
  571.  
  572. // END OF MUX3
  573.  
  574. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement