Here's the full code:
// ------------------------------ LIBRARIES ------------------------------ //
#include <Wire.h>
#include <Adafruit_NeoPixel.h>
#include <LEDFader.h>
// ------------------------------ DEFINITIONS ------------------------------ //
//Status LEDs
#define LEDSTART 44
#define LEDQTY 8
// NeoPixels
#define PIN 22 // data pin for NeoPixels
#define QTY 27 // quantity of NeoPixels (DS SET)
#define ANIMSPEED 4 // overall speed of the animation
#define FADEINTERVAL 10 // the speed of the fade up of the LED
#define DELAYINT 0 // interval between each LED
#define WAITINT 255 // the wait time between the first led getting to full then starting to fade out, with 255 it starts straight away
#define FADESTEP 255 // the number of steps in the fade (generally 100 or 255)
Adafruit_NeoPixel pixel = Adafruit_NeoPixel(QTY, PIN, NEO_RGB + NEO_KHZ800);
// ------------------------------ DECLARATIONS ------------------------------ //
byte statusLED[LEDQTY];
// Xbee
char dmx;
char xbeeraw;
// State Counter declarations
char colorString[] = {'R', '2', '3', '4', '5', '6', '7', '8', '9', 'Q', 'S', 'X', 'T'};
// declarations for the Chase function
int interval = FADEINTERVAL;
byte delayint = DELAYINT;
unsigned long currenttime;
unsigned long previoustime;
int timeArray[QTY];
//final value of bright LED
uint8_t valR;
uint8_t valG;
uint8_t valB;
uint8_t fadeR;
uint8_t fadeG;
uint8_t fadeB;
// value used in the map functions
// The number of Steps between the output being on and off
const int pwmIntervals = FADESTEP;
// The R value in the graph equation
float R;
// ------------------------------ SETUP ------------------------------ //
void setup()
{
//Serial transmission
Serial.begin(115200);
// begin NeoPixel code
pixel.begin();
pixel.show();
// calculation for the LOG curve
R = (pwmIntervals * log10(2))/(log10(255));
// fill array for LED delay stuff
for (int c=0; c<(QTY+1); c++){
timeArray[c] = (c*(-DELAYINT));
}
// status LEDs
for (byte i = 5; i < 13; i++) {
pinMode(i,OUTPUT);
}
for (byte i = LEDSTART; i < (LEDSTART + LEDQTY); i++) {
pinMode(i,OUTPUT);
statusLED[i - LEDSTART] = i;
}
// Debugging Lights
pinMode(12,OUTPUT); //Power
pinMode(13,OUTPUT); //Serial TX/RX
//12v LED PWM (MOSFETs)
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
}
// ------------------------------ LOOP ------------------------------ //
void loop()
{
// Power Light
digitalWrite(12,HIGH);
// NeoPixel output
pixel.show();
// Serial read stuff
if (Serial.available() > 0) {
xbeeraw = Serial.read();
//digitalWrite(13,HIGH); // Serial RX/TX On
}
else {
xbeeraw = 'N';
//digitalWrite(13,LOW);// Serial RX/TX Off
}
if (Serial.available() > 0) {
digitalWrite(13,HIGH);
}
else {
digitalWrite(13,LOW);
}
// Switch Case
switch (xbeeraw)
{
case 'R': // button 1 (RED)
chase(255,0,0);
status(0);
break;
case '2': // button 2 (ORANGE)
chase(230,192,0);
status(1);
break;
case '3': // button 3 (YELLOW)
chase(230,217,0);
status(2);
break;
case '4': // button 4 (GREEN1) DS SET
chase(0,255,0);
status(3);
break;
case 'Q': // button 4 (GREEN2) MS SET
chase(0,255,0);
status(3);
break;
case 'S': // button 4 (GREEN3) US SET
chase(0,255,0);
status(3);
break;
case '5': // button 5 (BLUE)
chase(0,0,255);
status(4);
break;
case '6': // button 6 (INDIGO)
chase(217,0,255);
status(5);
break;
case '7': // button 7 (VIOLET)
chase(192,0,255);
status(6);
break;
case '8': // button 8 (WHITE)
chase(255,255,255);
status(7);
break;
case 'X': // Grug House
analogWrite(5,255);
break;
case 'T': // Snoot House
analogWrite(6,255);
break;
default: // default (OFF)
analogWrite(5,0);
analogWrite(6,0);
for(int i=0; i<pixel.numPixels(); i++)
{
// set all pixels to off
pixel.setPixelColor(i,0,0,0);
pixel.show();
// reset time array as per the setup
for (int c=0; c<(QTY+1); c++){
timeArray[c] = (c*(-DELAYINT));
}
}
// set status LEDs to off
for (byte i = LEDSTART; i < (LEDSTART + LEDQTY); i++) {
digitalWrite(i,LOW);
}
break;
}
}
// ------------------------------ FUNCTIONS ------------------------------ //
// Main function used to chase the LEDs up and down
void chase(byte valR, byte valG, byte valB)
{
currenttime = millis();
if(currenttime - previoustime > interval) {// tells the function the interval at which to increment the LED brightness
for(byte i=0; i<QTY; i++) {// incremments the LED so that you can light up multiple
if (timeArray[0] < WAITINT) { // fades up the LED
fadeR = constrain(map(timeArray[i], 0, 255, 0, valR),0,FADESTEP);
fadeG = constrain(map(timeArray[i], 0, 255, 0, valG),0,FADESTEP);
fadeB = constrain(map(timeArray[i], 0, 255, 0, valB),0,FADESTEP);
}
else { // fades down the LED when its reached full
fadeR = constrain(map(timeArray[i]-WAITINT, 0, 255, valR, 0),0,FADESTEP);
fadeG = constrain(map(timeArray[i]-WAITINT, 0, 255, valG, 0),0,FADESTEP);
fadeB = constrain(map(timeArray[i]-WAITINT, 0, 255, valB, 0),0,FADESTEP);
}
// sends the actual level to the LED
// pixel.setPixelColor(i,curve(fadeR),curve(fadeG),curve(fadeB));
pixel.setPixelColor(i,fadeR,fadeG,fadeB);
pixel.show();
// increases the time
timeArray[i] = timeArray[i] + ANIMSPEED;
if (timeArray[QTY-1] >= (255 + WAITINT)) { // code to end the switch case when the last LED reaches its final time value
dmx = 0;
}
}
previoustime = currenttime;
}
}
// LED Logarithmic Curve so the dimming looks nicer (value out of 0 - 255)
byte curve(uint8_t increment)
{
if (increment <= 0 || increment > FADESTEP) {
return 0;
}
else {
return pow (2, (increment / R)) - 1;
}
}
void status(byte led) {
digitalWrite(statusLED[led],HIGH);
for (byte i = LEDSTART; i < (LEDSTART + LEDQTY); i++) {
if(i != statusLED[led]) {
digitalWrite(i,LOW);
}
}
}