Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This #include statement was automatically added by the Particle IDE.
- #include <neopixel.h>
- #include <math.h>
- void spin(int R, int G, int B);
- // IMPORTANT: Set pixel COUNT, PIN and TYPE
- #define PIXEL_COUNT 7
- #define PIXEL_TYPE WS2812
- #define RED 155,10,10
- #define GREEN 10,155,10
- #define BLUE 5,5,155
- const float STEPS = 1.0f / PIXEL_COUNT;
- float fRand()
- {
- const int max = 0xFF;
- float val = ((float)random(max)) / max;
- return val;
- }
- class Color {
- private:
- const float gamma = 1.0f / 2.2f;
- public:
- Color()
- {
- }
- Color(float r, float g, float b)
- :r(r),g(g),b(b)
- {
- }
- float r;
- float g;
- float b;
- int red(bool gammacorrect = false) const
- {
- float value = pow(r, gammacorrect ? gamma : 1.0f);
- return (int)(value * 0xFF) % 0xFF;
- }
- int green(bool gammacorrect = false) const
- {
- float value = pow(g, gammacorrect ? gamma : 1.0f);
- return (int)(value * 0xFF) % 0xFF;
- }
- int blue(bool gammacorrect = false) const
- {
- float value = pow(b, gammacorrect ? gamma : 1.0f);
- return (int)(value * 0xFF) % 0xFF;
- }
- void set(float r, float g, float b)
- {
- this->r = r;
- this->g = g;
- this->b = b;
- }
- void add(const Color& other, float amount = 1.0f)
- {
- this->r += other.r * amount;
- this->g += other.g * amount;
- this->b += other.b * amount;
- }
- void randomize()
- {
- delay(1);
- r = fRand();
- g = fRand();
- b = fRand();
- }
- Color& operator=(const Color& other)
- {
- r = other.r;
- g = other.g;
- b = other.b;
- return *this;
- }
- };
- class Block {
- public:
- Adafruit_NeoPixel strip;
- int at = 0;
- int position = 0;
- Color color;
- Color pixels[PIXEL_COUNT];
- Block(int port)
- : strip(PIXEL_COUNT, port, PIXEL_TYPE)
- {
- }
- void init()
- {
- //color.randomize();
- strip.begin();
- for(int i=0; i < PIXEL_COUNT; i++) {
- pixels[i] = color;
- }
- }
- void update()
- {
- static const int livePixelCount = PIXEL_COUNT -1;
- at++;
- const float fade = 0.05f;
- for(int i=1; i < PIXEL_COUNT; i++) {
- Color& pixel = pixels[i];
- pixel.r -= pixel.r * 0.25f;
- pixel.g -= pixel.g * 0.25f;
- pixel.b -= pixel.b * 0.25f;
- /*
- pixel.r -= fade;
- pixel.g -= fade;
- pixel.b -= fade;
- if(pixel.r < 0.0f ) pixel.r = 0.0f;
- if(pixel.g < 0.0f ) pixel.g = 0.0f;
- if(pixel.b < 0.0f ) pixel.b = 0.0f;
- */
- }
- color.r -= color.r * 0.01f;
- color.g -= color.g * 0.01f;
- color.b -= color.b * 0.01f;
- if((at % 600) == 0) {
- color.randomize();
- }
- pixels[0] = color;
- if(at % 4 == 0) {
- ++position;
- pixels[(position % livePixelCount) + 1].add(color);
- }
- }
- void draw()
- {
- int r,g,b;
- for(int i=0; i < PIXEL_COUNT; i++) {
- const Color& pixel = pixels[i];
- strip.setColor(i, pixel.red(true) , pixel.green(true), pixel.blue(true));
- }
- strip.show();
- //strip.setBrightness(j);
- }
- };
- Block strip = Block(D4);
- Block strip2 = Block(D5);
- Block strip3 = Block(D6);
- void setup() {
- strip.color.set(1.0f, 0.0f, 0.0f);
- strip2.color.set(0.0f, 1.0f, 0.0f);
- strip3.color.set(0.0f, 0.0f, 1.0f);
- strip.init();
- strip2.init();
- strip3.init();
- uint32_t seed = millis();
- randomSeed(seed);
- }
- void loop() {
- strip.update();
- strip2.update();
- strip3.update();
- strip.draw();
- strip2.draw();
- strip3.draw();
- delay(4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement