document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <SoftwareSerial.h>
  2. #include <TimerOne.h>
  3. int mval;
  4. const int onecount = 52;
  5. const int othercount = 53;
  6. int metronome = 130;
  7. int delaycount = 0;
  8. int metVal = 120;
  9. int cmdnum = 0;
  10. int CMDpotPin = 0;
  11. int StateCMDpotPin = 1;
  12. int METpotPin = A2;
  13. int Stateval = 0;
  14. int CMDval = 0; // variable to store the value coming from the sensor
  15. const byte anaIn = A0;
  16. const byte anaStateIn = A1;
  17. const byte anaMetronomeIn = A2;
  18. int snum = 1;
  19. SoftwareSerial mySerial(10, 11);
  20. int btnnum = 22;
  21. int buttonPin;
  22. int allbuttonNum = 18;
  23. int x;
  24. int bynum;
  25. byte TempCMDToChange;
  26. boolean currentStates[18];//stroage for current measured button state
  27. boolean lastStates[18] ;//storage for last measured button state
  28. boolean debouncedStates[18] ;//debounced button state
  29. int debounceInterval[18];//wait 20 ms for button pin to settle
  30. unsigned long timeOfLastButtonEvents[18];//store the last time the button state changed
  31. int PinToChange;
  32. int StateToChange;
  33. int nextbyte = 0;
  34. int BounceItInterval = 5;
  35. String fromsoftware;
  36. char* message = "";
  37. char* TempStatemessage = "";
  38. char* whencmds[3] = {
  39. "Push", "Held", "Released"
  40. };
  41. char* cmds[8] = {
  42. "Note On", "Note Off", "After Touch", "Continuous Controller", "Patch Change", "Channel Pressure", "Pitch Bend", "Non-Musical Commands"
  43. };
  44. char firstchar;
  45. int StateValue = 0;
  46. int PadToChange;
  47. char PadValue;
  48. byte PushCMDToChange ;
  49. byte RelCMDToChange;
  50. byte HeldCMDToChange;
  51. int CMDValue = 1;
  52. byte NoteToChange;
  53. int NoteValue = 1;
  54. byte VelToChange;
  55. int VelValue = 1;
  56. char TrackToChange;
  57. int TrackValue = 1;
  58. char BarsToChange;
  59. int BarsValue = 1;
  60. char LengthToChange;
  61. int LengthValue = 1;
  62. char ActBarToChange;
  63. int ActBarValue = 1;
  64. char BankToChange;
  65. int BankValue = 1;
  66. int val = 0;
  67. int padpin = 22;
  68. int otherbtnpin = 38;
  69. int banknum = 0;
  70. int stillHIGH;
  71. char thispadpin;
  72. int SelectedPad = 1;
  73. int mnum = 0;
  74.  
  75. void setup() {
  76. Timer1.initialize(1000000); // set a timer of length 100000 microseconds (or 0.1 sec - or 10Hz => the led will blink 5 times, 5 cycles of on-and-off, per second)
  77. Timer1.attachInterrupt( RealMetronome ); // attach the service routine here
  78.  
  79. pinMode(onecount, OUTPUT);
  80. pinMode(othercount, OUTPUT);
  81. pinMode(13, OUTPUT);
  82. pinMode(52, OUTPUT);
  83. pinMode(53, OUTPUT);
  84. pinMode(22, INPUT);//this time we will set the pin as INPUT
  85. pinMode(23, INPUT);
  86. pinMode(24, INPUT);
  87. pinMode(25, INPUT);
  88. pinMode(26, INPUT);
  89. pinMode(27, INPUT);
  90. pinMode(28, INPUT);
  91. pinMode(29, INPUT);
  92. pinMode(31, INPUT);
  93. pinMode(32, INPUT);
  94. pinMode(33, INPUT);
  95. pinMode(34, INPUT);
  96. pinMode(35, INPUT);
  97. pinMode(36, INPUT);
  98. pinMode(37, INPUT);
  99. pinMode(38, INPUT);
  100. pinMode(39, INPUT);
  101.  
  102. int pn = 1;
  103. int interval = 0;
  104. int padpin = 22;
  105. digitalWrite(13, LOW);
  106. while (pn <= allbuttonNum) {
  107. debounceInterval[interval] = BounceItInterval;
  108. digitalWrite(pn, LOW);
  109. debouncedStates[pn] = LOW;
  110. lastStates[pn] = LOW;
  111. currentStates[pn] = LOW;
  112. padpin++;
  113. interval++;
  114. pn++;
  115. }
  116. //Serial.begin(31250);//initialize Serial connection
  117. Serial.begin(9600);//initialize Serial connection
  118. }
  119.  
  120. void loop() {
  121. digitalWrite(othercount, LOW);
  122. digitalWrite(onecount, LOW);
  123. //DoMetronome();
  124. Serial.println(metVal);
  125. //POT KNOB CHECKS will be before to ensure the right settings are placed to a button
  126. //CODE GOES HERE
  127.  
  128. //LoadPadSettings(); // Load settings from file
  129.  
  130. //GET State Setting From State POT
  131. GETStatePotpin();
  132. //GET Command Setting From Comand POT
  133. GETCommandPotpin();
  134.  
  135. SelectedPad = 0;
  136. padpin = 22;
  137. otherbtnpin = 38;
  138. int pn = 1;
  139. while (pn < allbuttonNum) {
  140. currentStates[pn] = digitalRead(padpin);
  141. unsigned long currentTime = millis();
  142.  
  143. if (currentStates[pn] != lastStates[pn]) {
  144. timeOfLastButtonEvents[pn] = currentTime;
  145. }
  146.  
  147. if (currentTime - timeOfLastButtonEvents[pn] > debounceInterval[pn]) { //if enough time has passed
  148.  
  149. if ((pn > 0) && (pn < 16)) {
  150. //PAD BUTTONS
  151. if (currentStates[pn] != debouncedStates[pn]) { //if the current state is still different than our last stored debounced state
  152. debouncedStates[pn] = currentStates[pn];//update the debounced state
  153. //trigger an event
  154. PadToChange = 0;
  155. //PushCMDToChange = 0;
  156. //RelCMDToChange = 0;
  157. //HeldCMDToChange = 0;
  158.  
  159. if (debouncedStates[pn] == HIGH) {
  160. PadToChange = pn;
  161. if ((debouncedStates[pn] == HIGH) && (digitalRead(38) == HIGH) && (pn < 16)) {
  162. //Do Push Select Button
  163.  
  164. digitalWrite(13, 1 );
  165. SelectedPad = PadToChange;
  166. Serial.println(SelectedPad);
  167. }
  168.  
  169. //PushCMDToChange = 143 + cmdnum; //Get Current setting From Text File Or ini fro SD Card.
  170. VelToChange = 127;
  171. thispadpin = padpin;
  172. stillHIGH = 1;
  173.  
  174. StateToChange = 1;
  175. pushbuttonClicked(padpin);
  176. Serial.println("pressed");
  177. digitalWrite(13, 1 );
  178. } else {
  179. VelToChange = 0;
  180. thispadpin = padpin;
  181. //RelCMDToChange = 127 + cmdnum;
  182. //PadToChange = pn;
  183. StateToChange = 0;
  184. stillHIGH = 0;
  185. relbuttonClicked(padpin);
  186. Serial.println("released");
  187. digitalWrite(13, 0 );
  188. }
  189. while (stillHIGH > 0) {
  190. if (debouncedStates[pn] == HIGH) {
  191. thispadpin = padpin;
  192. //VelToChange = '127';
  193. //HeldCMDToChange = 144 + cmdnum;
  194. //PadToChange = pn;
  195. StateToChange = 1;
  196. stillHIGH = 0;
  197. //heldbuttonClicked(22);
  198. Serial.println("HELD...");
  199. digitalWrite(13, 1 );
  200. }
  201. }
  202. }
  203. //Serial.println(message);
  204. pn++;
  205. padpin++;
  206. }
  207. else {
  208. //All OTHER BUTTONS
  209. if (currentStates[pn] != debouncedStates[pn]) { //if the current state is still different than our last stored debounced state
  210. debouncedStates[pn] = currentStates[pn];//update the debounced state
  211. //trigger an event
  212. if (debouncedStates[pn] == HIGH) {
  213. if ((pn == 17) && (digitalRead(38) == HIGH)) {
  214. //Do Push Select Button
  215. otherbtnpin = 38;
  216. int spadnum = 38;
  217. for (int spnum = 38; spnum < 39; spnum++) {
  218. if ((debouncedStates[pn] == HIGH) && (digitalRead(38) == HIGH) && (pn > 16)) {
  219. //Do Push Select Button
  220. stillHIGH = 0;
  221. digitalWrite(13, 1 );
  222.  
  223. Serial.println("Select Push");
  224. }
  225. spadnum++;
  226. }
  227. }
  228. if ((pn == 18) && (digitalRead(39) == HIGH)) {
  229. //Do Push Save Button
  230. otherbtnpin = 39;
  231. stillHIGH = 0;
  232. if (PadToChange > 0) {
  233. digitalWrite(13, 1 );
  234. TempCMDToChange = 143 + cmdnum;
  235. Serial.println("Save Push");
  236. }
  237. }
  238. }
  239. else {
  240. if ((pn == 17) && (digitalRead(otherbtnpin) == LOW)) {
  241. //Do Release Select Button
  242. stillHIGH = 0;
  243. otherbtnpin = 38;
  244. SelectedPad = PadToChange;
  245. stillHIGH = 0;
  246. //ChangeSelectedPad;
  247. digitalWrite(13, 0 );
  248. Serial.println("Select release");
  249. }
  250. if ((pn == 18) && (digitalRead(otherbtnpin) == LOW)) {
  251. //Do Release Save Button
  252. if (PadToChange > 0) {
  253. stillHIGH = 0;
  254. otherbtnpin = 39;
  255. SavePadSetting();
  256. digitalWrite(13, 0 );
  257. Serial.println("Save Release");
  258. }
  259. }
  260. }
  261.  
  262.  
  263.  
  264. }
  265. otherbtnpin++;
  266. padpin++;
  267. pn++;
  268. }
  269.  
  270. }
  271.  
  272. lastStates[pn] = currentStates[pn];
  273. }
  274. }
  275.  
  276. void RealMetronome()
  277. {
  278.  
  279. if (delaycount >= 4) {
  280. delaycount = 1;
  281. } else {
  282. delaycount++;
  283. }
  284. if (delaycount == 1) {
  285. digitalWrite(othercount, LOW);
  286. digitalWrite(onecount, HIGH);
  287.  
  288. } else {
  289.  
  290. digitalWrite(onecount, LOW);
  291. digitalWrite(othercount, HIGH);
  292.  
  293. }
  294. }
  295.  
  296. void GETCommandPotpin() {
  297. int val = analogRead(CMDpotPin);
  298.  
  299. switch (val)
  300. {
  301. case 0 ... 127:
  302. message = cmds[0];
  303. cmdnum = 1;
  304. break;
  305. case 128 ... 254:
  306. message = cmds[1];
  307. cmdnum = 2;
  308. break;
  309. case 255 ... 381:
  310. message = cmds[2];
  311. cmdnum = 3;
  312. break;
  313. case 382 ... 508:
  314. message = cmds[3];
  315. cmdnum = 4;
  316. break;
  317. case 509 ... 635:
  318. message = cmds[4];
  319. cmdnum = 5;
  320. break;
  321. case 636 ... 762:
  322. message = cmds[5];
  323. cmdnum = 6;
  324. break;
  325. case 763 ... 889:
  326. message = cmds[6];
  327. cmdnum = 7;
  328. break;
  329. case 890 ... 1016:
  330. message = cmds[7];
  331. cmdnum = 8;
  332. break;
  333. default:
  334. message = "Null Command";
  335. }
  336. }
  337. void GETMetVAL() {
  338.  
  339.  
  340. switch (mval)
  341. {
  342.  
  343. case 0 ... 1023:
  344.  
  345. if (analogRead(A3) < 10) {
  346. metVal = 10;
  347. } else {
  348. mval = analogRead(A3);
  349. metVal = mval;
  350. }
  351. break;
  352. default:
  353. metVal = 130;
  354. }
  355. }
  356. void GETStatePotpin() {
  357. int Sval = analogRead(StateCMDpotPin);
  358. snum = 0;
  359. switch (Sval)
  360. {
  361. case 0 ... 341:
  362. TempStatemessage = whencmds[0];
  363. snum = 1;
  364. break;
  365. case 342 ... 682:
  366. TempStatemessage = whencmds[1];
  367. snum = 2;
  368. break;
  369. case 683 ... 1023:
  370. TempStatemessage = whencmds[2];
  371. snum = 3;
  372. break;
  373. default:
  374. TempStatemessage = "Null State";
  375. }
  376. }
  377. void SavePadSetting() {
  378. //Search for the correct state to save
  379. if (whencmds[snum] == "Push") {
  380. PushCMDToChange = TempCMDToChange;
  381. }
  382. if (whencmds[snum] == "Held") {
  383. HeldCMDToChange = TempCMDToChange;
  384. }
  385. if (whencmds[snum] == "Release") {
  386. RelCMDToChange = TempCMDToChange;
  387. }
  388. //then save chages to file
  389.  
  390. //Located at SelectedPad (retruns the currently selected pad as a int);
  391.  
  392. //Settings are automaticly reloaded as they are applyed with the next loop rotaion.
  393. }
  394. void pushbuttonClicked(int btn) {
  395. if (btn == padpin && debouncedStates[btn] == HIGH) {
  396. pushchangeAllStates();
  397. }
  398. }
  399. void heldbuttonClicked(int btn) {
  400. if (btn == padpin && debouncedStates[btn] == HIGH) {
  401. heldchangeAllStates();
  402. }
  403. }
  404. void relbuttonClicked(int btn) {
  405. if (btn == padpin && debouncedStates[btn] == HIGH) {
  406. relchangeAllStates();
  407. }
  408. }
  409. void pushchangeAllStates() {
  410. noteOn(PushCMDToChange, NoteToChange, VelToChange);
  411. }
  412. void heldchangeAllStates() {
  413. noteOn(HeldCMDToChange, NoteToChange, VelToChange);
  414. }
  415. void relchangeAllStates() {
  416. noteOn(RelCMDToChange, NoteToChange, VelToChange);
  417. }
  418. void noteOn(byte cmd, byte pitch, byte velocity) {
  419. //mySerial.write(cmd);
  420. //mySerial.write(pitch);
  421. // mySerial.write(velocity);
  422. //Serial.write(cmd);
  423. //Serial.write(pitch);
  424. //Serial.write(velocity);
  425. }
');