Guest User

glitchy code

a guest
Dec 8th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.69 KB | None | 0 0
  1. #define FASTLED_ESP8266_RAW_PIN_ORDER
  2. #define FASTLED_ESP8266_NODEMCU_PIN_ORDER
  3. #define FASTLED_ESP8266_D1_PIN_ORDER
  4.  
  5. #include "FastLED.h"
  6.  
  7. FASTLED_USING_NAMESPACE
  8. #define LED_LEAD D1
  9. #define LED_TYPE WS2812B
  10. #define COLOR_ORDER GRB
  11. #define NUM_LEDS 175
  12. CRGB leds[NUM_LEDS];
  13.  
  14.  
  15. int BOTTOM_INDEX = 0;
  16. int TOP_INDEX = int(NUM_LEDS/2);
  17. int EVENODD = NUM_LEDS%2;
  18. int ledsX[NUM_LEDS][3]; //ARRAY FOR COPYING WHATS IN THE LED STRIP CURRENTLY (FOR CELL-AUTOMATA, MARCH, ECT)
  19. int thisdelay = 20;
  20.  
  21.  
  22. #define BRIGHTNESS 255
  23. #define FRAMES_PER_SECOND 120
  24.  
  25. const int interval = 25;
  26.  
  27. unsigned long previousMillis = millis();
  28.  
  29. const int PIXEL_FLASH_INTERVAL = 25;
  30.  
  31. //unsigned long previousMillis = millis();
  32.  
  33.  
  34. //FX LOOPS DELAY VAR
  35. int thisstep = 20;
  36. int thishue = 0;
  37. int thissat = 255;
  38.  
  39. //LED FX VARS
  40. int idex = 0; //LED INDEX (0 TO NUM_LEDS-1)
  41. int ihue = 0; //HUE (0-255)
  42. int ibright = 0; //BRIGHTNESS (0-255)
  43. int isat = 0; //SATURATION (0-255);
  44. int bouncedirection = 0; //SWITCH FOR COLOR BOUNCE (0-1)
  45. float tcount = 0.0; //INC VAR FOR SIN LOOPS
  46. int lcount = 0; //ANOTHER COUNTING VAR
  47.  
  48.  
  49.  
  50. /*
  51. //monitor button press
  52. bool buttonListener() {
  53. bool modeChanged = false;
  54. buttonState = digitalRead(BUTTON_LEAD);
  55.  
  56. if (buttonState != lastButtonState) {
  57. if (buttonState == LOW) {
  58. mode++;
  59. modeChanged = true;
  60. delay(250); //Debounce delay
  61. }
  62. }
  63. lastButtonState = buttonState;
  64.  
  65. return modeChanged;
  66. }
  67. */
  68.  
  69. //---SET THE COLOR OF A SINGLE RGB LED
  70. void set_color_led(int adex, int cred, int cgrn, int cblu) {
  71. leds[adex].setRGB( cred, cgrn, cblu);
  72. }
  73.  
  74. //---FIND INDEX OF HORIZONAL OPPOSITE LED
  75. int horizontal_index(int i) {
  76. //-ONLY WORKS WITH INDEX < TOPINDEX
  77. if (i == BOTTOM_INDEX) {return BOTTOM_INDEX;}
  78. if (i == TOP_INDEX && EVENODD == 1) {return TOP_INDEX + 1;}
  79. if (i == TOP_INDEX && EVENODD == 0) {return TOP_INDEX;}
  80. return NUM_LEDS - i;
  81. }
  82.  
  83. //---FIND INDEX OF ANTIPODAL OPPOSITE LED
  84. int antipodal_index(int i) {
  85. int iN = i + TOP_INDEX;
  86. if (i >= TOP_INDEX) {iN = ( i + TOP_INDEX ) % NUM_LEDS; }
  87. return iN;
  88. }
  89.  
  90. void copy_led_array(){
  91. for(int i = 0; i < NUM_LEDS; i++ ) {
  92. ledsX[i][0] = leds[i].r;
  93. ledsX[i][1] = leds[i].g;
  94. ledsX[i][2] = leds[i].b;
  95. }
  96. }
  97.  
  98.  
  99. void one_color_all(int cred, int cgrn, int cblu) { //-SET ALL LEDS TO ONE COLOR
  100. for(int i = 0 ; i < NUM_LEDS; i++ ) {
  101. leds[i].setRGB( cred, cgrn, cblu);
  102. }
  103. }
  104.  
  105. /*
  106. void one_color_allHSV(int ahue) { //-SET ALL LEDS TO ONE COLOR (HSV)
  107. for(int i = 0 ; i < NUM_LEDS; i++ ) {
  108. leds[i] = CHSV(ahue, thissat, 255);
  109. }
  110. }
  111. */
  112.  
  113. void rainbow_loop() { //-m3-LOOP HSV RAINBOW
  114. idex++;
  115. ihue = ihue + thisstep;
  116. if (idex >= NUM_LEDS) {idex = 0;}
  117. if (ihue > 255) {ihue = 0;}
  118. leds[idex] = CHSV(ihue, thissat, 255);
  119. LEDS.show();
  120. delay(thisdelay);
  121. }
  122.  
  123. void random_burst() { //-m4-RANDOM INDEX/COLOR
  124. idex = random(0, NUM_LEDS);
  125. ihue = random(0, 255);
  126. leds[idex] = CHSV(ihue, thissat, 255);
  127. LEDS.show();
  128. delay(thisdelay);
  129. }
  130.  
  131.  
  132. void fade_vertical() { //-m12-FADE 'UP' THE LOOP
  133.  
  134. idex++;
  135. if (idex > TOP_INDEX) {idex = 0;}
  136. int idexA = idex;
  137. int idexB = horizontal_index(idexA);
  138. ibright = ibright + 10;
  139. if (ibright > 255) {ibright = 0;}
  140. leds[idexA] = CHSV(thishue, thissat, ibright);
  141. leds[idexB] = CHSV(thishue, thissat, ibright);
  142. LEDS.show();
  143. delay(thisdelay);
  144. }
  145.  
  146. void color_loop_vardelay() { //-m17-COLOR LOOP (SINGLE LED) w/ VARIABLE DELAY
  147. int di = abs(TOP_INDEX - idex);
  148. int t = constrain((10/di)*10, 10, 500);
  149. idex++;
  150. if (idex > NUM_LEDS) {idex = 0;}
  151. for(int i = 0; i < NUM_LEDS; i++ ) {
  152. if (i == idex) {
  153. leds[i] = CHSV(0, thissat, 255);
  154. }
  155. else {
  156. leds[i].r = 0; leds[i].g = 0; leds[i].b = 0;
  157. }
  158. }
  159. LEDS.show();
  160. delay(t);
  161. }
  162.  
  163. void sin_bright_wave() { //-m19-BRIGHTNESS SINE WAVE
  164. for(int i = 0; i < NUM_LEDS; i++ ) {
  165. tcount = tcount + .1;
  166. if (tcount > 3.14) {tcount = 0.0;}
  167. ibright = int(sin(tcount)*255);
  168. leds[i] = CHSV(thishue, thissat, ibright);
  169. LEDS.show();
  170. delay(thisdelay);
  171. }
  172. }
  173.  
  174. void pop_horizontal() { //-m20-POP FROM LEFT TO RIGHT UP THE RING
  175. int ix;
  176. if (bouncedirection == 0) {
  177. bouncedirection = 1;
  178. ix = idex;
  179. }
  180. else if (bouncedirection == 1) {
  181. bouncedirection = 0;
  182. ix = horizontal_index(idex);
  183. idex++;
  184. if (idex > TOP_INDEX) {idex = 0;}
  185. }
  186. for(int i = 0; i < NUM_LEDS; i++ ) {
  187. if (i == ix) {
  188. leds[i] = CHSV(thishue, thissat, 255);
  189. }
  190. else {
  191. leds[i].r = 0; leds[i].g = 0; leds[i].b = 0;
  192. }
  193. }
  194. LEDS.show();
  195. delay(thisdelay);
  196. }
  197.  
  198. void rainbow_vertical() { //-m23-RAINBOW 'UP' THE LOOP
  199. idex++;
  200. if (idex > TOP_INDEX) {idex = 0;}
  201. ihue = ihue + thisstep;
  202. if (ihue > 255) {ihue = 0;}
  203. int idexA = idex;
  204. int idexB = horizontal_index(idexA);
  205. leds[idexA] = CHSV(ihue, thissat, 255);
  206. leds[idexB] = CHSV(ihue, thissat, 255);
  207. LEDS.show();
  208. delay(thisdelay);
  209. }
  210.  
  211. void random_color_pop() { //-m25-RANDOM COLOR POP
  212. idex = random(0, NUM_LEDS);
  213. ihue = random(0, 255);
  214. one_color_all(0, 0, 0);
  215. leds[idex] = CHSV(ihue, thissat, 255);
  216. LEDS.show();
  217. delay(thisdelay);
  218. }
  219.  
  220. void kitt() { //-m28-KNIGHT INDUSTIES 2000
  221. int rand = random(0, TOP_INDEX);
  222. for(int i = 0; i < rand; i++ ) {
  223. leds[TOP_INDEX+i] = CHSV(thishue, thissat, 255);
  224. leds[TOP_INDEX-i] = CHSV(thishue, thissat, 255);
  225. LEDS.show();
  226. //delay(thisdelay/rand);
  227. }
  228. for(int i = rand; i > 0; i-- ) {
  229. leds[TOP_INDEX+i] = CHSV(thishue, thissat, 0);
  230. leds[TOP_INDEX-i] = CHSV(thishue, thissat, 0);
  231. LEDS.show();
  232. delay(thisdelay/rand);
  233. }
  234. }
  235.  
  236. void matrix() { //-m29-ONE LINE MATRIX
  237. int rand = random(0, 100);
  238. if (rand > 90) {
  239. leds[0] = CHSV(thishue, thissat, 255);
  240. }
  241. else {leds[0] = CHSV(thishue, thissat, 0);}
  242. copy_led_array();
  243. for(int i = 1; i < NUM_LEDS; i++ ) {
  244. leds[i].r = ledsX[i-1][0];
  245. leds[i].g = ledsX[i-1][1];
  246. leds[i].b = ledsX[i-1][2];
  247. }
  248. LEDS.show();
  249. delay(thisdelay);
  250. }
  251.  
  252. void new_rainbow_loop(){ //-m88-RAINBOW FADE FROM FAST_SPI2
  253. ihue -= 1;
  254. fill_rainbow( leds, NUM_LEDS, ihue );
  255. LEDS.show();
  256. delay(thisdelay);
  257. }
  258.  
  259.  
  260. //----------------------------------------SETUP AND LOOP---------------------------------------------
  261.  
  262.  
  263. void setup() {
  264. FastLED.addLeds<LED_TYPE,LED_LEAD,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  265.  
  266. FastLED.setBrightness(BRIGHTNESS);
  267. }
  268.  
  269. void loop() {
  270. int r = 10;
  271.  
  272.  
  273. thisdelay = 20; thisstep = 10; thishue = 0; thissat = 255;
  274. //one_color_all(255,255,255); LEDS.show(); delay(200);
  275.  
  276. for(int i=0; i<r*20; i++) {rainbow_loop();}
  277.  
  278. for(int i=0; i<r*20; i++) {random_burst();}
  279.  
  280. thisdelay = 60; thishue = 180;
  281. for(int i = 0; i<r*5; i++) {fade_vertical();}
  282.  
  283. thisdelay = 60; thishue = 95;
  284. for(int i=0; i<r*15; i++) {color_loop_vardelay();}
  285.  
  286. thisdelay = 35; thishue = 180;
  287. for(int i=0; i<r; i++) {sin_bright_wave();}
  288.  
  289. thisdelay = 100; thishue = 0;
  290. for(int i=0; i<r*5; i++) {pop_horizontal();}
  291.  
  292. thisdelay = 50; thisstep = 15;
  293. for(int i=0; i<r*12; i++) {rainbow_vertical();}
  294.  
  295. thisdelay = 35;
  296. for(int i=0; i<r*10; i++) {random_color_pop();}
  297.  
  298. thisdelay = 100; thishue = 95;
  299. for(int i=0; i<r*10; i++) {kitt();}
  300.  
  301. thisdelay = 30; thishue = 95;
  302. for(int i=0; i<r*25; i++) {matrix();}
  303.  
  304.  
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment