xmas-tree-full-fade.ino

#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

/* 
 *  This sketch controls an Adafruit Neopixel Sheild (5x8 grid) and
 *  was designed to run on an Arduino Uno. I created it to replace the
 *  broken lighting system on my fiber-optic Christmas tree. The previous
 *  lighting system had used a halogen light and a rotating colored disk.
 *  This is quieter, energy efficient, and won't melt the plastic tree stand.
 *  
 *  This version does a fade on the entire Christmas tree, one color to another.
 *  It was not the version I ultimately used for the tree. Though insights in
 *  this version led to the final version, xmas-tree-sections-face.ino
 *  
 *  Written 12-24-2021
 */


  Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(5, 8, 6,
  NEO_MATRIX_TOP     + NEO_MATRIX_RIGHT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
  NEO_RGBW + NEO_KHZ800); 

// define colors

  int green[4]={255,0,0,0};
  int red[4] = {0,255,0,0};
  int blue[4] = {0,0,255,0};
  int white[4] = {0,0,0,255};


  // fill the entire neopixel sheild with a given color
  void fillSheild (int* color){
    
    for (int i=0;i<64;i++){
      matrix.setPixelColor(i,color[0],color[1],color[2],color[3]);
    }
    
  }

  // Broke these into separate functions because only
  // one for loop would execute each function. Very odd.

  void fadeBlueRed(int timer){
    
    int color[4] = {0,0,255,0};
    
    fillSheild(color);
    matrix.show();
    
    for (int i=0; i<255; i++){
      
      color[2]=color[2]-1;
      color[1]=color[1]+1;

      fillSheild(color);
      matrix.show();
      delay(timer);
    }
  }

  void fadeRedGreen(int timer){
    
    int color[4] = {0,255,0,0};
    
    fillSheild(color);
    matrix.show();
    
    for (int i=0; i<255; i++){
      
      color[1]=color[1]-1;
      color[0]=color[0]+1;

      fillSheild(color);
      matrix.show();
      delay(timer);
    }
  }

  void fadeGreenWhite(int timer){
    
    int color[4] = {255,0,0,0};
    
    fillSheild(color);
    matrix.show();
    
    for (int i=0; i<255; i++){
      
      color[0]=color[0]-1;
      color[3]=color[3]+1;

      fillSheild(color);
      matrix.show();
      delay(timer);
    }
  }

    void fadeWhiteBlue(int timer){
    
    int color[4] = {0,0,0,255};
    
    fillSheild(color);
    matrix.show();
    
    for (int i=0; i<255; i++){
      
      color[3]=color[3]-1;
      color[2]=color[2]+1;

      fillSheild(color);
      matrix.show();
      delay(timer);
    }
  }



void setup() {
  // put your setup code here, to run once:

  matrix.begin();
  matrix.show(); // Initialize all pixels to 'off'


}

void loop() {
  // put your main code here, to run repeatedly:

    //full tree continuous fades

    fadeBlueRed(50);
    fadeRedGreen(50);
    fadeGreenWhite(50);
    fadeWhiteBlue(50);
}