Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. #include <Entropy.h>
  2. /* Original code downloaded fromhttps://github.com/strykeroz/ATTiny85-20-LED-snowflakes
  3. Original code by Geoff Steele. Altered code by commenting some sections out to just blink LEDs on and off by Alicia Gibb.
  4. The original code is still all there if others wanted to keep playing with it, just put the duty cycle back in,
  5. which have a fade effect rather than a blink effect.
  6. The delays have also been changed, but can easily be reinstated by looking at the orginal code.
  7.  
  8. ____ _ _ _ _ ____ ___
  9. / ___| |__ __ _ _ __| (_) ___ _ __ | | _____ _|___ \ / _ \ ___ _ __ _____ __
  10. | | | '_ \ / _` | '__| | |/ _ \ '_ \| |/ _ \ \/ / __) | | | / __| '_ \ / _ \ \ /\ / /
  11. | |___| | | | (_| | | | | | __/ |_) | | __/> < / __/| |_| \__ \ | | | (_) \ V V /
  12. \____|_| |_|\__,_|_| |_|_|\___| .__/|_|\___/_/\_\_____|\___/|___/_| |_|\___/ \_/\_/
  13. |_|
  14. Charlieplexing 20 LEDs using 5 ATTiny85 pins with fading by
  15. varying the duty cycle of each LED in the 'tail'.
  16. ATTiny85 connections
  17. Leg Function
  18. 1 Reset, no connection
  19. 2 D3 GREEN
  20. 3 D4 ORANGE
  21. 4 GND
  22. 5 D0 WHITE
  23. 6 D1 BLUE
  24. 7 D2 YELLOW
  25. 8 +5V
  26. Tested on ATTiny85 running at 8MHz.
  27. */
  28.  
  29.  
  30. // each block of 4 LEDs in the array is groupled by a common anode (+, long leg)
  31. // for simplicity of wiring on breadboard, using a colour code
  32. #define GREEN 0
  33. #define ORANGE 1
  34. #define WHITE 2
  35. #define BLUE 3
  36. #define YELLOW 4
  37.  
  38. // pin definitions {GREEN, ORANGE, WHITE, BLUE, YELLOW}
  39. const int charliePin[5] = {
  40. 3, 4, 0, 1, 2};
  41.  
  42. // Charlieplexed LED definitions (current flowing from-to pairs)
  43. const int LED[20][2] = {
  44. {
  45. ORANGE, GREEN }
  46. , // 0 (GREEN GROUP)
  47. {
  48. WHITE, GREEN }
  49. , // 1
  50. {
  51. BLUE, GREEN }
  52. , // 2
  53. {
  54. YELLOW, GREEN }
  55. , // 3
  56. {
  57. GREEN, ORANGE }
  58. , // 4 (ORANGE GROUP)
  59. {
  60. WHITE, ORANGE }
  61. , // 5
  62. {
  63. BLUE, ORANGE }
  64. , // 6
  65. {
  66. YELLOW, ORANGE }
  67. , // 7
  68. {
  69. GREEN, WHITE }
  70. , // 8 (WHITE GROUP)
  71. {
  72. ORANGE, WHITE }
  73. , // 9
  74. {
  75. BLUE, WHITE }
  76. , // 10
  77. {
  78. YELLOW, WHITE }
  79. , // 11
  80. {
  81. GREEN, BLUE }
  82. , // 12 (BLUE GROUP)
  83. {
  84. ORANGE, BLUE }
  85. , // 13
  86. {
  87. WHITE, BLUE }
  88. , // 14
  89. {
  90. YELLOW, BLUE }
  91. , // 15
  92. {
  93. GREEN, YELLOW }
  94. , // 16 (YELLOW GROUP)
  95. {
  96. ORANGE, YELLOW }
  97. , // 17
  98. {
  99. WHITE, YELLOW }
  100. , // 18
  101. {
  102. BLUE, YELLOW } // 19
  103. };
  104.  
  105. // other
  106. int current = 0; // LED in array with current focus
  107. int previous = 0; // previous LED that was lit
  108.  
  109. void setup() {
  110. Entropy.Initialize();
  111. randomSeed(Entropy.random());
  112. }
  113.  
  114. void loop() {
  115. unsigned long loopCount = 0; // used to determine duty cycle of each LED
  116. unsigned long timeNow = millis(); //
  117. unsigned long displayTime = 10 + random(10); // milliseconds to spend at each focus LED in descent
  118. while(millis()- timeNow < (displayTime+current)) { // animation slows toward end
  119. loopCount++;
  120. // the "snowflake" gets full duty cycle. When it gets to the end, hold it at the end until the tail collapses
  121. if (current > 19) charlieOFF(19); //This is altered from the original code, to turn LEDs off once the blink is over
  122. else charlieON(current);
  123. // each member of tail has reduced duty cycle, and never get to the final position
  124. if(!(loopCount % 3)) if(current-1 >=0 && current-1 < 19) charlieOFF(current-1);
  125. if(!(loopCount % 6)) if(current-2 >=0 && current-2 < 19) charlieOFF(current-2);
  126. if(!(loopCount % 9)) if(current-3 >=0 && current-3 < 19) charlieOFF(current-3);
  127. if(!(loopCount % 12)) if(current-4 >=0 && current-4 < 19) charlieOFF(current-4);
  128. }
  129.  
  130. current++;
  131. if(current==23) { // start over
  132. //Alicia commented out the below code to make the LEDs blink on and off rather than fade out.
  133. // now fade out the snowflake in that final position #19
  134. for(int dutyCycle = 3; dutyCycle <= 15; dutyCycle += 3) {
  135. //loopCount = 0;
  136. //timeNow = millis();
  137. //while(millis() - timeNow < (displayTime+current*2)) { // fade out as slow as animation has achieved by now
  138. // loopCount++;
  139. if(!(loopCount % dutyCycle)) charlieON(19);
  140. else charlieOFF(19);
  141. // }
  142. }
  143. current = 0;
  144. charlieOFF(19); // turn off the remaining (possibly) lit LED
  145. delay(50);
  146. //+ random(3000)); // and then rinse, repeat...after a short pause
  147. }
  148. }
  149.  
  150. // --------------------------------------------------------------------------------
  151. // turns on LED #thisLED. Turns off all LEDs if the value passed is out of range
  152. //
  153. void charlieON(int thisLED) {
  154. // turn off previous (reduces overhead, only switch 2 pins rather than 5)
  155. digitalWrite(charliePin[LED[previous][1]], LOW); // ensure internal pull-ups aren't engaged on INPUT mode
  156. pinMode(charliePin[LED[previous][0]], INPUT);
  157. pinMode(charliePin[LED[previous][1]], INPUT);
  158.  
  159. // turn on the one that's in focus
  160. if(thisLED >= 0 && thisLED <= 19) {
  161. pinMode(charliePin[LED[thisLED][0]], OUTPUT);
  162. pinMode(charliePin[LED[thisLED][1]], OUTPUT);
  163. digitalWrite(charliePin[LED[thisLED][0]], LOW);
  164. digitalWrite(charliePin[LED[thisLED][1]], HIGH);
  165. }
  166. previous = thisLED;
  167. }
  168.  
  169. // --------------------------------------------------------------------------------
  170. // turns off LED #thisLED.
  171. //
  172. void charlieOFF(int thisLED) {
  173. digitalWrite(charliePin[LED[thisLED][1]], LOW); // ensure internal pull-ups aren't engaged on INPUT mode
  174. pinMode(charliePin[LED[thisLED][0]], INPUT);
  175. pinMode(charliePin[LED[thisLED][1]], INPUT);
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement