Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_NeoPixel.h> //include library
- #define PIN 6 // Which pin on the Arduino is connected to the NeoPixels?
- #define NUMPIXELS 5 // How many NeoPixels are attached to the Arduino?
- int fadeSpeed = 5;
- // int redFinal; // set up a series of variables to hold your target color
- // int greenFinal;
- // int blueFinal;
- // When we setup the NeoPixel library object, we tell it how many pixels, and which pin to use to send signals.
- Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
- void setup() {
- pixels.begin();
- }
- void loop() {
- customFader(200,0,100);
- }
- void customFader(int redFinal, int greenFinal, int blueFinal) {
- int redvalue = 255; // set up a series of variables to hold the current color values as they fade from black to your target color
- int greenvalue = 255;
- int bluevalue = 255;
- for (int fade = 0; fade <255; fade++){ // the fader loop runs through the whole RGB color range, from 0-255
- for(int led = 0; led <5; led++){ // the inner loop loads the color change onto each LED
- pixels.setPixelColor(led, redvalue, greenvalue, bluevalue); // this loads the current red, green and blue values onto each LED
- }
- if (redvalue >= redFinal){ // if the current red value is less than your target color, add one to the red value
- redvalue --;
- }
- else {
- redvalue = redvalue; // otherwise, show the target red value
- }
- if (greenvalue >= greenFinal){ // if the current green value is less than your target color, add one to the green value
- greenvalue --;
- }
- else {
- greenvalue = greenvalue; // otherwise, set the green color to its current setting (the target setting
- }
- if (bluevalue >= blueFinal){ // if the blue value is less than the target, add one to make it closer to the target
- bluevalue --;
- }
- else {
- bluevalue = bluevalue; // otherwise, make the blue value equal to its current setting
- }
- pixels.show(); // show all the lights!
- delay(fadeSpeed); // keep them on for the length of time that you set in fade speed
- }
- // the second loop does exactly the same thing as the first loop, it just counts down from the target values to zero.
- for (int fade = 0; fade <255; fade++){
- for(int led = 0; led <5; led++){
- pixels.setPixelColor(led, redvalue, greenvalue, bluevalue); //parameters are (LED number in chain, Red 0-255, Green 0-255, Blue 0-255)
- }
- if (redvalue < 255){
- redvalue ++;
- }
- else {
- redvalue = 255;
- }
- if (greenvalue < 255){
- greenvalue ++;
- }
- else {
- greenvalue = 255;
- }
- if (bluevalue <255){
- bluevalue ++;
- }
- else {
- bluevalue = 255;
- }
- pixels.show();
- delay(fadeSpeed);
- }
- }
Add Comment
Please, Sign In to add comment