Advertisement
Guest User

Untitled

a guest
May 31st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <LedControl.h>
  2.  
  3. #define CYCLE_TIME 100
  4. #define LENGTH 4
  5. #define GROW_STEP 5
  6.  
  7. struct pos {
  8. int row;
  9. int col;
  10. }typedef Led_pos;
  11.  
  12. LedControl lc = LedControl(4, 3, 5, 1);
  13. struct pos[LENGTH];
  14.  
  15. void setup() {
  16. /*
  17. The MAX72XX is in power-saving mode on startup,
  18. we have to do a wakeup call
  19. */
  20. lc.shutdown(0,false);
  21. /* Set the brightness to a medium values */
  22. lc.setIntensity(0, 15);
  23. /* and clear the display */
  24. lc.clearDisplay(0);
  25. }
  26.  
  27. void loop() {
  28. start();
  29. }
  30.  
  31. void start() {
  32. Led_pos snake[LENGTH];
  33. int i,j;
  34. int len = 8;
  35. int count = 1;
  36. int dir = 1;
  37. int row = 0;
  38. int col = -1;
  39. int currLength = 0;
  40. int chuj = -1;
  41. Led_pos last;
  42. last.col= -LENGTH-1;
  43. last.row = 0;
  44. for(i = 0; i<LENGTH; i++){
  45. snake[i].col = -1-i;
  46. snake[i].row = 0;
  47. }
  48.  
  49. for(i = 0; i < 128; i++) {
  50. lc.setLed(0, last.row, last.col, false);
  51. switch(dir) {
  52. case 1:
  53. col++;
  54. break;
  55. case 2:
  56. row++;
  57. break;
  58. case 3:
  59. col--;
  60. break;
  61. case 4:
  62. row--;
  63. break;
  64. }
  65. lc.setLed(0, row, col, true);
  66.  
  67. last = snake[LENGTH-1];
  68. for(j=LENGTH-1; j>=1;j--){
  69. snake[j] = snake[j-1];
  70.  
  71. }
  72. snake[0].row = row;
  73. snake[0].col = col;
  74. delay(CYCLE_TIME);
  75. currLength++;
  76. if(currLength == len) {
  77. currLength = 0;
  78. count++;
  79. dir = dir % 4 + 1;
  80. if(count == 2) {
  81. len += chuj;
  82. count = 0;
  83. }
  84. }
  85. if(len == 0) {
  86. chuj = -chuj;
  87. len = 1;
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement