Guest User

Untitled

a guest
Mar 19th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. #include <Adafruit_NeoPixel.h> //include library
  2.  
  3. #define PIN 6 // Which pin on the Arduino is connected to the NeoPixels?
  4. #define NUMPIXELS 5 // How many NeoPixels are attached to the Arduino?
  5.  
  6. int fadeSpeed = 5;
  7.  
  8. // int redFinal; // set up a series of variables to hold your target color
  9. // int greenFinal;
  10. // int blueFinal;
  11.  
  12.  
  13. // When we setup the NeoPixel library object, we tell it how many pixels, and which pin to use to send signals.
  14. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
  15.  
  16. void setup() {
  17. pixels.begin();
  18. }
  19.  
  20. void loop() {
  21.  
  22. customFader(200,0,100);
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29.  
  30. void customFader(int redFinal, int greenFinal, int blueFinal) {
  31.  
  32. int redvalue = 255; // set up a series of variables to hold the current color values as they fade from black to your target color
  33. int greenvalue = 255;
  34. int bluevalue = 255;
  35.  
  36. for (int fade = 0; fade <255; fade++){ // the fader loop runs through the whole RGB color range, from 0-255
  37.  
  38. for(int led = 0; led <5; led++){ // the inner loop loads the color change onto each LED
  39.  
  40. pixels.setPixelColor(led, redvalue, greenvalue, bluevalue); // this loads the current red, green and blue values onto each LED
  41. }
  42.  
  43. if (redvalue >= redFinal){ // if the current red value is less than your target color, add one to the red value
  44. redvalue --;
  45. }
  46. else {
  47. redvalue = redvalue; // otherwise, show the target red value
  48. }
  49.  
  50. if (greenvalue >= greenFinal){ // if the current green value is less than your target color, add one to the green value
  51. greenvalue --;
  52. }
  53. else {
  54. greenvalue = greenvalue; // otherwise, set the green color to its current setting (the target setting
  55. }
  56.  
  57. if (bluevalue >= blueFinal){ // if the blue value is less than the target, add one to make it closer to the target
  58. bluevalue --;
  59. }
  60. else {
  61. bluevalue = bluevalue; // otherwise, make the blue value equal to its current setting
  62. }
  63. pixels.show(); // show all the lights!
  64. delay(fadeSpeed); // keep them on for the length of time that you set in fade speed
  65.  
  66. }
  67.  
  68. // the second loop does exactly the same thing as the first loop, it just counts down from the target values to zero.
  69. for (int fade = 0; fade <255; fade++){
  70. for(int led = 0; led <5; led++){
  71. pixels.setPixelColor(led, redvalue, greenvalue, bluevalue); //parameters are (LED number in chain, Red 0-255, Green 0-255, Blue 0-255)
  72. }
  73. if (redvalue < 255){
  74. redvalue ++;
  75. }
  76. else {
  77. redvalue = 255;
  78. }
  79.  
  80. if (greenvalue < 255){
  81. greenvalue ++;
  82. }
  83. else {
  84. greenvalue = 255;
  85. }
  86.  
  87. if (bluevalue <255){
  88. bluevalue ++;
  89. }
  90. else {
  91. bluevalue = 255;
  92. }
  93. pixels.show();
  94. delay(fadeSpeed);
  95.  
  96. }
  97. }
Add Comment
Please, Sign In to add comment