Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. /**
  2. * Led Cube
  3. *
  4. * PINS
  5. * ------------------------------------------------------
  6. * Data pins 22~29 mapping by PORTA[0~7]
  7. * Select pins 10~13 mapping by PORTB[4~7]
  8. * Layer Select pins 30~37 mapping by PORTC[7~0] (Invert)
  9. */
  10. #define WIDTH 8
  11. #define selectLayer(layer) (PORTC = 1 << layer)
  12. #define selectRow(y) (PORTB = (0x8f | y + 1 << 4))
  13.  
  14. short currentX,
  15. currentY,
  16. currentZ;
  17. // dataMap[y][z]
  18. short dataMap[8][8];
  19.  
  20. void output(int y, int z, int v) {
  21. if(y < 8 && y >= 0 &&
  22. z < 8 && z >= 0)
  23. dataMap[y][z] = v;
  24. }
  25.  
  26. // function pointer for current effect
  27. void (*effect)();
  28.  
  29. /**
  30. * clear all the led
  31. */
  32. void cleanEffect() {
  33. for(int i = 0; i < 8; i++)
  34. for(int j = 0; j < 8; j++)
  35. output(i, j, 0);
  36. }
  37. /**
  38. * light all the led
  39. */
  40. void fullEffect() {
  41. for(int i = 0; i < 8; i++)
  42. for(int j = 0; j < 8; j++)
  43. output(i, j, 255);
  44. }
  45.  
  46. void rainEffect() {
  47. // scanning y
  48. for(int y = 0; y < WIDTH; y++) {
  49. for(int z = 0; z < WIDTH; z++) {
  50. for(int x = 0; x < WIDTH; x++) {
  51. // if dataMap(x, y, z) is HIGH
  52. if(dataMap[y][z] & (1 << x)) {
  53. // make dataMap(x, y, z) LOW
  54. output(y, z, dataMap[y][z] & !(0b11111111 && (1 << x)));
  55. // then make next layer HIGH
  56. output(y, z-1, dataMap[y][z-1] | (1 << x));
  57. }
  58. }
  59. }
  60. }
  61. dataMap[random(0, 8)][7] = (1 << random(0, 8));
  62. }
  63.  
  64. void columnEffect() {
  65. static bool reverseFlag = 0;
  66. static int l = 0;
  67. static int sub_l = 7;
  68.  
  69. if(reverseFlag) {
  70. for(int i = 0; i < 8; i++) { //from top to bottom
  71. output(i, l+1, 0);
  72. output(i, sub_l-1, 0);
  73. }
  74. } else {
  75. for(int i = 0; i < 8; i++) { //from bottom to top
  76. output(i, l-1, 0);
  77. output(i, sub_l+1, 0);
  78. }
  79. }
  80.  
  81. output(0, l, 255);
  82. output(1, l, 129);
  83. output(2, l, 129);
  84. output(3, l, 129);
  85. output(4, l, 129);
  86. output(5, l, 129);
  87. output(6, l, 129);
  88. output(7, l, 255);
  89.  
  90. output(0, sub_l, 0);
  91. output(1, sub_l, 0);
  92. output(2, sub_l, 0x3c);
  93. output(3, sub_l, 0x24);
  94. output(4, sub_l, 0x24);
  95. output(5, sub_l, 0x3c);
  96. output(6, sub_l, 0);
  97. output(7, sub_l, 0);
  98.  
  99. if(reverseFlag) {
  100. l--;
  101. sub_l++;
  102. } else {
  103. l++;
  104. sub_l--;
  105. }
  106. if(l == -1 || l == 8) reverseFlag = !reverseFlag;
  107. }
  108.  
  109. /**
  110. * fill effect
  111. */
  112. short fillLayer,fillY,fillCounter;
  113. bool fillreverseFlag;
  114. void fillEffect() {
  115. if(fillreverseFlag){
  116. dataMap[fillY][fillLayer] &= ~(1 << fillCounter);
  117. } else {
  118. dataMap[fillY][fillLayer] |= (1 << fillCounter);
  119. }
  120. fillCounter++;
  121. if(fillCounter == 8) {
  122. fillY++;
  123. fillCounter = 0;
  124. }
  125. if(fillY == 8) {
  126. fillLayer++;
  127. fillY = 0;
  128. }
  129. if(fillLayer == 8) {
  130. fillLayer = 0;
  131. fillreverseFlag=!fillreverseFlag;
  132. }
  133. }
  134.  
  135. void clearAll() {
  136. for(int i = 0; i < 8; i++)
  137. for(int j = 0; j < 8; j++)
  138. output(i, j, 0);
  139. }
  140.  
  141. void setup() {
  142. DDRA = 0xff; // pin 22~29 PORTA -> ENABLE Output
  143. DDRB = 0xf0; // pin 10~13 PORTB HIGHEST 4 bits Enable Output
  144. DDRC = 0xff; // pin 30~37 PORTC -> ENABLE Output (Invert)
  145. PORTB = 0x80; // 138 G1 Enable
  146. TCCR1A = 0x00; // Normal mode, just as a Timer
  147. TCCR1B = TCCR1B & ~0x07;
  148. TCCR1B = TCCR1B | 0x02; // prescaler = CPU clock/8
  149. TIMSK1 |= _BV(TOIE1); // enable timer overflow interrupt
  150.  
  151. Serial.begin(9600);
  152. TCNT1 = -1250; // Ticks for 5ms @16 MHz,prescale=8
  153.  
  154. currentX = 0;
  155. currentY = 0;
  156. currentZ = 0;
  157.  
  158. effect = cleanEffect;
  159. }
  160.  
  161. void loop() {};
  162.  
  163. int counter = 0;
  164. ISR (TIMER1_OVF_vect) {
  165. PORTC = 0x00;
  166.  
  167. if(Serial.available()) {
  168. char buffer;
  169. buffer = Serial.read();
  170.  
  171. switch(buffer) {
  172. case '0':
  173. effect = cleanEffect;
  174. break;
  175. case '1':
  176. effect = fullEffect;
  177. break;
  178. case '2':
  179. effect = rainEffect;
  180. break;
  181. case '3':
  182. effect = columnEffect;
  183. break;
  184. case '4':
  185. effect = fillEffect;
  186. fillLayer = 0;
  187. fillY = 0;
  188. fillCounter = 0;
  189. fillreverseFlag = false;
  190. break;
  191. default:
  192. break;
  193. }
  194.  
  195. clearAll();
  196. }
  197.  
  198. counter++;
  199. if(counter >= 150) {
  200. effect();
  201. counter = 0;
  202. }
  203. while(currentY < 8) {
  204. PORTA = dataMap[currentY][currentZ]; // Output Data to Data pins dataMap[y][x]
  205. selectRow(currentY);
  206. currentY++;
  207. }
  208. selectLayer(currentZ);
  209.  
  210. currentY = 0;
  211. currentZ = (++currentZ) % 8;
  212. TCNT1 = -1250; // Tick Reset
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement