ldirko

sand automata

Dec 16th, 2020 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Simple sand automata
  2. //fastled 16x16 matrix demo
  3. //Yaroslaw Turbin 14.12.2020
  4. //https://vk.com/ldirko
  5. //https://www.reddit.com/user/ldirko/
  6.  
  7. //for non commercial use! ))
  8.  
  9. #define hue 30
  10.  
  11. void randomdot(){
  12. byte a= LED_COLS/2; //random8(LED_COLS/4)+LED_COLS*3/8; //
  13. if (!random8(4)) leds[XY(a,LED_ROWS-1)].setHue(random8(hue,hue+45)); // 0 or 1    
  14. }
  15.  
  16. void updatesand (){
  17. int index, indexXadd1Y, indexXsub1Y, indexXYadd1;  
  18. for (int y=0; y<LED_ROWS-1; y++) {
  19. for (int x=1; x<LED_COLS-1; x++) {
  20. index = XY(x,y); indexXadd1Y = XY(x+1,y); indexXsub1Y = XY(x-1,y); indexXYadd1 = XY(x,y+1);
  21.   if (!leds[index] && !leds[indexXYadd1]) continue;
  22.   if (!leds[index] && leds[indexXYadd1]) {leds[index]=leds[indexXYadd1]; leds[indexXYadd1]=0;}
  23.   if (leds[index] && leds[indexXYadd1] && !leds[indexXsub1Y] && !leds[indexXadd1Y])
  24.       if (random8(2)) {leds[indexXsub1Y]=leds[indexXYadd1]; leds[indexXYadd1]=0;}
  25.         else {leds[indexXadd1Y]=leds[indexXYadd1]; leds[indexXYadd1]=0;}
  26.   if (leds[index] && leds[indexXYadd1] && !leds[indexXsub1Y] && leds[indexXadd1Y]) {leds[indexXsub1Y]=leds[indexXYadd1]; leds[indexXYadd1]=0;}
  27.   if (leds[index] && leds[indexXYadd1] && leds[indexXsub1Y] && !leds[indexXadd1Y]) {leds[indexXadd1Y]=leds[indexXYadd1]; leds[indexXYadd1]=0;}
  28. }}
  29. }
  30.  
  31. void randomdel(){
  32.  for (int i=0; i<N_LEDS; i++) {
  33.    if (!random8(3)) leds[i]=0;
  34. }
  35. }
  36.  
  37. void falldown(){
  38. for (int y=0; y<LED_ROWS-1; y++) {
  39. for (int x=0; x<LED_COLS; x++) {
  40.   if (!leds[XY(x,y)] && leds[XY(x,y+1)]) {leds[XY(x,y)]=leds[XY(x,y+1)]; leds[XY(x,y+1)]=0;}
  41. }}
  42. }
  43.  
  44. void draw() {
  45. EVERY_N_MILLISECONDS(80) {updatesand(); randomdot(); }    
  46. EVERY_N_MILLISECONDS(10000) {randomdel(); falldown(); falldown(); falldown();}
  47. }
  48.  
  49.  
  50.  
Add Comment
Please, Sign In to add comment