hendriawan

matrix max7219 v9

Feb 7th, 2019
790
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 led = LED_BUILTIN;  // the pin with a LED
  20. int ledState = LOW;
  21.  
  22.  
  23. volatile byte state = 0;
  24.  
  25. LedControl lc = LedControl(12, 11, 10, 1);
  26. byte LEFT[] = { B00011000, B00110000, B01100000, B11011111, B11011111, B01100000, B00110000, B00011000};
  27. byte RIGHT[] = { B00011000, B00001100, B00000110, B11111011, B11111011, B00000110, B00001100, B00011000};
  28. byte UP[] = { B00011000, B00111100, B01100110, B11011011, B10011001, B00011000, B00011000, B00011000};
  29. byte DN[] = { B00011000, B00011000, B00011000, B10011001, B11011011, B01100110, B00111100, B00011000};
  30. byte DIAMOND[] = { B00011000, B00111100, B01111110, B11111111, B11111111, B01111110, B00111100, B00011000};
  31. byte SQUARE0[] = { B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111, B11111111};
  32. byte SQUARE1[] = { B00100100, B01000010, B10000001, B00000000, B00000000, B10000001, B01000010, B00100100};
  33. byte SQUARE2[] = { B00000000, B00100100, B01100110, B00000000, B00000000, B01100110, B00100100, B00000000};
  34. byte SQUARE3[] = { B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000};
  35.  
  36. byte CIRCLE0[] = { B00000000, B00000000, B00000000, B00011000, B00011000, B00000000, B00000000, B00000000};
  37. byte CIRCLE1[] = { B00000000, B00000000, B00011000, B00100100, B00100100, B00011000, B00000000, B00000000};
  38. byte CIRCLE2[] = { B00000000, B00111100, B01000010, B01000010, B01000010, B01000010, B00111100, B00000000};
  39. byte CIRCLE3[] = { B01111110, B10000001, B10000001, B10000001, B10000001, B10000001, B10000001, B01111110};
  40. byte BANK[8][8];
  41.  
  42. byte VAR1[] = { B10000000,
  43.                 B10000000,
  44.                 B10000000,
  45.                 B10000000,
  46.                 B10000000,
  47.                 B10000000,
  48.                 B10000000,
  49.                 B10000000
  50.               };
  51. byte TEST[] = { B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B11111111};
  52.  
  53. byte TEMP[8] ;
  54.  
  55. /* we always wait a bit between updates of the display */
  56. unsigned long delaytime = 50;
  57.  
  58. void setup() {
  59.   /*
  60.     The MAX72XX is in power-saving mode on startup,
  61.     we have to do a wakeup call
  62.   */
  63.   lc.shutdown(0, false);
  64.   /* Set the brightness to a medium values */
  65.   lc.setIntensity(0, 8);
  66.   /* and clear the display */
  67.   lc.clearDisplay(0);
  68.  
  69.   attachInterrupt(digitalPinToInterrupt(interruptPinA), MovetoLEFT, CHANGE);
  70.   attachInterrupt(digitalPinToInterrupt(interruptPinB), MovetoRIGHT, CHANGE);
  71.   attachInterrupt(digitalPinToInterrupt(interruptPinC), OFFMATRIX, CHANGE);
  72.  
  73.   Timer1.initialize(500000);
  74.   Timer1.attachInterrupt(blinkLED); // blinkLED to run every 500ms
  75.   pinMode(led, OUTPUT);
  76.  
  77. }
  78.  
  79. void blinkLED(void)
  80. {
  81.   if (ledState == LOW) {
  82.     ledState = HIGH;
  83.   } else {
  84.     ledState = LOW;
  85.   }
  86.   if (state == 1)
  87.     digitalWrite(led, ledState);
  88.   else
  89.     digitalWrite(led, LOW);
  90.  
  91. }
  92.  
  93. void matrix(byte PARAM[]) {
  94.   for (int i = 0; i < 8; i++) {
  95.     lc.setRow(0, i, PARAM[i]);
  96.   }
  97. }
  98.  
  99. void matrixShiftRIGHT(void) {
  100.   //SHift to left
  101.   memcpy(TEMP, RIGHT, 8); // or strcpy( TEMP, RIGHT);
  102.   for (int j = 8; j > 0; j--) {
  103.     for (int i = 0; i < 8; i++) {
  104.       TEMP[i] = TEMP[i] << j;
  105.     }
  106.     matrix(TEMP);
  107.     delay(delaytime);
  108.     memcpy(TEMP, RIGHT, 8);
  109.   }
  110.  
  111.   for (int j = 1; j < 8; j++) {
  112.     for (int i = 0; i < 8; i++) {
  113.       TEMP[i] = TEMP[i] >> j;
  114.     }
  115.     matrix(TEMP);
  116.     delay(delaytime);
  117.     memcpy(TEMP, RIGHT, 8);
  118.   }
  119. }
  120.  
  121. void matrixShiftLEFT(void) {
  122.  
  123.   //SHift to left
  124.   memcpy(TEMP, LEFT, 8);
  125.   for (int j = 8; j > 0; j--) {
  126.     for (int i = 0; i < 8; i++) {
  127.       TEMP[i] = TEMP[i] >> j;
  128.     }
  129.     matrix(TEMP);
  130.     delay(delaytime);
  131.     memcpy(TEMP, LEFT, 8);
  132.   }
  133.  
  134.   for (int j = 0; j < 8; j++) {
  135.     for (int i = 0; i < 8; i++) {
  136.       TEMP[i] = TEMP[i] << j;
  137.     }
  138.     matrix(TEMP);
  139.     delay(delaytime);
  140.     memcpy(TEMP, LEFT, 8);
  141.   }
  142. }
  143.  
  144. void matrixShiftUP(void) {
  145.   int k = 0;
  146.   memcpy(TEMP, UP, 8);
  147.   memcpy(BANK[k++], TEMP, 8);
  148.   //  matrix(TEMP);
  149.   //  delay(delaytime);
  150.   for (int j = 0; j < 8; j++) {
  151.     for (int i = 7; i > 0; i--) {
  152.       TEMP[i] = TEMP[i - 1];
  153.     }
  154.     TEMP[0] = 0;
  155.     //    matrix(TEMP);
  156.     memcpy(BANK[k++], TEMP, 8);
  157.     //    delay(delaytime);
  158.   }
  159.  
  160.   for (int j = 7; j >= 0; j--) {
  161.     matrix(BANK[j]);
  162.     delay(delaytime);
  163.   }
  164.  
  165.  
  166.   memcpy(TEMP, UP, 8);
  167.   for (int i = 0; i < 7; i++) {
  168.     for (int i = 0; i < 7; i++) {
  169.       TEMP[i] = TEMP[i + 1];
  170.     }
  171.     TEMP[7] = 0;
  172.     matrix(TEMP);
  173.     delay(delaytime);
  174.   }
  175. }
  176.  
  177. void matrixShiftDN(void) {
  178.   memcpy(TEMP, DN, 8);
  179.   matrix(TEMP);
  180.   delay(delaytime);
  181.   for (int i = 0; i <= 8; i++) {
  182.     for (int i = 6; i >= 0; i--) {
  183.       TEMP[i + 1] = TEMP[i];
  184.     }
  185.     TEMP[0] = 0;
  186.     matrix(TEMP);
  187.     delay(delaytime);
  188.   }
  189. }
  190.  
  191. void loop() {
  192.   //  switch (state) {
  193.   //    case 0:
  194.   //      break;
  195.   //    case 1:
  196.   //      break;
  197.   //  }
  198.   if (state == 1) {
  199.     matrixShiftRIGHT();
  200.   } if (state == 2) {
  201.     matrixShiftLEFT();
  202.   }
  203.   if (state == 3) {
  204.     lc.clearDisplay(0);
  205.   }
  206.  
  207.   //  matrixShiftUP();
  208.   //  matrixShiftUP();
  209.   //  matrixShiftUP();
  210.   //  matrixShiftLEFT();
  211.   //  matrixShiftLEFT();
  212.   //  matrixShiftLEFT();
  213.   //  matrixShiftRIGHT();
  214.   //  matrixShiftRIGHT();
  215.   //  matrixShiftRIGHT();
  216.  
  217.   //  matrix(LEFT);
  218.   //  delay(delaytime);
  219.   //  matrix(RIGHT);
  220.   //  delay(delaytime);
  221.   //  matrix(UP);
  222.   //  delay(delaytime);
  223.   //  matrix(DIAMOND);
  224.   //  delay(delaytime);
  225.   //  matrix(SQUARE);
  226.   //  delay(delaytime);
  227.   //  matrix(SQUARE1);
  228.   //  delay(delaytime);
  229.   //  matrix(SQUARE2);
  230.   //  delay(delaytime);
  231.   //  matrix(SQUARE3);
  232.   //  delay(delaytime);
  233.  
  234.   //    matrix(CIRCLE0);
  235.   //    delay(delaytime);
  236.   //    matrix(CIRCLE1);
  237.   //    delay(delaytime);
  238.   //    matrix(CIRCLE2);
  239.   //    delay(delaytime);
  240.   //    matrix(CIRCLE3);
  241.   //    delay(delaytime);
  242.  
  243.   //  for (int i = 0; i < 4; i++) {
  244.   //    matrix(CIRCLE3);
  245.   //    delay(delaytime);
  246.   //    matrix(CIRCLE2);
  247.   //    delay(delaytime);
  248.   //    matrix(CIRCLE1);
  249.   //    delay(delaytime);
  250.   //  }
  251.   //  lc.clearDisplay(0);
  252.   //  delay(100);
  253. }
  254.  
  255. void MovetoLEFT() {
  256.   state = 1;
  257. }
  258.  
  259. void MovetoRIGHT() {
  260.   state = 2;
  261. }
  262.  
  263. void OFFMATRIX() {
  264.   state = 3;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment