Advertisement
Guest User

Simon says MIDI Controller

a guest
Apr 21st, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.61 KB | None | 0 0
  1. void loop() {
  2. if (state==PLAY){
  3.   showsequence();
  4.   readsequnce();
  5. }
  6. else if(state==TRYAGIN){
  7.   incorrect();
  8.   state=PLAY;
  9. }
  10. }
  11. void MyHandleNoteOn(byte channel, byte pitch, byte velocity) {
  12. data1 = MIDI.getData1();
  13. if (data1==48){//C3
  14.   Status[0][0]=1;
  15.   noteOn(0,data1,100);
  16.   input[0]=data1;
  17. }
  18. if (data1==49){//C3#
  19. Status[0][1]=1;
  20. noteOn(0,data1,100);
  21. input[1]=data1;
  22. }
  23. if (data1==50){//D3
  24. Status[0][2]=1;
  25. noteOn(0,data1,100);
  26. input[2]=data1;
  27. }
  28. if (data1==51){//D3#
  29. Status[0][3]=1;
  30. noteOn(0,data1,100);
  31. input[3]=data1;
  32. }
  33. if (data1==52){//E3
  34. Status[0][4]=1;
  35. noteOn(0,data1,100);
  36. input[4]=data1;
  37. }
  38. if (data1==53){//F3
  39. Status[1][0]=1;
  40. noteOn(0,data1,100);
  41. input[5]=data1;
  42. }
  43. if (data1==54){//F3#
  44. Status[1][1]=1;
  45. noteOn(0,data1,100);
  46. input[6]=data1;
  47. }
  48. if (data1==55){//G3
  49. Status[1][2]=1;
  50. noteOn(0,data1,100);
  51. input[7]=data1;
  52. }
  53. if (data1==56){//G3#
  54. Status[1][3]=1;
  55. noteOn(0,data1,100);
  56. input[8]=data1;
  57. }
  58. if (data1==57){//A3
  59. Status[1][4]=1;
  60. noteOn(0,data1,100);
  61. input[9]=data1;
  62. }
  63. if (data1==58){//A3#
  64. Status[2][0]=1;
  65. noteOn(0,data1,100);
  66. input[10]=data1;
  67. }
  68. if (data1==59){//B3
  69. Status[2][1]=1;
  70. noteOn(0,data1,100);
  71. input[11]=data1;
  72. }
  73. if (data1==60){//C4
  74. Status[2][2]=1;
  75. noteOn(0,data1,100);
  76. input[12]=data1;
  77. }
  78. if (data1==61){//C4#
  79. Status[2][3]=1;
  80. noteOn(0,data1,100);
  81. input[13]=data1;
  82. }
  83. if (data1==62){//D4
  84. Status[2][4]=1;
  85. noteOn(0,data1,100);
  86. input[14]=data1;
  87. }
  88. if (data1==63){//D4#
  89. Status[3][0]=1;
  90. noteOn(0,data1,100);
  91. input[15]=data1;
  92. }
  93. if (data1==64){//E4
  94. Status[3][1]=1;
  95. noteOn(0,data1,100);
  96. input[16]=data1;
  97. }
  98. if (data1==65){//F4
  99. Status[3][2]=1;
  100. noteOn(0,data1,100);
  101. input[17]=data1;
  102. }
  103. if (data1==66){//F4#
  104. Status[3][3]=1;
  105. noteOn(0,data1,100);
  106. input[18]=data1;
  107. }
  108. if (data1==67){//G4
  109. Status[3][4]=1;
  110. noteOn(0,data1,100);
  111. input[19]=data1;
  112. }
  113. if (data1==68){//G4#
  114. Status[4][0]=1;
  115. noteOn(0,data1,100);
  116. input[20]=data1;
  117. }
  118. if (data1==69){//A4
  119. Status[4][1]=1;
  120. noteOn(0,data1,100);
  121. input[21]=data1;
  122. }
  123. if (data1==70){//A4#
  124. Status[4][2]=1;
  125. noteOn(0,data1,100);
  126. input[22]=data1;
  127. }
  128. if (data1==71){//B4
  129. Status[4][3]=1;
  130. noteOn(0,data1,100);
  131. input[23]=data1;
  132. }
  133. if (data1==72){//C5
  134. Status[4][4]=1;
  135. noteOn(0,data1,100);
  136. input[24]=data1;
  137. }
  138. }
  139. void MyHandleNoteOff(byte channel, byte pitch, byte velocity) {
  140.   data1 = MIDI.getData1();
  141. if (data1==48){//C3
  142. Status[0][0]=0;
  143. noteOff(0,data1,100);
  144. input[0]=0;
  145.  }
  146.  if (data1==49){//C3#
  147. Status[0][1]=0;
  148. noteOff(0,data1,100);
  149. input[1]=0;
  150. }
  151. if (data1==50){//D3
  152. Status[0][2]=0;
  153. noteOff(0,data1,100);
  154. input[2]=0;
  155. }
  156. if (data1==51){//D3#
  157. Status[0][3]=0;
  158. noteOff(0,data1,100);
  159. input[3]=0;
  160. }
  161. if (data1==52){//E3
  162. Status[0][4]=0;
  163. noteOff(0,data1,100);
  164. input[4]=0;
  165. }
  166. if (data1==53){//F3
  167. Status[1][0]=0;
  168. noteOff(0,data1,100);
  169. input[5]=0;
  170. }
  171. if (data1==54){//F3#
  172. Status[1][1]=0;
  173. noteOff(0,data1,100);
  174. input[6]=0;
  175. }
  176. if (data1==55){//G3
  177.  Status[1][2]=0;
  178.  noteOff(0,data1,100);
  179.  input[7]=0;
  180. }
  181. if (data1==56){//G3#
  182. Status[1][3]=0;
  183. noteOff(0,data1,100);
  184. input[8]=0;
  185. }
  186. if (data1==57){//A3
  187. Status[1][4]=0;
  188. noteOff(0,data1,100);
  189. input[9]=0;
  190. }
  191. if (data1==58){//A3#
  192. Status[2][0]=0;
  193. noteOff(0,data1,100);
  194. input[10]=0;
  195. }
  196. if (data1==59){//B3
  197. Status[2][1]=0;
  198. noteOff(0,data1,100);
  199. input[11]=0;
  200. }
  201. if (data1==60){//C4
  202. Status[2][2]=0;
  203. noteOff(0,data1,100);
  204. input[12]=0;
  205. }
  206. if (data1==61){//C4#
  207. Status[2][3]=0;
  208. noteOff(0,data1,100);
  209. input[13]=0;
  210. }
  211. if (data1==62){//D4
  212. Status[2][4]=0;
  213. noteOff(0,data1,100);
  214. input[14]=0;
  215. }
  216. if (data1==63){//D4#
  217. Status[3][0]=0;
  218. noteOff(0,data1,100);
  219. input[15]=0;
  220. }
  221. if (data1==64){//E4
  222. Status[3][1]=0;
  223. noteOff(0,data1,100);
  224. input[16]=0;
  225. }
  226. if (data1==65){//F4
  227. Status[3][2]=0;
  228. noteOff(0,data1,100);
  229. input[17]=0;
  230. }
  231. if (data1==66){//F4#
  232. Status[3][3]=0;
  233. noteOff(0,data1,100);
  234. input[18]=0;
  235. }
  236. if (data1==67){//G4
  237. Status[3][4]=0;
  238. noteOff(0,data1,100);
  239. input[19]=0;
  240. }
  241. if (data1==68){//G4#
  242. Status[4][0]=0;
  243. noteOff(0,data1,100);
  244. input[20]=0;
  245. }
  246. if (data1==69){//A4
  247. Status[4][1]=0;
  248. noteOff(0,data1,100);
  249. input[21]=0;
  250. }
  251. if (data1==70){//A4#
  252. Status[4][2]=0;
  253. noteOff(0,data1,100);
  254. input[22]=0;
  255. }
  256. if (data1==71){//B4
  257. Status[4][3]=0;
  258. noteOff(0,data1,100);
  259. input[23]=0;
  260. }
  261. if (data1==72){//C5
  262. Status[4][4]=0;
  263. noteOff(0,data1,100);
  264. input[24]=0;
  265. }
  266. }
  267. void noteOn(byte channel, byte note, byte attack_velocity) {
  268.   talkMIDI( (0x90 | channel), note, attack_velocity);
  269. }
  270. //Send a MIDI note-off message.  Like releasing a piano key
  271. void noteOff(byte channel, byte note, byte release_velocity) {
  272.   talkMIDI( (0x80 | channel), note, release_velocity);
  273.   }
  274. //Plays a MIDI note. Doesn't check to see that cmd is greater than 127, or that data values are less than 127
  275. void talkMIDI(byte cmd, byte data1, byte data2) {
  276.   mySerial.write(cmd);
  277.   mySerial.write(data1);
  278.  
  279.  
  280.  
  281.   //Some commands only have one data byte. All cmds less than 0xBn have 2 data bytes
  282.   //(sort of: http://253.ccarh.org/handout/midiprotocol/)
  283.   if( (cmd & 0xF0) <= 0xB0)
  284.     mySerial.write(data2);
  285.  
  286. }
  287.  void playNote(int midi, int row, int col, int len, bool short_led = false)
  288. {
  289.     int len1, len2;
  290.     if (short_led) {
  291.         len1 = len * 3 / 4;
  292.         len2 = len * 1 / 4;
  293.     } else {
  294.         len1 = len;
  295.         len2 = 0;
  296.     }
  297.     noteOn(0x90, midi, 100);
  298.     digitalWrite(LedR[row], HIGH);
  299.     digitalWrite(LedC[col], LOW);
  300.     delay(len1);
  301.     digitalWrite(LedR[row], LOW);
  302.     digitalWrite(LedC[col], HIGH);
  303.     delay(len2);
  304.     noteOff(0x90, midi, 0);
  305. }
  306. void songlearn (int tune){
  307.   if (tune==1){
  308.    lcd.clear();
  309.   lcd.setCursor(4,0);
  310.   lcd.print("Learning Song :");// Print a message to the LCD.
  311.   lcd.setCursor(1, 1);
  312.   lcd.print(menuP1);
  313.   //Plays Mary Had a Little Lamb on a loop at 145 BPM
  314.  
  315. //Bar 1
  316. note[0]=0x40;
  317. note[1]=0x3e;
  318. note[2]=0x3c;
  319. note[3]=0x3e;
  320. //Bar 2
  321. note[4]=0x40;
  322. note[5]=0x40;
  323. note[6]=0x40;
  324. //Bar 3
  325. note[7]=0x3e;
  326. note[8]=0x3e;
  327. note[9]=0x3e;
  328. //Bar 4
  329. note[10]=0x40;
  330. note[11]=0x43;
  331. note[12]=0x43;
  332. //Bar 5
  333. note[13]=0x40;
  334. note[14]=0x3e;
  335. note[15]=0x3c;
  336. note[16]=0x3e;
  337. //Bar 6
  338. note[17]=0x40;
  339. note[18]=0x40;
  340. note[19]=0x40;
  341. note[20]=0x40;
  342. //Bar 7
  343. note[21]=0x3e;
  344. note[22]=0x3e;
  345. note[23]=0x40;
  346. note[24]=0x3e;
  347. //Bar 8
  348. note[25]=0x3c;
  349. //End song
  350.   }
  351.  }
  352.  void (*SongSeq[x])() =
  353.  {//Bar 1
  354.  [] { playNote(0x40, 3, 1, 414); } ,//Quarter note: E
  355.  [] { playNote(0x3e, 2, 4, 414); } ,//Quarter note: D
  356.  [] { playNote(0x3c, 2, 2, 414); } ,//Quarter note: C
  357.  [] { playNote(0x3e, 2, 4, 414); } ,//Quarter note: D
  358.  //Bar 2
  359. [] {playNote(0x40, 3, 1, 414,1);} ,//Quarter note: E
  360. [] {playNote(0x40, 3, 1, 414,1); },//Quarter note: E
  361. [] {playNote(0x40, 3, 1, 828,1); },//Half note: E
  362. //Bar 3
  363. []{playNote(0x3e, 2, 4, 414,1);}, //Quarter note: D
  364. []{playNote(0x3e, 2, 4, 414,1);}, //Quarter note: D
  365. []{playNote(0x3e, 2, 4, 828,1);}, //Half note: D
  366. //Bar 4
  367. []{playNote(0x40, 3, 1, 414);}, //Quarter note: E
  368. []{playNote(0x43, 3, 4, 414,1);}, //Quarter note: G
  369. []{playNote(0x43, 3, 4, 828,1);}, //Half note: G
  370. //Bar 5
  371. []{playNote(0x40, 3, 1, 414);}, //Quarter note: E
  372. []{playNote(0x3e, 2, 4, 414);}, //Quarter note: D
  373. []{playNote(0x3c, 2, 2, 414);}, //Quarter note: C
  374. []{playNote(0x3e, 2, 4, 414);}, //Quarter note: D
  375. //Bar 6
  376. []{playNote(0x40, 3, 1, 414,1);}, //Quarter note: E
  377. []{playNote(0x40, 3, 1, 414,1);}, //Quarter note: E
  378. []{playNote(0x40, 3, 1, 414,1);}, //Quarter note: E
  379. []{playNote(0x40, 3, 1, 414,1);}, //Quarter note: E
  380. //Bar 7
  381. []{playNote(0x3e, 2, 4, 414,1);}, //Quarter note: D
  382. []{playNote(0x3e, 2, 4, 414,1);}, //Quarter note: D
  383. []{playNote(0x40, 3, 1, 414);}, //Quarter note: E
  384. []{playNote(0x3e, 2, 4, 414);}, //Quarter note: D
  385. //Bar 8
  386. []{playNote(0x3c, 2, 2, 1656);}, //Whole note: C
  387. //End song
  388.  
  389. };
  390.   void showsequence(){
  391.   //Add a new index to the end of the songseq
  392.  SongSeq[largestindex];
  393.  largestindex++;
  394.  //loop through the songseq
  395.  for (int index=0;index<largestindex;index++){
  396.   SongSeq[index]();
  397.  }
  398.   }
  399.  void readsequnce(){
  400.   bool mademistake=false;
  401.   int key;
  402.   for (int index=0;index<largestindex & mademistake== false;index++){
  403.     key=waitForButton(5000);
  404.     if (key==-1|key !=note[index]){
  405.       mademistake=true;
  406.       SongSeq[index]();
  407.       SongSeq[index]();
  408.       state=2;
  409.       }
  410.   }
  411.   }
  412.   int waitForButton(int delay)
  413. {
  414.   int keyPressed = -1;
  415.   int check;
  416.   boolean keyBackUp = false;
  417.  
  418.   currentMillis = millis();      // The number of ms since the program started running
  419.   previousMillis = currentMillis;   // Records the point when we start spinning the loop.
  420.  
  421.   // Keep spinning the loop until "delay" seconds have passed.
  422.   while (currentMillis - previousMillis < delay & keyBackUp == false)
  423.   {
  424.     // Read the button and record when it has been pushed down.
  425.     for(int i = 0; i < 25 & keyBackUp == false; i++)
  426.     {
  427.       if(input[i] != 0)
  428.       {
  429.         keyPressed = input[i];
  430.        
  431.         // Show the LED pushed.
  432.         noteOn(0,keyPressed,100);
  433.       //  digitalWrite(leds[pin], HIGH);
  434.              
  435.         // It is possible the button is still being pushed.
  436.         // This loop spins until the button is let up.
  437.         while (currentMillis - previousMillis < delay & keyBackUp == false)
  438.         {
  439.           check = input[i];
  440.           if(check == 0)
  441.           {
  442.             keyBackUp = true;
  443.           }
  444.           currentMillis = millis();
  445.         }
  446.        
  447.         // Turn the LED pushed off.
  448.         noteOff(0,keyPressed,0);
  449.        // digitalWrite(leds[pin], LOW);
  450.        
  451.         // See if they took to long.
  452.         if(currentMillis - previousMillis > delay)
  453.         {
  454.           keyPressed = -1; // They took to long to let the button up so they lose.
  455.         }
  456.       }
  457.     }
  458.  
  459.     currentMillis = millis();
  460.    
  461.   }
  462.  
  463.   return keyPressed;
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement