Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <util/delay.h>
- #include <math.h>
- #include "WS2811.h"
- #define BIT(B) (0x01 << (uint8_t)(B))
- #define SET_BIT_HI(V, B) (V) |= (uint8_t)BIT(B)
- #define SET_BIT_LO(V, B) (V) &= (uint8_t)~BIT(B)
- #define PAUSE 1000 // msec
- #define DELAY 10 // msec
- #define CONST_E 2.71828
- // Define the output function, using pin 0 on port b.
- DEFINE_WS2811_FN(WS2811RGB, PORTB, 0)
- void setup()
- {
- // Configure pin for output.
- SET_BIT_HI(DDRB, 0);
- SET_BIT_LO(PORTB, 0);
- // Serial.begin(9600);
- }
- float cR = 1,
- cG = 0,
- cB = 0;
- // Drive the three pixels in an infinite loop.
- void loop(void)
- {
- // off->red, off->green, off->blue
- RGB_t rgb[240] = {
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},
- {0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}
- };
- const int Elements = ARRAYLEN(rgb);
- WS2811RGB(rgb, Elements);
- _delay_ms(PAUSE);
- int col = 0;
- // loop forever.
- for (;;) {
- for(int k=0; k<10; k++) {
- for(int i = Elements - 1; i >= 1; i--)
- {
- rgb[i].r = rgb[i - 1].r;
- rgb[i].g = rgb[i - 1].g;
- rgb[i].b = rgb[i - 1].b;
- }
- long micLevel = analogRead(0);
- int log4Level = log(micLevel) / log(4) * 12.5;
- int squareLevel = micLevel * micLevel * micLevel / ((long)4 * 1024 * 1024);
- float sCurveLevel = (int)(sCurve(micLevel, 4) * 255);
- float fMicLevel = micLevel / 4.0f / 200.0f;
- float quadraticLevel = pow(fMicLevel, 2);
- //Serial.println(squareLevel);
- const float one = 1.f;
- rgb[0].r = rgb[0].g = rgb[0].b = 0;
- setRgb(&(rgb[0]), (squareLevel / 2.0f));
- //the lighting of the LED's works on a 0 to 1 scale
- // 0 is off and 1 is full bright and anything in between dims the light
- // in the code "(squareLevel / 0.2f))" the number 0.2 restricts the range between on and off
- // increasing that number, increases the range but also makes the lights less responsive
- //if(log4Level > squareLevel && log4Level > sCurveLevel)
- //rgb[0].r = log4Level;
- //else if(squareLevel > log4Level && squareLevel > sCurveLevel)
- // rgb[0].g = quadraticLevel * 255;
- //else if(sCurveLevel > log4Level && sCurveLevel > squareLevel)
- //rgb[0].b = sCurveLevel;
- WS2811RGB(rgb, Elements);
- //_delay_ms(5);
- fadeRgb();
- }
- //_delay_ms(500);
- col = ++col % 6; // no idea
- cycleRgb(col); // makes them blinky
- }
- }
- const int phaseLength = 5; // thinking that it dose the color length or the length of the power coming in
- const int period = phaseLength * 5;
- int iteration = 0;
- void fadeRgb() {
- // Determine which phase we are in
- int phase = iteration / phaseLength;
- int step = iteration % phaseLength;
- switch(phase)
- {
- case 0: // Red on, green increasing, blue off
- cR = 1;
- cG = step / ((float)phaseLength);
- cB = 0;
- break;
- case 1: // Red decreasing, green on, blue off
- cR = (phaseLength - step) / ((float)phaseLength);
- cG = 1;
- cB = 0;
- break;
- case 2: // Red off, green on, blue increasing
- cR = 0;
- cG = 1;
- cB = step / ((float)phaseLength);
- break;
- case 3: // Red off, green decreasing, blue on
- cR = 0;
- cG = (phaseLength - step) / ((float)phaseLength);
- cB = 1;
- break;
- case 4: // Red increasing, green off, blue on
- cR = step / ((float)phaseLength);
- cG = 0;
- cB = 1;
- break;
- case 5: // Red on, green off, blue decreasing
- cR = 1;
- cG = 0;
- cB = (phaseLength - step) / ((float)phaseLength);
- break;
- }
- iteration = ++iteration % period;
- }
- void cycleRgb(int col) {
- switch(col)
- {
- case 0:
- cR = 1;
- cG = 0;
- cB = 0;
- break;
- case 1:
- cR = 0.5;
- cG = 0.5;
- cB = 0;
- break;
- case 2:
- cR = 0;
- cG = 1;
- cB = 0;
- break;
- case 3:
- cR = 0;
- cG = 0.5;
- cB = 0.5;
- break;
- case 4:
- cR = 0;
- cG = 0;
- cB = 1;
- break;
- case 5:
- cR = 0.5;
- cG = 0;
- cB = 0.5;
- break;
- }
- }
- void setRgb(RGB_t* rgb, float val)
- {
- if (val > 1.0f) val = 1.0f;
- rgb->r = (int)(val * cR * 255);
- rgb->g = (int)(val * cG * 255);
- rgb->b = (int)(val * cB * 255);
- }
- float sCurve(int micInput, float power)
- {
- float normalised = micInput / 1024.0f;
- return normalised / (pow(1 + pow(normalised, power), 1.0f / power) );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement