hendriawan

matrix max7219 v8

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