Advertisement
Guest User

Ummmmmmmmmm

a guest
Nov 28th, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. /*
  2. ledcube.pde - Example sketch for controlling an LED cube.
  3. Created by Gamaiel Zavala (gzip), 2009-2012
  4. MIT License. See accompanying LICENSE file for terms.
  5. */
  6.  
  7. #include <LedCube.h>
  8. #ifndef LedCube_h
  9. #define LedCube_h
  10.  
  11. #if defined(ARDUINO) && ARDUINO >= 100
  12. #include "Arduino.h"
  13. #else
  14. #include "WProgram.h"
  15. #endif
  16.  
  17. struct cubeFrame {
  18. unsigned int size;
  19. unsigned int delay;
  20. byte *sequence;
  21. };
  22.  
  23. class LedCube
  24. {
  25. public:
  26. LedCube(byte size, byte levelPins[], byte colPins[]);
  27. ~LedCube();
  28.  
  29. byte getCols(){ return cols; }
  30. byte getLevels(){ return levels; }
  31. byte getNumLights(){ return num; }
  32.  
  33. void light(byte level, byte col, byte val);
  34. void lightOn(byte level, byte col);
  35. void lightOff(byte level, byte col);
  36. void lightPulse(byte level, byte col, unsigned int wait = 5);
  37. void lightSequence(byte seq[], byte length, unsigned int time = 5, byte gap = 1);
  38. void lightLevel(byte r, unsigned int wait = 50);
  39. void lightRow(byte r, byte level, unsigned int wait = 50);
  40. void lightPlane(byte r, unsigned int wait = 50);
  41. void lightColumn(byte col, unsigned int wait = 50);
  42. void lightDrop(byte col, unsigned int wait = 50);
  43. void lightPerimeter(byte level, byte rotations, unsigned int wait = 50);
  44. void randomLight(byte numLights, unsigned int wait = 50);
  45. void randomColumn(byte numColumns = 1, unsigned int wait = 50);
  46. void lightsOut(unsigned int wait = 5);
  47.  
  48. cubeFrame* createFrame(byte sequence[], unsigned int length, unsigned int delay);
  49. void destroyFrame(cubeFrame* frame);
  50. void lightFrames(cubeFrame* frames[], unsigned int length);
  51.  
  52. void enableBuffer(boolean enable = true);
  53. void invertBuffer(boolean invert = true);
  54. void clearBuffer();
  55. void fillBuffer();
  56. void drawBuffer(unsigned int wait = 1);
  57. byte getBufferAt(byte lv, byte col);
  58.  
  59. private:
  60. byte levels;
  61. byte cols;
  62. byte num;
  63. byte **buffer;
  64. byte *colPins;
  65. byte *levelPins;
  66. boolean bufferEnabled;
  67. boolean bufferInverted;
  68.  
  69. void setBuffer (byte val);
  70. };
  71.  
  72. #endif
  73.  
  74.  
  75. #define SIZE 3
  76. #define COLS (SIZE*SIZE)
  77.  
  78. byte levelPins[SIZE] = {
  79. 11,12,13};
  80. byte colPins[COLS] = {
  81. 2,3,4,5,6,7,8,9,10};
  82.  
  83. LedCube cube(SIZE, levelPins, colPins);
  84. int Pot = A2;
  85. int potValue = 0;
  86.  
  87. void setup (){
  88. pinMode (Pot,INPUT);
  89. }
  90.  
  91. void loop (){
  92. potValue = analogRead(Pot);
  93. delay(10);
  94.  
  95. if (potValue <= 5) {
  96.  
  97.  
  98. for(byte level=0; level<cube.getLevels(); level++)
  99. {
  100. for(byte col=0; col<cube.getCols(); col++)
  101. {
  102. cube.lightPulse(level, col, 100);
  103. }
  104. }
  105. }
  106. if (potValue >= 6 && potValue <= 100) {
  107. // light one level at a time, increasing speed each time
  108. for(byte d=25; d>2; d-=2)
  109. {
  110. for(byte l=1; l <= cube.getLevels(); l++)
  111. {
  112. cube.lightLevel(l, d);
  113. }
  114. }
  115. }
  116.  
  117.  
  118. if (potValue >= 101 && potValue <= 200) {
  119. // light each row on each level
  120. for(byte level=1; level<=cube.getLevels(); level++)
  121. {
  122. for(byte row=1; row<=cube.getLevels()*2; row++)
  123. {
  124. cube.lightRow(row, level);
  125. }
  126. }
  127. }
  128.  
  129. if (potValue >= 201 && potValue <= 300) {
  130. // light each plane
  131. for(byte i=3; i; i--)
  132. {
  133. for(byte row=1; row<=cube.getLevels()*2; row++)
  134. {
  135. cube.lightPlane(row, 10*i);
  136. }
  137. }
  138. }
  139.  
  140. if (potValue >= 301 && potValue <= 400) {
  141. // single random light at a time
  142. cube.randomLight(random(25,100),100);
  143.  
  144. // random column drop
  145. for(byte x=0; x<=15; x++)
  146. {
  147. cube.lightDrop(random(0,cube.getCols()), random(50,150));
  148. }
  149. }
  150.  
  151. if (potValue >= 401 && potValue <= 500) {
  152. // circle around cube at a random level
  153. for(byte x=0; x<=5; x++)
  154. {
  155. cube.lightPerimeter(random(0,cube.getLevels()), random(1,5), random(25,100));
  156. }
  157. }
  158. if (potValue >= 501 && potValue <= 600) {
  159. // light each face
  160. byte planes[] = {
  161. cube.getLevels()+1,cube.getLevels(),cube.getLevels()*2,1 };
  162. for(byte i=5; i; i--)
  163. {
  164. for(byte p=0; p<sizeof(planes); p++)
  165. {
  166. cube.lightPlane(planes[p], 5*i);
  167. }
  168. }
  169. }
  170. if (potValue >= 601 && potValue <= 700) {
  171. // random columns
  172. cube.randomColumn(25);
  173. }
  174. if (potValue >= 701 && potValue <= 800) {
  175.  
  176. // turn off a single column randomly
  177. cube.enableBuffer();
  178. for(byte c=0; c<30; c++)
  179. {
  180. cube.fillBuffer();
  181. cube.invertBuffer();
  182. cube.randomColumn();
  183. cube.drawBuffer(7);
  184. }
  185. cube.enableBuffer(false);
  186. }
  187. if (potValue >= 801 && potValue <= 900) {
  188. // cols in and out
  189. for(byte c=1, d=0; c<=10; c++)
  190. {
  191. if(c%2 == 0)
  192. {
  193. for(d=0; d<20; d++)
  194. {
  195. cube.lightColumn(2,1);
  196. cube.lightColumn(4,1);
  197. cube.lightColumn(6,1);
  198. cube.lightColumn(8,1);
  199. }
  200. }
  201. else if(c%4 == 1)
  202. {
  203. for(d=0; d<30; d++)
  204. {
  205. cube.lightColumn(1,1);
  206. cube.lightColumn(3,1);
  207. cube.lightColumn(7,1);
  208. cube.lightColumn(9,1);
  209. }
  210. }
  211. else
  212. {
  213. for(d=0; d<70; d++)
  214. {
  215. cube.lightColumn(5,1);
  216. }
  217. }
  218. }
  219. }
  220. if (potValue >= 901 && potValue <= 950) {
  221.  
  222. // diamond and box
  223. byte diamond[] = {
  224. 0,4, 1,1, 1,3, 1,4, 1,5, 1,7, 2,4 };
  225. byte box[] = {
  226. 2,0, 2,1, 2,2, 2,3, 2,5, 2,6, 2,7, 2,8,
  227. 1,0, 1,2, 1,6, 1,8,
  228. 0,0, 0,1, 0,2, 0,3, 0,5, 0,6, 0,7, 0,8
  229. };
  230. cube.lightSequence(box, sizeof(box), 200);
  231. cube.lightSequence(diamond, sizeof(diamond), 400);
  232. }
  233.  
  234. if (potValue >= 951 && potValue <= 980) {
  235. // helicopter effect
  236. byte topSeq[8] = {
  237. 0,3,6,7,8,5,2,1 };
  238. byte botSeq[8] = {
  239. 8,5,2,1,0,3,6,7 };
  240. for(byte loops = 0, delay = 50; loops<=8; loops++)
  241. {
  242. for(byte s=0; s<8; s++)
  243. {
  244. byte seq[] = {
  245. 2,topSeq[s], 1,4, 0,botSeq[s] };
  246. cube.lightSequence(seq, sizeof(seq), delay);
  247. }
  248. if(loops < 5) delay-=10;
  249. else delay += 10;
  250. }
  251. }
  252. if (potValue >= 981&& potValue <= 1000) {
  253. // turn off one light at a time
  254. cube.enableBuffer();
  255. cube.fillBuffer();
  256. cube.drawBuffer(25);
  257. for(byte w=0, l, c, max = cube.getNumLights(); w<max; )
  258. {
  259. // lower bound is inclusive, upper is exclusive
  260. l = random(0, cube.getLevels());
  261. c = random(0, cube.getCols());
  262.  
  263. if(cube.getBufferAt(l,c) == HIGH)
  264. {
  265. cube.lightOff(l,c);
  266. cube.drawBuffer(5);
  267. w++;
  268. }
  269. }
  270. }
  271. cube.enableBuffer(false);
  272. }
  273. .
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement