Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- // Based on the great example by Mark Kriegsman found here:
- // http://pastebin.com/g8Bxi6zW
- const uint8_t kCubeSize = 8;
- const uint8_t kFracs = 16;
- #define DATA_PIN 2
- #define NUM_LEDS kCubeSize * kCubeSize * kCubeSize
- #define MAX_POS (kCubeSize-1)*kFracs
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- #define MASTER_BRIGHTNESS 255
- #define FADE 230
- CRGB leds[NUM_LEDS];
- int F16posX = 0; // x position of the pixel
- int F16posY = 0; // y position of the pixel
- int F16posZ = 0; // z position of the pixel
- uint8_t hue = 0;
- void setup() {
- delay(3000); // setup guard
- FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(MASTER_BRIGHTNESS);
- }
- uint16_t XYZ( uint8_t x, uint8_t y, uint8_t z)
- {
- uint16_t i;
- i = kCubeSize * ((y * kCubeSize) + x) + z;
- if ((i >= 0) && (i <= 512))
- return i;
- else
- return 0;
- }
- // Draw a pixel on a matrix using fractions of light. Positions are measured in
- // sixteenths of a pixel. Fractional positions are
- // rendered using 'anti-aliasing' of pixel brightness.
- void drawFractionalPixel()
- {
- uint8_t x = F16posX / kFracs; // convert from pos to raw pixel number
- uint8_t y = F16posY / kFracs; // convert from pos to raw pixel number
- uint8_t z = F16posZ / kFracs; // convert from pos to raw pixel number
- uint8_t fracX = F16posX & 0x0F; // extract the 'factional' part of the position
- uint8_t fracY = F16posY & 0x0F; // extract the 'factional' part of the position
- uint8_t fracZ = F16posZ & 0x0F; // extract the 'factional' part of the position
- uint8_t px = 255 - (fracX * kFracs);
- uint8_t py = 255 - (fracY * kFracs);
- uint8_t pz = 255 - (fracZ * kFracs);
- leds[XYZ(x,y,z)] += CHSV( hue, 255, scale8(px, scale8(py, pz)));
- leds[XYZ(x,y+1,z)] += CHSV( hue, 255, scale8(px, scale8((255-py), pz)));
- leds[XYZ(x,y,z+1)] += CHSV( hue, 255, scale8(px, scale8(py, (255-pz))));
- leds[XYZ(x,y+1,z+1)] += CHSV( hue, 255, scale8(px, scale8((255-py), (255-pz))));
- leds[XYZ(x+1,y,z)] += CHSV( hue, 255, scale8((255-px), scale8(py, pz)));
- leds[XYZ(x+1,y+1,z)] += CHSV( hue, 255, scale8((255-px), scale8((255-py), pz)));
- leds[XYZ(x+1,y,z+1)] += CHSV( hue, 255, scale8((255-px), scale8(py, (255-pz))));
- leds[XYZ(x+1,y+1,z+1)] += CHSV( hue, 255, scale8((255-px), scale8((255-py), (255-pz))));
- }
- void loop()
- {
- nscale8(leds, NUM_LEDS, FADE);
- F16posX = beatsin16(143, 0, MAX_POS);
- F16posY = beatsin16(91, 0, MAX_POS);
- F16posZ = beatsin16(74, 0, MAX_POS);
- drawFractionalPixel();
- hue++;
- FastLED.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment