Advertisement
WayGroovy

Stacker 1.1 Tiva C Launchpad with MAX7219 4in1

Dec 8th, 2016
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.55 KB | None | 0 0
  1. //We always have to include the library
  2. #include "LedControl.h"
  3.  
  4. /*
  5. Now we need a LedControl to work with.
  6. PD0 is connected to the DataIn
  7. PD1 is connected to the CS
  8. PD2 is connected to CLK
  9. ***** Please set the number of devices you have *****
  10. But the maximum default of 8 MAX72XX wil also work.
  11. */
  12. LedControl lc=LedControl(23,25,24,4);
  13.  
  14. // set pin numbers:
  15. const int buttonPin = PUSH2; // the number of the pushbutton pin
  16. int buttonState = 0; // variable for reading the pushbutton status
  17. int lastButtonState = 0; // previous reading from the input pin
  18. int devices=lc.getDeviceCount();
  19. int row=1, col=1, dir=0, gameOver=0; // dir 0 = left 1 = right
  20.  
  21. /* we always wait a bit between updates of the display */
  22. unsigned long delaytime=192;
  23.  
  24. // Array to keep track of LED state -- mray
  25. unsigned char led_state[8] = {0,0,1,1,1,1,0,0};
  26. unsigned char prev_state[8] = {1,1,1,1,1,1,1,1};
  27. unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
  28. unsigned long debounceDelay = 15; // the debounce time; increase if the output flickers
  29.  
  30.  
  31. // Function to change LED state and store to array -- mray
  32. void setOn(int row, int col);
  33. void setOff(int row, int col);
  34.  
  35. // Controls for the Bar
  36. void drawRow(int row, unsigned char *led_state);
  37. void shiftRow(int row, int dir, unsigned char *led_state);
  38. void clearScreen();
  39.  
  40. void setup() {
  41. pinMode(buttonPin, INPUT_PULLUP);
  42.  
  43. //we have already set the number of devices when we created the LedControl
  44. int devices=lc.getDeviceCount();
  45. //we have to init all devices in a loop
  46. for(int address=0;address<devices;address++) {
  47. /*The MAX72XX is in power-saving mode on startup*/
  48. lc.shutdown(address,false);
  49. /* Set the brightness to a medium values */
  50. lc.setIntensity(address,8);
  51. /* and clear the display */
  52. lc.clearDisplay(address);
  53.  
  54. }
  55.  
  56. bootScreen();
  57. clearScreen();
  58. }
  59.  
  60. void loop() {
  61.  
  62. // draw the row
  63. drawRow(row, led_state);
  64. delay(delaytime);
  65.  
  66. // read the state of the switch into a local variable:
  67. int reading = digitalRead(buttonPin);
  68.  
  69. // check to see if you just pressed the button
  70. // (i.e. the input went from LOW to HIGH), and you've waited
  71. // long enough since the last press to ignore any noise:
  72.  
  73. // If the switch changed, due to noise or pressing:
  74. if (reading != lastButtonState) {
  75. // reset the debouncing timer
  76. lastDebounceTime = millis();
  77. }
  78.  
  79. if ((millis() - lastDebounceTime) > debounceDelay) {
  80. // whatever the reading is at, it's been there for longer
  81. // than the debounce delay, so take it as the actual current state:
  82.  
  83. // if the button state has changed:
  84. if (reading != buttonState) {
  85. buttonState = reading;
  86.  
  87. // only increment the row if the new button state is LOW
  88. if (buttonState == LOW) {
  89.  
  90. // logic to increment row
  91.  
  92. if (memcmp(led_state, prev_state, sizeof(led_state))){ // if new position does not
  93. drawRow(row, led_state); // equal old position
  94. delay(100); // flash the error
  95. drawRow(row, prev_state);
  96. delay(100);
  97. drawRow(row, led_state);
  98. delay(100);
  99. drawRow(row, prev_state);
  100. delay(100);
  101. drawRow(row, led_state);
  102. delay(100);
  103. drawRow(row, prev_state);
  104. delay(100);
  105. drawRow(row, led_state);
  106. delay(100);
  107. drawRow(row, prev_state);
  108. delay(100);
  109. }
  110.  
  111. for(int iBS=0;iBS<8;iBS+=1){
  112. if(led_state[iBS]==1 && prev_state[iBS]==1){
  113. led_state[iBS]=1;
  114. } else {
  115. led_state[iBS]=0;
  116. }
  117. prev_state[iBS]=led_state[iBS];
  118. }
  119. drawRow(row, led_state); //clear hanging leds
  120. row=row+1;
  121. delaytime = delaytime - 4; //speed up
  122. }
  123. }
  124. }
  125.  
  126. // save the reading. Next time through the loop,
  127. // it'll be the lastButtonState:
  128. lastButtonState = reading;
  129.  
  130.  
  131. shiftRow(row, dir, led_state);
  132.  
  133. if (led_state[0] == 1) {
  134. dir=1;
  135. }
  136. if (led_state[7] == 1) {
  137. dir=0;
  138. }
  139.  
  140. gameOver=0;
  141. for (int iGO=0;iGO<8;iGO+=1) {
  142. if (led_state[iGO]==0){
  143. gameOver+=1;
  144. }
  145. }
  146.  
  147.  
  148. if (row==33 || gameOver==8){
  149. gameOverScreen();
  150. row=1;
  151. delaytime=192;
  152. clearScreen();
  153. for(int i=0;i<8;i+=1){
  154. prev_state[i]=1;
  155. led_state[i]=1;
  156. }
  157. led_state[0] = 0;
  158. led_state[1] = 0;
  159. led_state[6] = 0;
  160. led_state[7] = 0;
  161. bootScreen();
  162. }
  163.  
  164. }
  165.  
  166. void drawRow(int row, unsigned char *led_state) {
  167.  
  168. for(int col=0;col<8;col+=1) {
  169. if (led_state[col] == 0) {
  170. // setOn(row, col);
  171. // delay(10);
  172. setOff(row, col);
  173. } else if (led_state[col] == 1) {
  174. // setOff(row, col);
  175. // delay(10);
  176. setOn(row, col);
  177. }
  178. // delay(10);
  179. }
  180. }
  181.  
  182. void shiftRow(int row, int dir, unsigned char *led_state) {
  183. if (dir==0) { // shift left
  184. led_state[0]=led_state[1];
  185. led_state[1]=led_state[2];
  186. led_state[2]=led_state[3];
  187. led_state[3]=led_state[4];
  188. led_state[4]=led_state[5];
  189. led_state[5]=led_state[6];
  190. led_state[6]=led_state[7];
  191. led_state[7]=0;
  192.  
  193. } else { // shift right
  194. // led_state[8]=led_state[7];
  195. led_state[7]=led_state[6];
  196. led_state[6]=led_state[5];
  197. led_state[5]=led_state[4];
  198. led_state[4]=led_state[3];
  199. led_state[3]=led_state[2];
  200. led_state[2]=led_state[1];
  201. led_state[1]=led_state[0];
  202. led_state[0]=0;
  203.  
  204. }
  205. }
  206.  
  207. void clearScreen() {
  208. delay(1000);
  209. for(int ir=0;ir<33;ir+=1) {
  210. for (int ic=0;ic<8;ic+=1) {
  211. setOff (ir, ic);
  212. }
  213. }
  214. }
  215.  
  216. void setOn(int row, int col) {
  217. int address = 0;
  218. if(row>24) {
  219. address = 3;
  220. }
  221. else if(row>16) {
  222. address = 2;
  223. }
  224. else if(row>8) {
  225. address = 1;
  226. }
  227. else {
  228. address = 0;
  229. }
  230. row -= 1;
  231. row = row % 8;
  232. switch(row) {
  233. case 0:
  234. row = 7;
  235. break;
  236. case 1:
  237. row = 6;
  238. break;
  239. case 2:
  240. row = 5;
  241. break;
  242. case 3:
  243. row = 4;
  244. break;
  245. case 4:
  246. row = 3;
  247. break;
  248. case 5:
  249. row = 2;
  250. break;
  251. case 6:
  252. row = 1;
  253. break;
  254. case 7:
  255. row = 0;
  256. break;
  257. }
  258. switch(col) {
  259. case 0:
  260. col = 7;
  261. break;
  262. case 1:
  263. col = 6;
  264. break;
  265. case 2:
  266. col = 5;
  267. break;
  268. case 3:
  269. col = 4;
  270. break;
  271. case 4:
  272. col = 3;
  273. break;
  274. case 5:
  275. col = 2;
  276. break;
  277. case 6:
  278. col = 1;
  279. break;
  280. case 7:
  281. col = 0;
  282. break;
  283. }
  284. lc.setLed(address,col,row,true);
  285. }
  286.  
  287. void setOff(int row, int col) {
  288. int address = 0;
  289. if(row>24) {
  290. address = 3;
  291. }
  292. else if(row>16) {
  293. address = 2;
  294. }
  295. else if(row>8) {
  296. address = 1;
  297. }
  298. else {
  299. address = 0;
  300. }
  301. row -= 1;
  302. row = row % 8;
  303. switch(row) {
  304. case 0:
  305. row = 7;
  306. break;
  307. case 1:
  308. row = 6;
  309. break;
  310. case 2:
  311. row = 5;
  312. break;
  313. case 3:
  314. row = 4;
  315. break;
  316. case 4:
  317. row = 3;
  318. break;
  319. case 5:
  320. row = 2;
  321. break;
  322. case 6:
  323. row = 1;
  324. break;
  325. case 7:
  326. row = 0;
  327. break;
  328. }
  329. switch(col) {
  330. case 0:
  331. col = 7;
  332. break;
  333. case 1:
  334. col = 6;
  335. break;
  336. case 2:
  337. col = 5;
  338. break;
  339. case 3:
  340. col = 4;
  341. break;
  342. case 4:
  343. col = 3;
  344. break;
  345. case 5:
  346. col = 2;
  347. break;
  348. case 6:
  349. col = 1;
  350. break;
  351. case 7:
  352. col = 0;
  353. break;
  354. }
  355. lc.setLed(address,col,row,false);
  356.  
  357. }
  358.  
  359. void bootScreen() {
  360. clearScreen();
  361. unsigned char text[32][8]={
  362. {0,1,1,0,0,0,0,0} ,
  363. {1,0,0,0,0,0,0,0} ,
  364. {0,1,0,0,0,0,0,0} ,
  365. {0,0,1,0,0,0,0,0} ,
  366. {1,1,0,0,0,0,0,0} ,
  367. {0,0,0,0,0,0,0,0} ,
  368. {0,1,1,1,0,0,0,0} ,
  369. {0,0,1,0,0,0,0,0} ,
  370. {0,0,1,1,0,0,0,0} ,
  371. {0,0,0,0,0,0,0,0} ,
  372. {0,0,0,1,1,1,0,0} ,
  373. {0,0,1,0,0,1,0,0} ,
  374. {0,0,1,0,0,1,0,0} ,
  375. {0,0,0,1,1,1,0,0} ,
  376. {0,0,0,0,0,0,0,0} ,
  377. {0,0,0,0,1,1,0,0} ,
  378. {0,0,0,1,0,0,1,0} ,
  379. {0,0,0,1,0,0,0,0} ,
  380. {0,0,0,1,0,0,1,0} ,
  381. {0,0,0,0,1,1,0,0} ,
  382. {0,0,0,0,0,0,0,0} ,
  383. {0,0,0,0,1,0,0,0} ,
  384. {0,0,0,0,1,0,0,0} ,
  385. {0,0,0,0,1,0,1,0} ,
  386. {0,0,0,0,1,1,0,0} ,
  387. {0,0,0,0,1,1,0,0} ,
  388. {0,0,0,0,1,0,1,0} ,
  389. {0,0,0,0,0,0,0,0} ,
  390. {0,0,0,0,0,1,1,0} ,
  391. {0,0,0,0,0,1,0,1} ,
  392. {0,0,0,0,0,1,0,0} ,
  393. {0,0,0,0,0,1,0,0}
  394. };
  395.  
  396. /* now display them one by one with a small delay */
  397. for (int i=0;i<32;i+=1){
  398. drawRow(32-i,text[i]);
  399. delay(50);
  400. }
  401.  
  402. delay(250);
  403. clearScreen();
  404. }
  405.  
  406. void gameOverScreen() {
  407. clearScreen();
  408. unsigned int scrollDelay=250;
  409. /* here is the data for the characters */
  410. unsigned char text1[32][8]={
  411. {0,0,0,0,0,0,0,0} ,
  412. {0,0,1,1,1,1,0,0} ,
  413. {0,1,0,0,0,0,1,0} ,
  414. {0,1,0,0,0,0,0,0} ,
  415. {0,1,0,0,1,1,1,0} ,
  416. {0,1,0,0,0,0,1,0} ,
  417. {0,0,1,1,1,1,1,0} ,
  418. {0,0,0,0,0,0,0,0} ,
  419. {0,0,0,0,0,0,0,0} ,
  420. {0,0,0,1,1,0,0,0} ,
  421. {0,0,1,0,0,1,0,0} ,
  422. {0,0,1,0,0,1,0,0} ,
  423. {0,1,1,1,1,1,1,0} ,
  424. {0,1,0,0,0,0,1,0} ,
  425. {0,1,0,0,0,0,1,0} ,
  426. {0,0,0,0,0,0,0,0} ,
  427. {0,0,0,0,0,0,0,0} ,
  428. {0,1,0,0,0,0,1,0} ,
  429. {0,1,1,0,0,1,1,0} ,
  430. {0,1,1,1,1,1,1,0} ,
  431. {0,1,0,1,1,0,1,0} ,
  432. {0,1,0,0,0,0,1,0} ,
  433. {0,1,0,0,0,0,1,0} ,
  434. {0,0,0,0,0,0,0,0} ,
  435. {0,0,0,0,0,0,0,0} ,
  436. {0,1,1,1,1,1,1,0} ,
  437. {0,1,0,0,0,0,0,0} ,
  438. {0,1,1,1,1,0,0,0} ,
  439. {0,1,1,1,1,0,0,0} ,
  440. {0,1,0,0,0,0,0,0} ,
  441. {0,1,1,1,1,1,1,0} ,
  442. {0,0,0,0,0,0,0,0}
  443. }, text2[32][8]={
  444. {0,0,0,0,0,0,0,0} ,
  445. {0,0,1,1,1,1,0,0} ,
  446. {0,1,0,0,0,0,1,0} ,
  447. {0,1,0,0,0,0,1,0} ,
  448. {0,1,0,0,0,0,1,0} ,
  449. {0,1,0,0,0,0,1,0} ,
  450. {0,0,1,1,1,1,0,0} ,
  451. {0,0,0,0,0,0,0,0} ,
  452. {0,0,0,0,0,0,0,0} ,
  453. {0,1,0,0,0,0,1,0} ,
  454. {0,1,0,0,0,0,1,0} ,
  455. {0,0,1,0,0,1,0,0} ,
  456. {0,0,1,0,0,1,0,0} ,
  457. {0,0,1,0,0,1,0,0} ,
  458. {0,0,0,1,1,0,0,0} ,
  459. {0,0,0,0,0,0,0,0} ,
  460. {0,0,0,0,0,0,0,0} ,
  461. {0,1,1,1,1,1,1,0} ,
  462. {0,1,0,0,0,0,0,0} ,
  463. {0,1,1,1,1,0,0,0} ,
  464. {0,1,1,1,1,0,0,0} ,
  465. {0,1,0,0,0,0,0,0} ,
  466. {0,1,1,1,1,1,1,0} ,
  467. {0,0,0,0,0,0,0,0} ,
  468. {0,0,0,0,0,0,0,0} ,
  469. {0,1,1,1,1,1,0,0} ,
  470. {0,1,0,0,0,0,1,0} ,
  471. {0,1,0,0,0,0,1,0} ,
  472. {0,1,1,1,1,1,0,0} ,
  473. {0,1,0,0,0,0,1,0} ,
  474. {0,1,0,0,0,0,1,0} ,
  475. {0,0,0,0,0,0,0,0}
  476. };
  477. /* now display them one by one with a small delay */
  478. for (int i=0;i<32;i+=1){
  479. drawRow(32-i,text1[i]);
  480. delay(50);
  481. }
  482.  
  483. delay(250);
  484.  
  485. for (int i=0;i<32;i+=1){
  486. drawRow(32-i,text2[i]);
  487. delay(50);
  488. }
  489.  
  490. delay(500);
  491.  
  492. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement