Advertisement
WayGroovy

Stacker 1.0 Tiva C Launchpad with MAX7219 4in1

Dec 8th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.58 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.  
  57. void loop() {
  58.  
  59. // draw the row
  60. drawRow(row, led_state);
  61. delay(delaytime);
  62.  
  63.   // read the state of the switch into a local variable:
  64.   int reading = digitalRead(buttonPin);
  65.  
  66.   // check to see if you just pressed the button
  67.   // (i.e. the input went from LOW to HIGH),  and you've waited
  68.   // long enough since the last press to ignore any noise:
  69.  
  70.   // If the switch changed, due to noise or pressing:
  71.   if (reading != lastButtonState) {
  72.     // reset the debouncing timer
  73.     lastDebounceTime = millis();
  74.   }
  75.  
  76.   if ((millis() - lastDebounceTime) > debounceDelay) {
  77.     // whatever the reading is at, it's been there for longer
  78.     // than the debounce delay, so take it as the actual current state:
  79.  
  80.     // if the button state has changed:
  81.     if (reading != buttonState) {
  82.       buttonState = reading;
  83.  
  84.       // only increment the row if the new button state is LOW
  85.       if (buttonState == LOW) {
  86.  
  87.         // logic to increment row
  88.  
  89.         if (memcmp(led_state, prev_state, sizeof(led_state))){ // if new position does not
  90.           drawRow(row, led_state);                             // equal old position
  91.           delay(100);                                          // flash the error
  92.           drawRow(row, prev_state);
  93.           delay(100);
  94.           drawRow(row, led_state);
  95.           delay(100);
  96.           drawRow(row, prev_state);
  97.           delay(100);
  98.           drawRow(row, led_state);
  99.           delay(100);
  100.           drawRow(row, prev_state);
  101.           delay(100);
  102.           drawRow(row, led_state);
  103.           delay(100);
  104.           drawRow(row, prev_state);
  105.           delay(100);
  106.         }
  107.        
  108.         for(int iBS=0;iBS<8;iBS+=1){
  109.           if(led_state[iBS]==1 && prev_state[iBS]==1){
  110.             led_state[iBS]=1;        
  111.           } else {
  112.             led_state[iBS]=0;
  113.           }
  114.           prev_state[iBS]=led_state[iBS];
  115.         }
  116.         drawRow(row, led_state);        //clear hanging leds
  117.         row=row+1;
  118.         delaytime = delaytime - 4; //speed up
  119.       }
  120.     }
  121.   }
  122.  
  123.   // save the reading.  Next time through the loop,
  124.   // it'll be the lastButtonState:
  125.   lastButtonState = reading;
  126.  
  127.  
  128. shiftRow(row, dir, led_state);
  129.  
  130.     if (led_state[0] == 1) {
  131.       dir=1;
  132.     }
  133.     if (led_state[7] == 1) {
  134.       dir=0;
  135.       }
  136.      
  137. gameOver=0;
  138. for (int iGO=0;iGO<8;iGO+=1) {
  139.   if (led_state[iGO]==0){
  140.     gameOver+=1;
  141.   }
  142. }
  143.  
  144.  
  145. if (row==33 || gameOver==8){
  146.   row=1;
  147.   delaytime=192;
  148.   clearScreen();
  149.   for(int i=0;i<8;i+=1){
  150.     prev_state[i]=1;
  151.     led_state[i]=1;
  152.   }
  153.   led_state[0] = 0;
  154.   led_state[1] = 0;
  155.   led_state[6] = 0;
  156.   led_state[7] = 0;
  157.   }
  158.  
  159. }
  160.  
  161. void drawRow(int row, unsigned char *led_state) {
  162.  
  163.   for(int col=0;col<8;col+=1) {
  164.     if (led_state[col] == 0) {
  165. //      setOn(row, col);
  166. //      delay(10);
  167.       setOff(row, col);
  168.     } else if (led_state[col] == 1) {
  169. //      setOff(row, col);
  170. //      delay(10);
  171.       setOn(row, col);
  172.     }
  173. //    delay(10);
  174.   }
  175. }
  176.  
  177. void shiftRow(int row, int dir, unsigned char *led_state) {
  178.   if (dir==0) { // shift left
  179.     led_state[0]=led_state[1];
  180.     led_state[1]=led_state[2];
  181.     led_state[2]=led_state[3];
  182.     led_state[3]=led_state[4];
  183.     led_state[4]=led_state[5];
  184.     led_state[5]=led_state[6];
  185.     led_state[6]=led_state[7];
  186.     led_state[7]=0;
  187.  
  188.   } else {  // shift right
  189. //    led_state[8]=led_state[7];
  190.     led_state[7]=led_state[6];
  191.     led_state[6]=led_state[5];
  192.     led_state[5]=led_state[4];
  193.     led_state[4]=led_state[3];
  194.     led_state[3]=led_state[2];
  195.     led_state[2]=led_state[1];
  196.     led_state[1]=led_state[0];
  197.     led_state[0]=0;
  198.  
  199.   }
  200. }
  201.  
  202. void clearScreen() {
  203.   delay(1000);
  204.   for(int ir=0;ir<33;ir+=1) {
  205.     for (int ic=0;ic<8;ic+=1) {
  206.       setOff (ir, ic);
  207.     }
  208.   }
  209. }
  210.  
  211. void setOn(int row, int col) {
  212.   int address = 0;
  213.   if(row>24) {
  214.     address = 3;
  215.   }
  216.   else if(row>16) {
  217.     address = 2;
  218.   }
  219.   else if(row>8) {
  220.     address = 1;
  221.   }
  222.   else {
  223.     address = 0;
  224.   }
  225.   row -= 1;
  226.   row = row % 8;
  227.   switch(row) {
  228.     case 0:
  229.       row = 7;
  230.       break;
  231.     case 1:
  232.       row = 6;
  233.       break;
  234.     case 2:
  235.       row = 5;
  236.       break;
  237.     case 3:
  238.       row = 4;
  239.       break;
  240.     case 4:
  241.       row = 3;
  242.       break;
  243.     case 5:
  244.       row = 2;
  245.       break;
  246.     case 6:
  247.       row = 1;
  248.       break;
  249.     case 7:
  250.       row = 0;
  251.       break;
  252.   }
  253.   switch(col) {
  254.     case 0:
  255.       col = 7;
  256.       break;
  257.     case 1:
  258.       col = 6;
  259.       break;
  260.     case 2:
  261.       col = 5;
  262.       break;
  263.     case 3:
  264.       col = 4;
  265.       break;
  266.     case 4:
  267.       col = 3;
  268.       break;
  269.     case 5:
  270.       col = 2;
  271.       break;
  272.     case 6:
  273.       col = 1;
  274.       break;
  275.     case 7:
  276.       col = 0;
  277.       break;
  278.   }
  279.   lc.setLed(address,col,row,true);
  280. }
  281.  
  282. void setOff(int row, int col) {
  283.   int address = 0;
  284.   if(row>24) {
  285.     address = 3;
  286.   }
  287.   else if(row>16) {
  288.     address = 2;
  289.   }
  290.   else if(row>8) {
  291.     address = 1;
  292.   }
  293.   else {
  294.     address = 0;
  295.   }
  296.   row -= 1;
  297.   row = row % 8;
  298.   switch(row) {
  299.     case 0:
  300.       row = 7;
  301.       break;
  302.     case 1:
  303.       row = 6;
  304.       break;
  305.     case 2:
  306.       row = 5;
  307.       break;
  308.     case 3:
  309.       row = 4;
  310.       break;
  311.     case 4:
  312.       row = 3;
  313.       break;
  314.     case 5:
  315.       row = 2;
  316.       break;
  317.     case 6:
  318.       row = 1;
  319.       break;
  320.     case 7:
  321.       row = 0;
  322.       break;
  323.   }
  324.   switch(col) {
  325.     case 0:
  326.       col = 7;
  327.       break;
  328.     case 1:
  329.       col = 6;
  330.       break;
  331.     case 2:
  332.       col = 5;
  333.       break;
  334.     case 3:
  335.       col = 4;
  336.       break;
  337.     case 4:
  338.       col = 3;
  339.       break;
  340.     case 5:
  341.       col = 2;
  342.       break;
  343.     case 6:
  344.       col = 1;
  345.       break;
  346.     case 7:
  347.       col = 0;
  348.       break;
  349.   }
  350.   lc.setLed(address,col,row,false);
  351.  
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement