Guest User

Untitled

a guest
Jul 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.89 KB | None | 0 0
  1. #include <avr/pgmspace.h>
  2.  
  3. #define clockpin 13 // CI
  4. #define enablepin 10 // EI
  5. #define latchpin 9 // LI
  6. #define datapin 11 // DI
  7.  
  8. #define NumLEDs 56
  9.  
  10. #define rows 8
  11. #define columns 7
  12.  
  13. int LEDChannels[NumLEDs][3] = {0};
  14. int SB_CommandMode;
  15. int SB_RedCommand;
  16. int SB_GreenCommand;
  17. int SB_BlueCommand;
  18.  
  19. int current_img[columns][rows][3] = {0};
  20. int next_img[columns][rows][3] = {0};
  21.  
  22. int num_frames = 6;
  23. int current_frame = 0;
  24.  
  25. PROGMEM prog_int16_t all_white[columns][rows][3] = {
  26. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } },
  27. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } },
  28. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } },
  29. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } },
  30. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } },
  31. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } },
  32. { { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } , { 1000, 1000, 1000 } }
  33. };
  34.  
  35. PROGMEM prog_int16_t all_black[columns][rows][3] = {
  36. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } },
  37. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } },
  38. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } },
  39. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } },
  40. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } },
  41. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } },
  42. { { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } , { 0, 0, 0 } }
  43. };
  44.  
  45. PROGMEM prog_int16_t big_H[columns][rows][3] = {
  46. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  47. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  48. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  49. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  50. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  51. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  52. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } }
  53. };
  54.  
  55. PROGMEM prog_int16_t big_I[columns][rows][3] = {
  56. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  57. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  58. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  59. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  60. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  61. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  62. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } }
  63. };
  64.  
  65. PROGMEM prog_int16_t big_V[columns][rows][3] = {
  66. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  67. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  68. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  69. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  70. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  71. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  72. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } }
  73. };
  74.  
  75. PROGMEM prog_int16_t big_E[columns][rows][3] = {
  76. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } },
  77. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  78. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  79. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  80. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  81. { { 0000, 0000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 1000, 0000 } , { 0000, 0000, 0000 } },
  82. { { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } , { 0000, 0000, 0000 } }
  83. };
  84.  
  85.  
  86. void setup() {
  87.  
  88. pinMode(datapin, OUTPUT);
  89. pinMode(latchpin, OUTPUT);
  90. pinMode(enablepin, OUTPUT);
  91. pinMode(clockpin, OUTPUT);
  92. SPCR = (1<<SPE)|(1<<MSTR)|(0<<SPR1)|(0<<SPR0);
  93. digitalWrite(latchpin, LOW);
  94. digitalWrite(enablepin, LOW);
  95.  
  96. }
  97.  
  98.  
  99. void SB_SendPacket() {
  100.  
  101. if (SB_CommandMode == B01) {
  102. SB_RedCommand = 65;
  103. SB_GreenCommand = 50;
  104. SB_BlueCommand = 50;
  105. }
  106.  
  107. SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
  108. while(!(SPSR & (1<<SPIF)));
  109. SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
  110. while(!(SPSR & (1<<SPIF)));
  111. SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
  112. while(!(SPSR & (1<<SPIF)));
  113. SPDR = SB_GreenCommand;
  114. while(!(SPSR & (1<<SPIF)));
  115.  
  116. }
  117.  
  118. void WriteLEDArray() {
  119.  
  120. SB_CommandMode = B00; // Write to PWM control registers
  121. for (int h = 0;h<NumLEDs;h++) {
  122. SB_RedCommand = LEDChannels[h][0];
  123. SB_GreenCommand = LEDChannels[h][1];
  124. SB_BlueCommand = LEDChannels[h][2];
  125. SB_SendPacket();
  126. }
  127.  
  128. delayMicroseconds(15);
  129. digitalWrite(latchpin,HIGH); // latch data into registers
  130. delayMicroseconds(15);
  131. digitalWrite(latchpin,LOW);
  132.  
  133. SB_CommandMode = B01; // Write to current control registers
  134. for (int z = 0; z < NumLEDs; z++) SB_SendPacket();
  135. delayMicroseconds(15);
  136. digitalWrite(latchpin,HIGH); // latch data into registers
  137. delayMicroseconds(15);
  138. digitalWrite(latchpin,LOW);
  139.  
  140. }
  141.  
  142. void ConvertImg(){
  143. //converts the "2D" current_img array into the "1D" LEDChannels array
  144.  
  145. for ( int x = 0; x < columns; x++ ){
  146. for ( int y = 0; y < rows; y++ ){
  147.  
  148. int h = 0;
  149. //the following deals with the alternating direction of the rows
  150. if ( x%2 == 0 ){
  151. h = (rows*x) + y;
  152. }
  153. else{
  154. h = (rows*x) + (rows-y-1);
  155. }
  156.  
  157. LEDChannels[h][0] = current_img[x][y][0];
  158. LEDChannels[h][1] = current_img[x][y][1];
  159. LEDChannels[h][2] = current_img[x][y][2];
  160. }
  161. }
  162.  
  163. }
  164.  
  165. void loop() {
  166.  
  167. //do something to modify the next_img
  168.  
  169. //spells out "H-I-V-E"
  170. switch(current_frame){
  171. case 0:
  172. memcpy_P( next_img , big_H , ( sizeof(next_img) ) );
  173. break;
  174. case 1:
  175. memcpy_P( next_img , big_I , ( sizeof(next_img) ) );
  176. break;
  177. case 2:
  178. memcpy_P( next_img , big_V , ( sizeof(next_img) ) );
  179. break;
  180. case 3:
  181. memcpy_P( next_img , big_E , ( sizeof(next_img) ) );
  182. break;
  183. case 4:
  184. memcpy_P( next_img , all_black , ( sizeof(next_img) ) );
  185. break;
  186. default:
  187. memcpy_P( next_img , all_black , ( sizeof(next_img) ) );
  188. break;
  189. }
  190.  
  191.  
  192. current_frame++;
  193. if ( current_frame >= num_frames ) current_frame = 0;
  194.  
  195. //no need to alter below this point
  196.  
  197. //copy the next_img to the current_img
  198. memcpy( current_img , next_img , sizeof(current_img) );
  199. //memcpy( current_img , next_img , ( NumLEDs * 3 * sizeof(int) ) );
  200.  
  201. //change the 2D grid of values into a 1D chain of values
  202. ConvertImg();
  203.  
  204. //write out the current array to the "display"
  205. WriteLEDArray();
  206.  
  207. //pause between refresh
  208. delay(400);
  209.  
  210. }
Add Comment
Please, Sign In to add comment