mfuller016

FastLED 3D Circle with straight 100 LED Strip

Feb 22nd, 2021 (edited)
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.36 KB | None | 0 0
  1. /*
  2. This code creates a spinning circle that is oriented in a horizontal plane like a bicycle wheel laying on its side.  A single LED appears to rotate around the circumference and there is one light in the middle.  It runs on an ordinary straight addressable LED light Strip.
  3. It creates a very good 3D effect.  Derivative works can include multiple circles with different colors and directions, and even figure 8s.  Those are left to your imagination.*/
  4.  
  5. #include <FastLED.h>
  6.  
  7. #define LED_PIN     5
  8. #define COLOR_ORDER BRG
  9. #define CHIPSET     WS2811
  10. #define NUM_LEDS    100
  11.  
  12.  
  13.  
  14.  
  15. CRGB leds[NUM_LEDS];
  16.  
  17. void setup() {
  18.    
  19.  
  20. //delay(3000); // sanity delay
  21. FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  22. FastLED.setBrightness( BRIGHTNESS );
  23.  
  24.   circle(1);
  25.  
  26. }
  27.  
  28.  
  29.  
  30. /*****************************************************************
  31.  **** circle - uses trig functions to drive a circular orbit ****
  32.  * **************************************************************/
  33. void circle(int reps)
  34. {
  35.  
  36. int j,xpoint,quarter,color;
  37. float testx,firstx,lastx,lasty,nextx,nexty,ledpos,lastledpos,brightpos;
  38. float angle;
  39. int bright, center;
  40.  
  41. center = 50; //positions the center of the circle
  42. angle = .8; //set this to change the speed of rotation higher numbers are faster (it's degrees of rotation for
  43.               //each show() command loop)
  44. firstx=lastx= 50; //set this to change the diameter of the circle
  45.  
  46. lasty=0;  //can be changed but no good reason for it.
  47.  
  48.  
  49.  
  50. while(reps--)
  51. {
  52.  
  53.   for (j=0;j<100;j++)leds[j].setRGB(0,0,0); //sets all the leds to off.
  54.   FastLED.show();
  55. while(1)
  56.      { //endless loop - need to change this if used with other subroutines
  57.  
  58.      
  59.       nextx=(lastx*cos(angle*PI/180)-lasty*sin(angle*PI/180));// These two lines calculate the next
  60.                                                               // point on the circle from any other
  61.                                                               // point on it.
  62.       nexty=(lasty*cos(angle*PI/180)+lastx*sin(angle*PI/180));
  63.      
  64.       lastledpos = (lastx+NUM_LEDS)/2; // calculates the last led to be lit so that it can be turned off.
  65.            
  66.       ledpos=(nextx+NUM_LEDS)/2;
  67.      
  68.      
  69.      
  70.  
  71.       brightpos=(nexty+NUM_LEDS)/2;
  72.      
  73.       bright = 85 * (brightpos-(center-(firstx/2)))/firstx;//want min brightness when lit led is at back of circle,
  74.                                                            // max brightness when closest to front;    
  75.            
  76.       if ((nextx>0)&&(nexty>0)) quarter = 1;
  77.       if ((nextx<0)&&(nexty>0)) quarter = 2;
  78.       if ((nextx<0)&&(nexty<0)) quarter = 3;
  79.       if ((nextx>0)&&(nexty<0)) quarter = 4;
  80.  
  81.       //  figure out which quarter of the circle we're in
  82.  
  83.       color = bright*3;
  84.          
  85.       xpoint=int(ledpos);
  86.      
  87.       leds[int(lastledpos)].setRGB(0,0,0);
  88.       leds[center].setRGB(0,150,0); //turns on a green dot in the middle of the string
  89.       leds[xpoint].setRGB(0,color,color);
  90.      
  91.       if(((xpoint>(center-3))&&(xpoint<(center+4)))&&(quarter<3)) //dims dot when moving led passes in front
  92.         {          
  93.            leds[center].setRGB(0,0,0);
  94.         }
  95.            
  96.    
  97.       if((quarter>2)&&(xpoint == center))leds[center].setRGB(0,150,0);
  98.      
  99.       lastx=nextx;
  100.       lasty=nexty;
  101.  
  102.      FastLED.show();
  103.     //delay(100);
  104.  
  105.      }
  106. }}
Add Comment
Please, Sign In to add comment