Advertisement
nitrodog96

tetris code final

Jan 23rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.23 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. int leftOneLED = 8;
  4. int leftTwoLED = 9;
  5. int leftThreeLED = 2;
  6. int leftFourLED = 3;
  7. int rightOneLED = 4;
  8. int rightTwoLED = 5;
  9. int rightThreeLED = 6;
  10. int rightFourLED = 7;
  11.  
  12. int row01 = 52;
  13. int row02 = 42;
  14. int row03 = 53;
  15. int row04 = 46;
  16. int row05 = 39;
  17. int row06 = 51;
  18. int row07 = 41;
  19. int row08 = 47;
  20. int row09 = 36;
  21. int row10 = 26;
  22. int row11 = 37;
  23. int row12 = 30;
  24. int row13 = 23;
  25. int row14 = 35;
  26. int row15 = 25;
  27. int row16 = 31;
  28.  
  29. int colBot01 = 44;
  30. int colBot02 = 43;
  31. int colBot03 = 45;
  32. int colBot04 = 50;
  33. int colBot05 = 49;
  34. int colBot06 = 48;
  35. int colBot07 = 40;
  36. int colBot08 = 38;
  37. int colTop01 = 28;
  38. int colTop02 = 27;
  39. int colTop03 = 29;
  40. int colTop04 = 34;
  41. int colTop05 = 33;
  42. int colTop06 = 32;
  43. int colTop07 = 24;
  44. int colTop08 = 22;
  45.  
  46. bool pieceI[8] = {true, true, true, true, false, false, false, false};
  47. bool pieceJ[8] = {false, false, false, true, false, true, true, true};
  48. bool pieceL[8] = {false, true, true, true, false, false, false, true};
  49. bool pieceO[8] = {false, false, true, true, false, false, true, true};
  50. bool pieceS[8] = {false, true, true, false, false, false, true, true};
  51. bool pieceT[8] = {false, true, true, true, false, false, true, false};
  52. bool pieceZ[8] = {false, false, true, true, false, true, true, false};
  53.  
  54. const byte rowsBot[] = {row01,row02,row03,row04,row05,row06,row07,row08};
  55. const byte rowsTop[] = {row09,row10,row11,row12,row13,row14,row15,row16};
  56. const byte colsBot[] = {colBot08,colBot07,colBot06,colBot05,colBot04,colBot03,colBot02,colBot01};
  57. const byte colsTop[] = {colTop08,colTop07,colTop06,colTop05,colTop04,colTop03,colTop02,colTop01};
  58.  
  59. byte bytes[256] = {B11111111,B11111110,B11111101,B11111100,B11111011,B11111010,B11111001,B11111000,B11110111,B11110110,B11110101,B11110100,B11110011,B11110010,B11110001,B11110000,
  60. B11101111,B11101110,B11101101,B11101100,B11101011,B11101010,B11101001,B11101000,B11100111,B11100110,B11100101,B11100100,B11100011,B11100010,B11100001,B11100000,
  61. B11011111,B11011110,B11011101,B11011100,B11011011,B11011010,B11011001,B11011000,B11010111,B11010110,B11010101,B11010100,B11010011,B11010010,B11010001,B11010000,
  62. B11001111,B11001110,B11001101,B11001100,B11001011,B11001010,B11001001,B11001000,B11000111,B11000110,B11000101,B11000100,B11000011,B11000010,B11000001,B11000000,
  63. B10111111,B10111110,B10111101,B10111100,B10111011,B10111010,B10111001,B10111000,B10110111,B10110110,B10110101,B10110100,B10110011,B10110010,B10110001,B10110000,
  64. B10101111,B10101110,B10101101,B10101100,B10101011,B10101010,B10101001,B10101000,B10100111,B10100110,B10100101,B10100100,B10100011,B10100010,B10100001,B10100000,
  65. B10011111,B10011110,B10011101,B10011100,B10011011,B10011010,B10011001,B10011000,B10010111,B10010110,B10010101,B10010100,B10010011,B10010010,B10010001,B10010000,
  66. B10001111,B10001110,B10001101,B10001100,B10001011,B10001010,B10001001,B10001000,B10000111,B10000110,B10000101,B10000100,B10000011,B10000010,B10000001,B10000000,
  67. B01111111,B01111110,B01111101,B01111100,B01111011,B01111010,B01111001,B01111000,B01110111,B01110110,B01110101,B01110100,B01110011,B01110010,B01110001,B01110000,
  68. B01101111,B01101110,B01101101,B01101100,B01101011,B01101010,B01101001,B01101000,B01100111,B01100110,B01100101,B01100100,B01100011,B01100010,B01100001,B01100000,
  69. B01011111,B01011110,B01011101,B01011100,B01011011,B01011010,B01011001,B01011000,B01010111,B01010110,B01010101,B01010100,B01010011,B01010010,B01010001,B01010000,
  70. B01001111,B01001110,B01001101,B01001100,B01001011,B01001010,B01001001,B01001000,B01000111,B01000110,B01000101,B01000100,B01000011,B01000010,B01000001,B01000000,
  71. B00111111,B00111110,B00111101,B00111100,B00111011,B00111010,B00111001,B00111000,B00110111,B00110110,B00110101,B00110100,B00110011,B00110010,B00110001,B00110000,
  72. B00101111,B00101110,B00101101,B00101100,B00101011,B00101010,B00101001,B00101000,B00100111,B00100110,B00100101,B00100100,B00100011,B00100010,B00100001,B00100000,
  73. B00011111,B00011110,B00011101,B00011100,B00011011,B00011010,B00011001,B00011000,B00010111,B00010110,B00010101,B00010100,B00010011,B00010010,B00010001,B00010000,
  74. B00001111,B00001110,B00001101,B00001100,B00001011,B00001010,B00001001,B00001000,B00000111,B00000110,B00000101,B00000100,B00000011,B00000010,B00000001,B00000000
  75. };
  76.  
  77. byte topDisplay[8] = {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111};
  78. byte botDisplay[8] = {B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111};
  79.  
  80. int topDisplayInts[8] = {0,0,0,0,0,0,0,0};
  81. int botDisplayInts[8] = {0,0,0,0,0,0,0,0};
  82.  
  83. int pieceCoords[8] = {-1,-1,-1,-1,-1,-1,-1,-1};
  84.  
  85. bool colsMatrix[8][16] = {
  86. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  87. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  88. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  89. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  90. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  91. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  92. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  93. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}
  94. };
  95.  
  96. bool lockedMatrix[8][16] = {
  97. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  98. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  99. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  100. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  101. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  102. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  103. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false},
  104. {false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false}
  105. };
  106.  
  107. String curPiece = "";
  108. String nextPiece = "";
  109.  
  110. int fallSpeed = 18; //frames per line dropped
  111.  
  112. int frLeftPressed = -1;
  113. int frRightPressed = -1;
  114. int dasFrame = -1;
  115.  
  116. void setup() {
  117. Serial.begin(9600);
  118. randomSeed(analogRead(A0));
  119.  
  120. // put your setup code here, to run once:
  121. int i;
  122. int a;
  123. for(i=2;i<10;i++)
  124. {
  125. pinMode(i, OUTPUT);
  126. }
  127. for(a=22;a<54;a++)
  128. {
  129. pinMode(a, OUTPUT);
  130. }
  131.  
  132. pinMode(11, INPUT);
  133. pinMode(12, INPUT);
  134. pinMode(13, INPUT);
  135.  
  136. nextPiece = generateNextPiece();
  137. int failed = spawnPiece();
  138. }
  139.  
  140. void loop() {
  141. // put your main code here, to run repeatedly:
  142. //drawScreen(topDisplay, false);
  143. //nextPiece = generateNextPiece();
  144. //displayNextPiece(nextPiece);
  145.  
  146. drawScreen(topDisplay, false);
  147. drawScreen(botDisplay, true);
  148.  
  149. displayNextPiece(nextPiece);
  150.  
  151. int frame = millis() * 60 / 1000 + 1;
  152.  
  153. int failed;
  154.  
  155. //DAS!!!!
  156. //15 frame startup
  157. //With DAS, 6 frames per shift
  158. if(digitalRead(11) == 1)
  159. {
  160. //Right
  161. frLeftPressed = -1;
  162. if(frRightPressed == -1)
  163. {
  164. int a;
  165. bool canShift = true;
  166. for(a=0;a<4;a++)
  167. {
  168. if(pieceCoords[2 * a] == 0 || lockedMatrix[pieceCoords[2 * a] - 1][pieceCoords[2 * a + 1]])
  169. {
  170. canShift = false;
  171. }
  172. }
  173.  
  174. if(canShift)
  175. {
  176. Serial.println("Shift");
  177. shiftMatrices(true);
  178. pieceCoords[0] -= 1;
  179. pieceCoords[2] -= 1;
  180. pieceCoords[4] -= 1;
  181. pieceCoords[6] -= 1;
  182. updateDisplay();
  183. }
  184. frRightPressed = frame;
  185. }
  186. else
  187. {
  188. if(frame - frRightPressed >= 15 && dasFrame == -1)
  189. {
  190. dasFrame = frame;
  191. }
  192. else if(dasFrame != -1)
  193. {
  194. //in DAS
  195. if((frame - dasFrame) % 6 == 0)
  196. {
  197. int a;
  198. bool canShift = true;
  199. for(a=0;a<4;a++)
  200. {
  201. if(pieceCoords[2 * a] == 0 || lockedMatrix[pieceCoords[2 * a] - 1][pieceCoords[2 * a + 1]])
  202. {
  203. canShift = false;
  204. }
  205. }
  206.  
  207. if(canShift)
  208. {
  209. Serial.println("Shift");
  210. shiftMatrices(true);
  211. pieceCoords[0] -= 1;
  212. pieceCoords[2] -= 1;
  213. pieceCoords[4] -= 1;
  214. pieceCoords[6] -= 1;
  215. updateDisplay();
  216. }
  217. }
  218. }
  219. }
  220. }
  221. else if(digitalRead(12) == 1)
  222. {
  223. //Left
  224. frRightPressed = -1;
  225. if(frLeftPressed == -1)
  226. {
  227. int a;
  228. bool canShift = true;
  229. for(a=0;a<4;a++)
  230. {
  231. if(pieceCoords[2 * a] == 7 || lockedMatrix[pieceCoords[2 * a] + 1][pieceCoords[2 * a + 1]])
  232. {
  233. canShift = false;
  234. }
  235. }
  236.  
  237. if(canShift)
  238. {
  239. Serial.println("Shift");
  240. shiftMatrices(false);
  241. pieceCoords[0] += 1;
  242. pieceCoords[2] += 1;
  243. pieceCoords[4] += 1;
  244. pieceCoords[6] += 1;
  245. updateDisplay();
  246. }
  247. frLeftPressed = frame;
  248. }
  249. else
  250. {
  251. if(frame - frLeftPressed >= 15 && dasFrame == -1)
  252. {
  253. dasFrame = frame;
  254. }
  255. else if(dasFrame != -1)
  256. {
  257. //in DAS
  258. if((frame - dasFrame) % 6 == 0)
  259. {
  260. int a;
  261. bool canShift = true;
  262. for(a=0;a<4;a++)
  263. {
  264. if(pieceCoords[2 * a] == 7 || lockedMatrix[pieceCoords[2 * a] + 1][pieceCoords[2 * a + 1]])
  265. {
  266. canShift = false;
  267. }
  268. }
  269.  
  270. if(canShift)
  271. {
  272. Serial.println("Shift");
  273. shiftMatrices(false);
  274. pieceCoords[0] += 1;
  275. pieceCoords[2] += 1;
  276. pieceCoords[4] += 1;
  277. pieceCoords[6] += 1;
  278. updateDisplay();
  279. }
  280. }
  281. }
  282. }
  283. }
  284. else
  285. {
  286. frLeftPressed = -1;
  287. frRightPressed = -1;
  288. dasFrame = -1;
  289. }
  290.  
  291. if(frame % fallSpeed == 0)
  292. {
  293. int d = dropPiece();
  294. clearLines();
  295. if(d == 1)
  296. {
  297. failed = spawnPiece();
  298. }
  299. }
  300. }
  301.  
  302. void clearLines()
  303. {
  304. bool cleared[16] = {true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true};
  305. bool someToClear = false;
  306. int n = 0;
  307.  
  308. int i;
  309. int l;
  310. for(i=0;i<16;i++)
  311. {
  312. for(l=0;l<8;l++)
  313. {
  314. if(!lockedMatrix[l][i])
  315. {
  316. cleared[i] = false;
  317. }
  318. }
  319. if(cleared[i])
  320. {
  321. someToClear = true;
  322. }
  323. }
  324.  
  325. if(someToClear)
  326. {
  327. int j;
  328. int k;
  329. int n = 1;
  330. int seq = 0;
  331. for(k=15;k>=1;k--)
  332. {
  333. if(cleared[k - 1])
  334. {
  335. n++;
  336. if(seq > 0)
  337. {
  338. seq--;
  339. }
  340. if(cleared[k - 2])
  341. {
  342. n++;
  343. seq++;
  344. if(cleared[k - 3])
  345. {
  346. n++;
  347. seq++;
  348. }
  349. }
  350. }
  351. if(n>0)
  352. {
  353. for(j=0;j<8;j++)
  354. {
  355. if(k - n - seq < 0)
  356. {
  357. colsMatrix[j][k] = false;
  358. lockedMatrix[j][k] = false;
  359. }
  360. else
  361. {
  362. colsMatrix[j][k] = colsMatrix[j][k - n - seq];
  363. lockedMatrix[j][k] = lockedMatrix[j][k - n - seq];
  364. }
  365. }
  366. }
  367. }
  368. int m;
  369. for(m=0;m<8;m++)
  370. {
  371. colsMatrix[m][0] = false;
  372. lockedMatrix[m][0] = false;
  373. }
  374. }
  375. updateDisplay();
  376. }
  377.  
  378. void shiftMatrices(bool R)
  379. {
  380. if(R)
  381. {
  382. int i;
  383. int j;
  384. for(i=0;i<4;i++)
  385. {
  386. colsMatrix[pieceCoords[2 * i] - 1][pieceCoords[2 * i + 1]] = colsMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]];
  387. lockedMatrix[pieceCoords[2 * i] - 1][pieceCoords[2 * i + 1]] = lockedMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]];
  388. colsMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]] = false;
  389. lockedMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]] = false;
  390. }
  391. }
  392. else
  393. {
  394. int i;
  395. int j;
  396. for(i=3;i>=0;i--)
  397. {
  398. colsMatrix[pieceCoords[2 * i] + 1][pieceCoords[2 * i + 1]] = colsMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]];
  399. lockedMatrix[pieceCoords[2 * i] + 1][pieceCoords[2 * i + 1]] = lockedMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]];
  400. colsMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]] = false;
  401. lockedMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]] = false;
  402. }
  403. }
  404. }
  405.  
  406. void dropMatrices()
  407. {
  408. int i;
  409. int j;
  410. for(i=0;i<8;i++)
  411. {
  412. for(j=15;j>=1;j--)
  413. {
  414. if(!lockedMatrix[i][j] && !lockedMatrix[i][j - 1])
  415. {
  416. colsMatrix[i][j] = colsMatrix[i][j - 1];
  417. lockedMatrix[i][j] = lockedMatrix[i][j - 1];
  418. }
  419. }
  420. colsMatrix[i][0] = false;
  421. lockedMatrix[i][0] = false;
  422. }
  423. }
  424.  
  425. int dropPiece()
  426. {
  427. delay(1);
  428. bool canDrop = true;
  429. if(pieceCoords[1] == 15 || pieceCoords[3] == 15 || pieceCoords[5] == 15 || pieceCoords[7] == 15)
  430. {
  431. canDrop = false;
  432. }
  433. else if(lockedMatrix[pieceCoords[0]][pieceCoords[1] + 1] || lockedMatrix[pieceCoords[2]][pieceCoords[3] + 1] ||
  434. lockedMatrix[pieceCoords[4]][pieceCoords[5] + 1] || lockedMatrix[pieceCoords[6]][pieceCoords[7] + 1])
  435. {
  436. canDrop = false;
  437. }
  438.  
  439. if(canDrop)
  440. {
  441. pieceCoords[1] += 1;
  442. pieceCoords[3] += 1;
  443. pieceCoords[5] += 1;
  444. pieceCoords[7] += 1;
  445. }
  446. else
  447. {
  448. int i;
  449. for(i=0;i<4;i++)
  450. {
  451. lockedMatrix[pieceCoords[2 * i]][pieceCoords[2 * i + 1]] = true;
  452. }
  453. updateDisplay();
  454. return 1;
  455. }
  456.  
  457. dropMatrices();
  458. updateDisplay();
  459. return 0;
  460. }
  461.  
  462. void updateDisplay()
  463. {
  464. int k;
  465. int l;
  466. for(k=0;k<8;k++)
  467. {
  468. topDisplayInts[k] = 0;
  469. botDisplayInts[k] = 0;
  470. for(l=0;l<8;l++)
  471. {
  472. if(colsMatrix[k][l])
  473. {
  474. topDisplayInts[k] += 128 / pow(2, l);
  475. }
  476. if(colsMatrix[k][l + 8])
  477. {
  478. botDisplayInts[k] += 128 / pow(2, l);
  479. }
  480. }
  481. topDisplay[k] = bytes[topDisplayInts[k]];
  482. botDisplay[k] = bytes[botDisplayInts[k]];
  483. }
  484. }
  485.  
  486. int spawnPiece()
  487. {
  488. int returnOut = 0;
  489.  
  490. Serial.println("Spawning " + nextPiece + " piece.");
  491. curPiece = nextPiece;
  492. /*if(nextPiece == "I")
  493. {
  494. if(colsMatrix[2][0] || colsMatrix[3][0] || colsMatrix[4][0] || colsMatrix[5][0])
  495. {
  496. returnOut = 1;
  497. }
  498. colsMatrix[2][0] = true;
  499. colsMatrix[3][0] = true;
  500. colsMatrix[4][0] = true;
  501. colsMatrix[5][0] = true;
  502.  
  503. int i;
  504. for(i=0;i<8;i++)
  505. {
  506. if(i % 2 == 0)
  507. {
  508. pieceCoords[i] = i / 2 + 2;
  509. }
  510. else
  511. {
  512. pieceCoords[i] = 0;
  513. }
  514. }
  515. }
  516. if(nextPiece == "J")
  517. {
  518. if(colsMatrix[2][0] || colsMatrix[2][1] || colsMatrix[3][0] || colsMatrix[4][0])
  519. {
  520. returnOut = 1;
  521. }
  522. colsMatrix[2][0] = true;
  523. colsMatrix[2][1] = true;
  524. colsMatrix[3][0] = true;
  525. colsMatrix[4][0] = true;
  526.  
  527. pieceCoords[0] = 2;
  528. pieceCoords[1] = 0;
  529. pieceCoords[2] = 2;
  530. pieceCoords[3] = 1;
  531. pieceCoords[4] = 3;
  532. pieceCoords[5] = 0;
  533. pieceCoords[6] = 4;
  534. pieceCoords[7] = 0;
  535. }
  536. if(nextPiece == "L")
  537. {
  538. if(colsMatrix[2][0] || colsMatrix[3][0] || colsMatrix[4][0] || colsMatrix[4][1])
  539. {
  540. returnOut = 1;
  541. }
  542. colsMatrix[2][0] = true;
  543. colsMatrix[3][0] = true;
  544. colsMatrix[4][0] = true;
  545. colsMatrix[4][1] = true;
  546.  
  547. pieceCoords[0] = 2;
  548. pieceCoords[1] = 0;
  549. pieceCoords[2] = 3;
  550. pieceCoords[3] = 0;
  551. pieceCoords[4] = 4;
  552. pieceCoords[5] = 0;
  553. pieceCoords[6] = 4;
  554. pieceCoords[7] = 1;
  555. }*/
  556. if(nextPiece == "O")
  557. {
  558. if(colsMatrix[3][0] || colsMatrix[3][1] || colsMatrix[4][0] || colsMatrix[4][1])
  559. {
  560. returnOut = 1;
  561. }
  562. colsMatrix[3][0] = true;
  563. colsMatrix[3][1] = true;
  564. colsMatrix[4][0] = true;
  565. colsMatrix[4][1] = true;
  566.  
  567. pieceCoords[0] = 3;
  568. pieceCoords[1] = 0;
  569. pieceCoords[2] = 3;
  570. pieceCoords[3] = 1;
  571. pieceCoords[4] = 4;
  572. pieceCoords[5] = 0;
  573. pieceCoords[6] = 4;
  574. pieceCoords[7] = 1;
  575. }
  576. /*if(nextPiece == "S")
  577. {
  578. if(colsMatrix[2][0] || colsMatrix[3][0] || colsMatrix[3][1]| colsMatrix[4][1])
  579. {
  580. returnOut = 1;
  581. }
  582. colsMatrix[2][0] = true;
  583. colsMatrix[3][0] = true;
  584. colsMatrix[3][1] = true;
  585. colsMatrix[4][1] = true;
  586.  
  587. pieceCoords[0] = 2;
  588. pieceCoords[1] = 0;
  589. pieceCoords[2] = 3;
  590. pieceCoords[3] = 0;
  591. pieceCoords[4] = 3;
  592. pieceCoords[5] = 1;
  593. pieceCoords[6] = 4;
  594. pieceCoords[7] = 1;
  595. }
  596. if(nextPiece == "T")
  597. {
  598. if(colsMatrix[2][0] || colsMatrix[3][0] || colsMatrix[3][1] || colsMatrix[4][0])
  599. {
  600. returnOut = 1;
  601. }
  602. colsMatrix[2][0] = true;
  603. colsMatrix[3][0] = true;
  604. colsMatrix[3][1] = true;
  605. colsMatrix[4][0] = true;
  606.  
  607. pieceCoords[0] = 2;
  608. pieceCoords[1] = 0;
  609. pieceCoords[2] = 3;
  610. pieceCoords[3] = 0;
  611. pieceCoords[4] = 3;
  612. pieceCoords[5] = 1;
  613. pieceCoords[6] = 4;
  614. pieceCoords[7] = 0;
  615. }
  616. if(nextPiece == "Z")
  617. {
  618. if(colsMatrix[2][1] || colsMatrix[3][0] || colsMatrix[3][1] || colsMatrix[4][0])
  619. {
  620. returnOut = 1;
  621. }
  622. colsMatrix[2][1] = true;
  623. colsMatrix[3][0] = true;
  624. colsMatrix[3][1] = true;
  625. colsMatrix[4][0] = true;
  626.  
  627. pieceCoords[0] = 2;
  628. pieceCoords[1] = 1;
  629. pieceCoords[2] = 3;
  630. pieceCoords[3] = 0;
  631. pieceCoords[4] = 3;
  632. pieceCoords[5] = 1;
  633. pieceCoords[6] = 4;
  634. pieceCoords[7] = 0;
  635. }*/
  636.  
  637. updateDisplay();
  638.  
  639. nextPiece = generateNextPiece();
  640.  
  641. return returnOut;
  642. }
  643.  
  644. void drawScreen(byte buffer2[], bool bottom) // FIGURE OUT PINS FOR MATRICES
  645. {
  646. if(bottom)
  647. {
  648. // Turn on each row in series
  649. for (byte i = 0; i < 8; i++) // count next row
  650. {
  651. digitalWrite(rowsBot[i], HIGH); //initiate whole row
  652. for (byte a = 0; a < 8; a++) // count next row
  653. {
  654. // if You set (~buffer2[i] >> a) then You will have positive
  655. digitalWrite(colsBot[a], (buffer2[i] >> a) & 0x01); // initiate whole column
  656.  
  657. delayMicroseconds(100); // uncoment deley for diferent speed of display
  658. //delayMicroseconds(1000);
  659. //delay(10);
  660. //delay(100);
  661.  
  662. digitalWrite(colsBot[a], 1); // reset whole column
  663. }
  664. digitalWrite(rowsBot[i], LOW); // reset whole row
  665. // otherwise last row will intersect with next row
  666. }
  667. }
  668. else
  669. {
  670. // Turn on each row in series
  671. for (byte i = 0; i < 8; i++) // count next row
  672. {
  673. digitalWrite(rowsTop[i], HIGH); //initiate whole row
  674. for (byte a = 0; a < 8; a++) // count next row
  675. {
  676. // if You set (~buffer2[i] >> a) then You will have positive
  677. digitalWrite(colsTop[a], (buffer2[i] >> a) & 0x01); // initiate whole column
  678.  
  679. delayMicroseconds(100); // uncoment deley for diferent speed of display
  680. //delayMicroseconds(1000);
  681. //delay(10);
  682. //delay(100);
  683.  
  684. digitalWrite(colsTop[a], 1); // reset whole column
  685. }
  686. digitalWrite(rowsTop[i], LOW); // reset whole row
  687. // otherwise last row will intersect with next row
  688. }
  689. }
  690. }
  691.  
  692. String generateNextPiece()
  693. {
  694. /*String pieces[7] = {"I", "J", "L", "O", "S", "T", "Z"};
  695. long randNumber = random(0,714) / 102;
  696. int randIndex = (int) floor(randNumber);
  697. String nP = pieces[randIndex];
  698. return nP;*/
  699. return "O";
  700. }
  701.  
  702. void displayNextPiece(String piece)
  703. {
  704. int i;
  705.  
  706. if(piece == "I")
  707. {
  708. for(i=0;i<8;i++)
  709. {
  710. if(pieceI[i])
  711. {
  712. if(i == 0 || i == 1)
  713. {
  714. digitalWrite(i + 8, HIGH);
  715. }
  716. else
  717. {
  718. digitalWrite(i, HIGH);
  719. }
  720. }
  721. else
  722. {
  723. if(i == 0 || i == 1)
  724. {
  725. digitalWrite(i + 8, LOW);
  726. }
  727. else
  728. {
  729. digitalWrite(i, LOW);
  730. }
  731. }
  732. }
  733. }
  734. if(piece == "J")
  735. {
  736. for(i=0;i<8;i++)
  737. {
  738. if(pieceJ[i])
  739. {
  740. if(i == 0 || i == 1)
  741. {
  742. digitalWrite(i + 8, HIGH);
  743. }
  744. else
  745. {
  746. digitalWrite(i, HIGH);
  747. }
  748. }
  749. else
  750. {
  751. if(i == 0 || i == 1)
  752. {
  753. digitalWrite(i + 8, LOW);
  754. }
  755. else
  756. {
  757. digitalWrite(i, LOW);
  758. }
  759. }
  760. }
  761. }
  762. if(piece == "L")
  763. {
  764. for(i=0;i<8;i++)
  765. {
  766. if(pieceL[i])
  767. {
  768. if(i == 0 || i == 1)
  769. {
  770. digitalWrite(i + 8, HIGH);
  771. }
  772. else
  773. {
  774. digitalWrite(i, HIGH);
  775. }
  776. }
  777. else
  778. {
  779. if(i == 0 || i == 1)
  780. {
  781. digitalWrite(i + 8, LOW);
  782. }
  783. else
  784. {
  785. digitalWrite(i, LOW);
  786. }
  787. }
  788. }
  789. }
  790. if(piece == "O")
  791. {
  792. for(i=0;i<8;i++)
  793. {
  794. if(pieceO[i])
  795. {
  796. if(i == 0 || i == 1)
  797. {
  798. digitalWrite(i + 8, HIGH);
  799. }
  800. else
  801. {
  802. digitalWrite(i, HIGH);
  803. }
  804. }
  805. else
  806. {
  807. if(i == 0 || i == 1)
  808. {
  809. digitalWrite(i + 8, LOW);
  810. }
  811. else
  812. {
  813. digitalWrite(i, LOW);
  814. }
  815. }
  816. }
  817. }
  818. if(piece == "S")
  819. {
  820. for(i=0;i<8;i++)
  821. {
  822. if(pieceS[i])
  823. {
  824. if(i == 0 || i == 1)
  825. {
  826. digitalWrite(i + 8, HIGH);
  827. }
  828. else
  829. {
  830. digitalWrite(i, HIGH);
  831. }
  832. }
  833. else
  834. {
  835. if(i == 0 || i == 1)
  836. {
  837. digitalWrite(i + 8, LOW);
  838. }
  839. else
  840. {
  841. digitalWrite(i, LOW);
  842. }
  843. }
  844. }
  845. }
  846. if(piece == "T")
  847. {
  848. for(i=0;i<8;i++)
  849. {
  850. if(pieceT[i])
  851. {
  852. if(i == 0 || i == 1)
  853. {
  854. digitalWrite(i + 8, HIGH);
  855. }
  856. else
  857. {
  858. digitalWrite(i, HIGH);
  859. }
  860. }
  861. else
  862. {
  863. if(i == 0 || i == 1)
  864. {
  865. digitalWrite(i + 8, LOW);
  866. }
  867. else
  868. {
  869. digitalWrite(i, LOW);
  870. }
  871. }
  872. }
  873. }
  874. if(piece == "Z")
  875. {
  876. for(i=0;i<8;i++)
  877. {
  878. if(pieceZ[i])
  879. {
  880. if(i == 0 || i == 1)
  881. {
  882. digitalWrite(i + 8, HIGH);
  883. }
  884. else
  885. {
  886. digitalWrite(i, HIGH);
  887. }
  888. }
  889. else
  890. {
  891. if(i == 0 || i == 1)
  892. {
  893. digitalWrite(i + 8, LOW);
  894. }
  895. else
  896. {
  897. digitalWrite(i, LOW);
  898. }
  899. }
  900. }
  901. }
  902. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement