hendriawan

matrix max7219 v10

Feb 8th, 2019
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://xantorohara.github.io/led-matrix-editor/#ff00000000000000
  2. //http://robojax.com/learn/arduino/8x8LED/
  3. //We always have to include the library
  4. #include "LedControl.h"
  5. #include <TimerOne.h>
  6.  
  7. /*
  8.   Now we need a LedControl to work with.
  9.  ***** These pin numbers will probably not work with your hardware *****
  10.   pin 12 is connected to the DataIn
  11.   pin 11 is connected to the CLK
  12.   pin 10 is connected to LOAD
  13.   We have only a single MAX72XX.
  14. */
  15.  
  16. const byte interruptPinA = 2;
  17. const byte interruptPinB = 3;
  18. const byte interruptPinC = 18;
  19. const int LEDRIGHT = 7;  // the pin with a LED
  20. const int LEDLEFT = 6;  // the pin with a LED
  21. int ledState = LOW;
  22.  
  23.  
  24. volatile byte state = 0;
  25.  
  26. LedControl lc = LedControl(12, 11, 10, 1);
  27. byte LEFT[] = { B00011000, B00110000, B01100000, B11011111, B11011111, B01100000, B00110000, B00011000};
  28. byte RIGHT[] = { B00011000, B00001100, B00000110, B11111011, B11111011, B00000110, B00001100, B00011000};
  29. byte UP[] = { B00011000, B00111100, B01100110, B11011011, B10011001, B00011000, B00011000, B00011000};
  30. byte DN[] = { B00011000, B00011000, B00011000, B10011001, B11011011, B01100110, B00111100, B00011000};
  31. byte DIAMOND[] = { B00011000, B00111100, B01111110, B11111111, B11111111, B01111110, B00111100, B00011000};
  32. byte SQUARE0[] = { B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000};
  33. byte SQUARE1[] = { B00000000, B00000000, B00111100, B00100100, B00100100, B00111100, B00000000, B00000000};
  34. byte SQUARE2[] = { B00000000, B01111110, B01000010, B01000010, B01000010, B01000010, B01111110, B00000000};
  35. byte SQUARE3[] = { B11111111, B10000001, B10000001, B10000001, B10000001, B10000001, B10000001, B11111111};
  36.  
  37. byte CIRCLE0[] = { B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000};
  38. byte CIRCLE1[] = { B00000000, B00000000, B00011000, B00100100, B00100100, B00011000, B00000000, B00000000};
  39. byte CIRCLE2[] = { B00000000, B00111100, B01000010, B01000010, B01000010, B01000010, B00111100, B00000000};
  40. byte CIRCLE3[] = { B01111110, B10000001, B10000001, B10000001, B10000001, B10000001, B10000001, B01111110};
  41.  
  42.  
  43. const byte ROTATE[][8] = {
  44.   { B10001111,    B11001110,    B11101100,    B11111000,    B00011111,    B00110111,    B01110011,    B11110001  },
  45.   { B11110001,    B01110011,    B00110111,    B00011111,    B11111000,    B11101100,    B11001110,    B10001111  },
  46.   { B00011100,    B00011100,    B11001000,    B11111011,     B11011111,   B00010011,    B00111000,    B00111000  }
  47. };
  48.  
  49. const int ROTATE_LEN = sizeof(ROTATE) / 8;
  50.  
  51. byte BANK[8][8];
  52.  
  53. byte VAR1[] = { B10000000,
  54.                 B10000000,
  55.                 B10000000,
  56.                 B10000000,
  57.                 B10000000,
  58.                 B10000000,
  59.                 B10000000,
  60.                 B10000000
  61.               };
  62. byte TEST[] = { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B11111111};
  63.  
  64. byte TEMP[8] ;
  65.  
  66. /* we always wait a bit between updates of the display */
  67. unsigned long delaytime = 50;
  68.  
  69. void setup() {
  70.   /*
  71.     The MAX72XX is in power-saving mode on startup,
  72.     we have to do a wakeup call
  73.   */
  74.   lc.shutdown(0, false);
  75.   /* Set the brightness to a medium values */
  76.   lc.setIntensity(0, 8);
  77.   /* and clear the display */
  78.   lc.clearDisplay(0);
  79.  
  80.   attachInterrupt(digitalPinToInterrupt(interruptPinA), MovetoLEFT, CHANGE);
  81.   attachInterrupt(digitalPinToInterrupt(interruptPinB), MovetoRIGHT, CHANGE);
  82.   attachInterrupt(digitalPinToInterrupt(interruptPinC), OFFMATRIX, CHANGE);
  83.  
  84.   Timer1.initialize(500000);
  85.   Timer1.attachInterrupt(blinkLED); // blinkLED to run every 500ms
  86.   pinMode(LEDRIGHT, OUTPUT);
  87.   pinMode(LEDLEFT, OUTPUT);
  88.  
  89. }
  90.  
  91. void blinkLED(void)
  92. {
  93.   if (ledState == LOW) {
  94.     ledState = HIGH;
  95.   } else {
  96.     ledState = LOW;
  97.   }
  98.  
  99.   if (state == 1)
  100.     digitalWrite(LEDRIGHT, ledState);
  101.   else
  102.     digitalWrite(LEDRIGHT, HIGH);
  103.  
  104.   if (state == 2)
  105.     digitalWrite(LEDLEFT, ledState);
  106.   else
  107.     digitalWrite(LEDLEFT, HIGH);
  108.  
  109. }
  110.  
  111. void matrix(byte PARAM[]) {
  112.   for (int i = 0; i < 8; i++) {
  113.     lc.setRow(0, i, PARAM[i]);
  114.   }
  115. }
  116.  
  117. void matrixShiftRIGHT(void) {
  118.   //SHift to left
  119.   memcpy(TEMP, RIGHT, 8); // or strcpy( TEMP, RIGHT);
  120.   for (int j = 8; j > 0; j--) {
  121.     for (int i = 0; i < 8; i++) {
  122.       TEMP[i] = TEMP[i] << j;
  123.     }
  124.     matrix(TEMP);
  125.     delay(delaytime);
  126.     memcpy(TEMP, RIGHT, 8);
  127.   }
  128.  
  129.   for (int j = 1; j < 8; j++) {
  130.     for (int i = 0; i < 8; i++) {
  131.       TEMP[i] = TEMP[i] >> j;
  132.     }
  133.     matrix(TEMP);
  134.     delay(delaytime);
  135.     memcpy(TEMP, RIGHT, 8);
  136.   }
  137. }
  138.  
  139. void matrixShiftLEFT(void) {
  140.  
  141.   //SHift to left
  142.   memcpy(TEMP, LEFT, 8);
  143.   for (int j = 8; j > 0; j--) {
  144.     for (int i = 0; i < 8; i++) {
  145.       TEMP[i] = TEMP[i] >> j;
  146.     }
  147.     matrix(TEMP);
  148.     delay(delaytime);
  149.     memcpy(TEMP, LEFT, 8);
  150.   }
  151.  
  152.   for (int j = 0; j < 8; j++) {
  153.     for (int i = 0; i < 8; i++) {
  154.       TEMP[i] = TEMP[i] << j;
  155.     }
  156.     matrix(TEMP);
  157.     delay(delaytime);
  158.     memcpy(TEMP, LEFT, 8);
  159.   }
  160. }
  161.  
  162. void matrixShiftUP(void) {
  163.   int k = 0;
  164.   memcpy(TEMP, UP, 8);
  165.   memcpy(BANK[k++], TEMP, 8);
  166.   //  matrix(TEMP);
  167.   //  delay(delaytime);
  168.   for (int j = 0; j < 8; j++) {
  169.     for (int i = 7; i > 0; i--) {
  170.       TEMP[i] = TEMP[i - 1];
  171.     }
  172.     TEMP[0] = 0;
  173.     //    matrix(TEMP);
  174.     memcpy(BANK[k++], TEMP, 8);
  175.     //    delay(delaytime);
  176.   }
  177.  
  178.   for (int j = 7; j >= 0; j--) {
  179.     matrix(BANK[j]);
  180.     delay(delaytime);
  181.   }
  182.  
  183.  
  184.   memcpy(TEMP, UP, 8);
  185.   for (int i = 0; i < 7; i++) {
  186.     for (int i = 0; i < 7; i++) {
  187.       TEMP[i] = TEMP[i + 1];
  188.     }
  189.     TEMP[7] = 0;
  190.     matrix(TEMP);
  191.     delay(delaytime);
  192.   }
  193. }
  194.  
  195. void matrixShiftDN(void) {
  196.   memcpy(TEMP, DN, 8);
  197.   matrix(TEMP);
  198.   delay(delaytime);
  199.   for (int i = 0; i <= 8; i++) {
  200.     for (int i = 6; i >= 0; i--) {
  201.       TEMP[i + 1] = TEMP[i];
  202.     }
  203.     TEMP[0] = 0;
  204.     matrix(TEMP);
  205.     delay(delaytime);
  206.   }
  207. }
  208.  
  209. void matrixShiftSquare(void) {
  210.   int delayTime = 150;
  211.   matrix(SQUARE0);
  212.   delay(delayTime);
  213.   matrix(SQUARE1);
  214.   delay(delayTime);
  215.   matrix(SQUARE2);
  216.   delay(delayTime);
  217.   matrix(SQUARE3);
  218.   delay(delayTime);
  219.   lc.clearDisplay(0);
  220.   delay(delayTime);
  221.   matrix(SQUARE2);
  222.   delay(delayTime);
  223.   matrix(SQUARE1);
  224.   delay(delayTime);
  225.   matrix(SQUARE0);
  226.   delay(delayTime);
  227.   lc.clearDisplay(0);
  228.   delay(delayTime);
  229. }
  230.  
  231. void matrixRotateLED(void) {
  232.   int delayTime = 150;
  233.     matrix(ROTATE[0]);
  234.     delay(delayTime);
  235.     matrix(ROTATE[1]);
  236.     delay(delayTime);
  237.     matrix(ROTATE[2]);
  238.     delay(delayTime);
  239. }
  240.  
  241. void loop() {
  242.   //  switch (state) {
  243.   //    case 0:
  244.   //      break;
  245.   //    case 1:
  246.   //      break;
  247.   //  }
  248.   if (state == 1) {
  249.     matrixShiftRIGHT();
  250.   } if (state == 2) {
  251.     matrixShiftLEFT();
  252.   }
  253.   if (state == 3) {
  254.     //    lc.clearDisplay(0);
  255.     matrixRotateLED();
  256.   }
  257.  
  258.   //  matrixShiftUP();
  259.   //  matrixShiftUP();
  260.   //  matrixShiftUP();
  261.   //  matrixShiftLEFT();
  262.   //  matrixShiftLEFT();
  263.   //  matrixShiftLEFT();
  264.   //  matrixShiftRIGHT();
  265.   //  matrixShiftRIGHT();
  266.   //  matrixShiftRIGHT();
  267.  
  268.   //  matrix(LEFT);
  269.   //  delay(delaytime);
  270.   //  matrix(RIGHT);
  271.   //  delay(delaytime);
  272.   //  matrix(UP);
  273.   //  delay(delaytime);
  274.   //  matrix(DIAMOND);
  275.   //  delay(delaytime);
  276.   //  matrix(SQUARE);
  277.   //  delay(delaytime);
  278.   //  matrix(SQUARE1);
  279.   //  delay(delaytime);
  280.   //  matrix(SQUARE2);
  281.   //  delay(delaytime);
  282.   //  matrix(SQUARE3);
  283.   //  delay(delaytime);
  284.  
  285.   //    matrix(CIRCLE0);
  286.   //    delay(delaytime);
  287.   //    matrix(CIRCLE1);
  288.   //    delay(delaytime);
  289.   //    matrix(CIRCLE2);
  290.   //    delay(delaytime);
  291.   //    matrix(CIRCLE3);
  292.   //    delay(delaytime);
  293.  
  294.   //  for (int i = 0; i < 4; i++) {
  295.   //    matrix(CIRCLE3);
  296.   //    delay(delaytime);
  297.   //    matrix(CIRCLE2);
  298.   //    delay(delaytime);
  299.   //    matrix(CIRCLE1);
  300.   //    delay(delaytime);
  301.   //  }
  302.   //  lc.clearDisplay(0);
  303.   //  delay(100);
  304. }
  305.  
  306. void MovetoLEFT() {
  307.   state = 1;
  308. }
  309.  
  310. void MovetoRIGHT() {
  311.   state = 2;
  312. }
  313.  
  314. void OFFMATRIX() {
  315.   state = 3;
  316. }
Advertisement
Add Comment
Please, Sign In to add comment