xmas-tree-sections.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 simultaneous fade on tetris-like sections of the 
 *  Neopixel sheild, replicating the "dope light" version of the original lighting
 *  system for the tree. This is the version I used.
 *  
 *  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};

// define squares on Neopixel sheild
// not using pixels on the edges because 
// they're outside the tree's fiber optic radius

  int a[6] = {1,2,9,10,17};
  int b[6] = {3,4,11,12,20};
  int c[6] = {5,6,13,14,22};
  int d[6] = {18,25,26,33,34};
  int e[6] = {19,27,28,35,36};
  int f[6] = {21,29,30,37,38};


// wrote the fade as 4 steps because I could not
// get more than one for-loop to work in a function
// and it was Christmas Eve and this needed to get done.

  void fadeStep1(int timer){
    
    int colorA[4] = {0,0,255,0}; //blue
    int colorB[4] = {0,255,0,0}; //red
    int colorC[4] = {255,0,0,0}; //green
    int colorD[4] = {0,0,0,255}; //white

    setSquareColor(a,colorA);
    setSquareColor(b,colorB);
    setSquareColor(c,colorC);
    setSquareColor(d,colorD);
    
    //duplicate colors, for simplicity
    setSquareColor(e,colorC);
    setSquareColor(f,colorA);
    
    matrix.show();

    for (int i=0; i<255; i++){
      
      colorA[2]=colorA[2]-1; //blue
      colorA[1]=colorA[1]+1; //red

      colorB[1]=colorB[1]-1; //red
      colorB[0]=colorB[0]+1; //green

      colorC[0]=colorC[0]-1; //green
      colorC[3]=colorC[3]+1; //white

      colorD[3]=colorD[3]-1; //white
      colorD[2]=colorD[2]+1; //blue

      setSquareColor(a,colorA);
      setSquareColor(b,colorB);
      setSquareColor(c,colorC);
      setSquareColor(d,colorD);
      
      //duplicate colors, for simplicity
      setSquareColor(e,colorC);
      setSquareColor(f,colorA);
      
      matrix.show(); 
      delay(timer);
      
    }

    
  }

  void fadeStep2(int timer){
    
    int colorA[4] = {0,255,0,0}; //red
    int colorB[4] = {255,0,0,0}; //green
    int colorC[4] = {0,0,0,255}; //white
    int colorD[4] = {0,0,255,0}; //blue

    setSquareColor(a,colorA);
    setSquareColor(b,colorB);
    setSquareColor(c,colorC);
    setSquareColor(d,colorD);

    //duplicate colors, for simplicity
    setSquareColor(e,colorC);
    setSquareColor(f,colorA);
      
    matrix.show();

    for (int i=0; i<255; i++){

      colorA[1]=colorA[1]-1; //red
      colorA[0]=colorA[0]+1; //green

      colorB[0]=colorB[0]-1; //green
      colorB[3]=colorB[3]+1; //white

      colorC[3]=colorC[3]-1; //white
      colorC[2]=colorC[2]+1; //blue

      colorD[2]=colorD[2]-1; //blue
      colorD[1]=colorD[1]+1; //red

      setSquareColor(a,colorA);
      setSquareColor(b,colorB);
      setSquareColor(c,colorC);
      setSquareColor(d,colorD);

      //duplicate colors, for simplicity
      setSquareColor(e,colorC);
      setSquareColor(f,colorA);
      
      matrix.show(); 
      delay(timer);
      
    }

    
  }


  void fadeStep3(int timer){
    
    int colorA[4] = {255,0,0,0}; //green
    int colorB[4] = {0,0,0,255}; //white
    int colorC[4] = {0,0,255,0}; //blue
    int colorD[4] = {0,255,0,0}; //red

    setSquareColor(a,colorA);
    setSquareColor(b,colorB);
    setSquareColor(c,colorC);
    setSquareColor(d,colorD);

    //duplicate colors, for simplicity
    setSquareColor(e,colorC);
    setSquareColor(f,colorA);
    
    matrix.show();

    for (int i=0; i<255; i++){

      colorA[0]=colorA[0]-1; //green
      colorA[3]=colorA[3]+1; //white

      colorB[3]=colorB[3]-1; //white
      colorB[2]=colorB[2]+1; //blue

      colorC[2]=colorC[2]-1; //blue
      colorC[1]=colorC[1]+1; //red

      colorD[1]=colorD[1]-1; //red
      colorD[0]=colorD[0]+1; //green

      setSquareColor(a,colorA);
      setSquareColor(b,colorB);
      setSquareColor(c,colorC);
      setSquareColor(d,colorD);

      //duplicate colors, for simplicity
      setSquareColor(e,colorC);
      setSquareColor(f,colorA);
      
      matrix.show(); 
      delay(timer);
      
    }

    
  }


    void fadeStep4(int timer){
    
    int colorA[4] = {0,0,0,255}; //white
    int colorB[4] = {0,0,255,0}; //blue
    int colorC[4] = {0,255,0,0}; //red
    int colorD[4] = {255,0,0,0}; //green

    setSquareColor(a,colorA);
    setSquareColor(b,colorB);
    setSquareColor(c,colorC);
    setSquareColor(d,colorD);

    //duplicate colors, for simplicity
    setSquareColor(e,colorC);
    setSquareColor(f,colorA);
      
    matrix.show();

    for (int i=0; i<255; i++){

      colorA[3]=colorA[3]-1; //white
      colorA[2]=colorA[2]+1; //blue

      colorB[2]=colorB[2]-1; //blue
      colorB[1]=colorB[1]+1; //red

      colorC[1]=colorC[1]-1; //red
      colorC[0]=colorC[0]+1; //green

      colorD[0]=colorD[0]-1; //green
      colorD[3]=colorD[3]+1; //white

      setSquareColor(a,colorA);
      setSquareColor(b,colorB);
      setSquareColor(c,colorC);
      setSquareColor(d,colorD);

      //duplicate colors, for simplicity
      setSquareColor(e,colorC);
      setSquareColor(f,colorA);
      
      matrix.show(); 
      delay(timer);
      
    }

    
  }
  
  // set the color of one of the predefined squares
  void setSquareColor (int* square, int* color)
  {
    for (int i=0;i<5;i++){

      matrix.setPixelColor(square[i],color[0],color[1],color[2],color[3]);
      
    }
  }

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



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:

    fadeStep1(25);
    fadeStep2(25);
    fadeStep3(25);
    fadeStep4(25);
}