Advertisement
mfuller016

infinity 3D Figure 8

Mar 1st, 2021
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.80 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN     5
  4. #define COLOR_ORDER RBG
  5. #define CHIPSET     WS2811
  6. #define NUM_LEDS    100
  7.  
  8. #define BRIGHTNESS  255
  9. #define FRAMES_PER_SECOND 150
  10. //bool gReverseDirection = true;
  11.  
  12. CRGB leds[NUM_LEDS];
  13.  
  14. void setup() {
  15.  
  16.   FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  17.   FastLED.setBrightness( BRIGHTNESS );
  18. }
  19. void loop()
  20. {
  21.   infinity();
  22. }
  23.  
  24.  
  25. /*******************************************************************
  26.  **** Infinity = makes a figure 8 out of two circles laying flat****
  27.  * *****************************************************************/
  28. void infinity()
  29. {
  30. /*
  31.     Here we have two circles both rotating at the same rate.  The dot tracing the circumference of one circle
  32.     is blacked out during the time when the other circle is lit up.  This is what creates the figure 8 (or
  33.     if you will, the infinity symbol)  Written by Mike Fuller but a lot of credit has to go to the authors of
  34.     FastLED.  I had been using a radioshack program to run my LEDs for years and just recently (2020) became
  35.     aware of what FastLED can do.  The color handling and that fadeToBlackBy() function are dynamite.  I
  36.     could've used the FastLED sin and cos functions but the arduino functions were adequate here.
  37.  
  38.     I think you could use this scheme to have a 3D serpent run up and down your led string without much work.
  39.     just use smaller circles and more of them.
  40.  */
  41. boolean fig;
  42.  
  43. // These variables are for the right circle
  44. int j,xpoint,quarter,blue,loopcounter,bright;
  45. float firstx,lastx,lasty,nextx,nexty,ledpos,lastledpos,brightpos;
  46. float angle;
  47.  
  48.  
  49. //r parameters***********************These are for the left circle
  50. int jr,xpointr,quarterr,bluer, countr,brightr;
  51. float firstxr,lastxr,lastyr,nextxr,nextyr,ledposr,lastledposr,brightposr;
  52. float angler;
  53.  
  54. //Set starting values here
  55.  
  56.   fig = true;            //this variable determines where we are in the figure 8 true if we are on the right.
  57.   angle = -1.2;          //set this to change the speed of rotation higher numbers are faster (it's degrees or rotation for
  58.                          //each FastLED.show() command loop.  Use a negative number to rotate backwards - programming later
  59.                          //expects backward rotation.
  60.   firstx=lastx = 40;     //set this to change the diameter of the circle
  61.   lasty = 0;             //can be changed but no good reason for it here.  By setting it at zero you start your
  62.                          //circle at the ordered pair coordinate(40,0) [(x,y}]
  63.  
  64. //r values*******************for the left circle - why not use "L" you ask?
  65.  
  66.   angler = -1.2;
  67.   firstxr = lastxr= -40; //here we want the left circle to start at (-40,0) so it hits the midpoint at just the right time
  68.                          //also note that both circles must spin at the same rate (same angle)
  69.   lastyr = 0;            //starting last "y" position for the left, left circle - not the right one.
  70.  
  71.   for (j = 0;j < 100; j++)leds[j].setRGB(0,0,0); //sets all the leds to off. I know, theres a function for that.
  72.  
  73. while(1)
  74. {
  75.  
  76.  
  77.  
  78. while(1)
  79.      { //endless loop - need to change this if used with other subroutines
  80.  
  81.       nextx = (lastx*cos(angle*PI/180)-lasty*sin(angle*PI/180)); //formulae for next x coordinate and next y coordinate after
  82.       nexty = (lasty*cos(angle*PI/180)+lastx*sin(angle*PI/180)); // "angle" degrees of rotation.
  83.      
  84.       ledpos = (nextx+140)/2;  // calculates where in your array of leds the nextx position is (because next x values go
  85.                                // negative as you spin around a circle - so do the y values.  Add 140 to keep ledpos from
  86.                                // going negative and establishes the center point of the right circle
  87.       brightpos=(nexty+140)/2; // This gives a range of values based on how close to the front the LED is supposed to be
  88.          
  89.            
  90.       if ((nextx>0)&&(nexty>0)) quarter = 1;  //these lines determine what quadrant on an (x,y) plot the circle is in.
  91.       if ((nextx<0)&&(nexty>0)) quarter = 2;
  92.       if ((nextx<0)&&(nexty<0)) quarter = 3;
  93.       if ((nextx>0)&&(nexty<0)) quarter = 4;
  94.  
  95.       if (lasty<=0 && nexty>=0) fig=!fig; //This is the trigger that determines which half of the circle is lit
  96.                                           //only need to set this once and then its go for both circles until it flops.
  97.            
  98.       bright = 85 * (brightpos-(70-(firstx/2)))/firstx; // setup to give brightest light at middle of front and
  99.                                                         // dim but more or less visible all the way at the back
  100.        
  101.       blue = 80+(bright*3)*.5 ;                         // more tweaking on brightness - started off with blue dots
  102.                                                         // never got around to changing the variable name
  103.      
  104.       xpoint=int(ledpos);                               // need an integer to index the array of LEDs
  105.      
  106.       leds[70]=CHSV(25,200,100); //turns on a  dot in the middle of the string for the moving dot(s) to pass in front of
  107.                                  //when the dot is going in front of the center point, and behind it when going around
  108.                                  //the back of the circle
  109.       if (fig && !(xpoint == 70)) leds[xpoint].setRGB(0,0,0); // this keeps the right circle dark while the dot is passing
  110.                                                               // along the backside of its rotation, when the left circle is
  111.                                                               // on the front of its rotation.
  112.       else
  113.       if (!fig ) //now we want to see the light
  114.          {
  115.          
  116.             leds[xpoint]= CHSV(192,255,blue);
  117.             leds[xpoint] %= blue;
  118.          
  119.         }
  120.      
  121.       if(((xpoint>67)&&(xpoint<77))&&(quarter<3)&&!fig) //Turn the center point off as we pass by
  122.         {
  123.            
  124.            
  125.            leds[70].setRGB(0,0,0);
  126.            if (fig) leds[xpoint].setRGB(0,0,0);
  127.            
  128.        
  129.            }
  130.            
  131.    
  132.       if((quarter>2)&&(xpoint == 70))leds[70]= CHSV(25,200,100);  //keeps center point light on here.
  133.  
  134.       lastx=nextx;
  135.       lasty=nexty;
  136. //r parameters *************************************************************************
  137.        nextxr = (lastxr*cos(angler*PI/180)-lastyr*sin(angler*PI/180));
  138.        nextyr = (lastyr*cos(angler*PI/180)+lastxr*sin(angler*PI/180));
  139.      
  140.        ledposr=(nextxr+60)/2;
  141.        brightposr=(nextyr+60)/2;
  142.        if ((nextxr>0)&&(nextyr>0)) quarterr = 1;
  143.        if ((nextxr<0)&&(nextyr>0)) quarterr = 2;
  144.        if ((nextxr<0)&&(nextyr<0)) quarterr = 3;
  145.        if ((nextxr>0)&&(nextyr<0)) quarterr = 4;
  146.        
  147.        brightr = 85 * (brightposr-(30-(firstxr/2)))/firstxr;
  148.      
  149.        bluer = 80+(brightr*3)*.5 ;
  150.        xpointr=int(ledposr);
  151.    
  152.       leds[30]= CHSV(25,200,100);
  153.       if (fig)
  154.         {
  155.         //here's where we want to see the light- opposite of right circle
  156.         leds[xpointr]= CHSV(192,255,bluer);
  157.         leds[xpointr]%=bluer;
  158.         }
  159.      
  160.       else
  161.       if (!fig && !(xpointr==30)) leds[xpointr].setRGB(0,0,0);
  162.      
  163.       if(((xpointr>24)&&(xpointr<36))&&(quarterr>3)&&fig)
  164.          {
  165.           leds[xpointr]= CHSV(192,255,bluer);
  166.           leds[30].setRGB(0,0,0);
  167.          }
  168.      
  169.  
  170.    
  171.       if((quarterr<3)&&(xpointr == 30))
  172.       {
  173.         leds[30] = CHSV(25,200,100);
  174.    
  175.       }
  176.        
  177.       lastxr=nextxr;
  178.       lastyr=nextyr;  
  179.       fadeToBlackBy(leds, NUM_LEDS,40);
  180.       FastLED.show();  //called once for each loop through. The X position of the lit led does not change with every loop.
  181.                        //it stays the same for a brief time at the right and left edges of each circle as would be expected.
  182.  
  183.      }
  184.      
  185.  
  186.  
  187.      
  188. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement