Got it. Can't thank you enough. Went through and started from the ground up. This code works flawlessly, even blacks out nice and slow. I made a choice to have slower response and a nice fade, though if I put the "if ( mode !=1)" -then break code in the crossFade function it was an instant blackout.
#include <Conceptinetics.h>
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 100, 100, 100 };
int red[3] = { 100, 0, 0 };
int green[3] = { 0, 100, 0 };
int blue[3] = { 0, 0, 100 };
int yellow[3] = { 100, 50, 0 };
int orange[3] = { 100, 25, 0 };
int purple[3] = { 80, 0, 100};
int pink [3] = { 100, 0, 45};
int teal [3] = { 0, 70, 100};
// Set initial color
int redVal = black[0];
int grnVal = black[1];
int bluVal = black[2];
int wait = 5; // 10ms internal crossFade delay; increase for slower fades
int hold = 0; // Optional hold when a color is complete, before the next crossFade
int repeat = 0; // How many times should we loop before stopping? (0 for no stop)
int j = 0; // Loop counter for repeat
// Initialize color variables
int prevR = redVal;
int prevG = grnVal;
int prevB = bluVal;
////////////////////////////////////
int bpin1 = (3);
int bpin2 = (4);
int bpin3 = (5);
int bpin4 = (6);
int lpin = (13);
int mode = (0);
DMX_Master dmx_master (512,2); //DMX Initialize
/////////////////////////////////////
void setup(){
pinMode (bpin1, INPUT);
pinMode (bpin2, INPUT);
pinMode (lpin, OUTPUT);
digitalWrite (lpin, LOW);
pinMode (2,OUTPUT); //This is for the DMX Shield
dmx_master.enable ();
}
void loop(){
buttonCheck();
while (mode == 1){
crossFade(red);
if (mode != 1){ //jump out
break; }
crossFade(orange);
if (mode != 1){ //jump out
break; }
crossFade(yellow);
if (mode != 1){ //jump out
break; }
crossFade(green);
if (mode != 1){ //jump out
break; }
crossFade(teal);
if (mode != 1){ //jump out
break; }
crossFade(blue);
if (mode != 1){ //jump out
break; }
crossFade(purple);
if (mode != 1){ //jump out
break; }
crossFade(pink);
if (mode != 1){ //jump out
break; }
if (repeat) { // Do we loop a finite number of times?
j += 1;
if (j >= repeat) { // Are we there yet?
exit(j); // If so, stop.
}
}
digitalWrite (13,LOW);
}
while (mode == 2){
crossFade(black);
digitalWrite (13,HIGH);
buttonCheck();
if (mode != 2){
redVal = black[0];
grnVal = black[1];
bluVal = black[2];
break; }
}
while (mode == 3){
dmx_master.setChannelValue (1,255);
dmx_master.setChannelValue (2,255);
dmx_master.setChannelValue (3,255);
digitalWrite (13,HIGH);
buttonCheck();
if (mode != 3){
break; }
}
while (mode == 4){
crossFade(blue);
digitalWrite (13,HIGH);
buttonCheck();
if (mode != 4){
break; }
}
}
///////////////
void buttonCheck(){
if (digitalRead(bpin1) == HIGH){
mode = (1);
}
if (digitalRead(bpin2) == HIGH){
mode = (2);
}
if (digitalRead(bpin3) == HIGH){
mode = (3);
}
if (digitalRead(bpin4) == HIGH){
mode = (4);
}
}
//LED FADE PROGRAM ...................................................................................
//...
/*
* Code for cross-fading 3 LEDs, red, green and blue (RGB)
* To create fades, you need to do two things:
* 1. Describe the colors you want to be displayed
* 2. List the order you want them to fade in
*
* DESCRIBING A COLOR:
* A color is just an array of three percentages, 0-100,
* controlling the red, green and blue LEDs
*
* Red is the red LED at full, blue and green off
* int red = { 100, 0, 0 }
* Dim white is all three LEDs at 30%
* int dimWhite = {30, 30, 30}
* etc.
*
* Some common colors are provided below, or make your own
*
* LISTING THE ORDER:
* In the main part of the program, you need to list the order
* you want to colors to appear in, e.g.
* crossFade(red);
* crossFade(green);
* crossFade(blue);
*
* Those colors will appear in that order, fading out of
* one color and into the next
*
* In addition, there are 5 optional settings you can adjust:
* 1. The initial color is set to black (so the first color fades in), but
* you can set the initial color to be any other color
* 2. The internal loop runs for 1020 interations; the 'wait' variable
* sets the approximate duration of a single crossfade. In theory,
* a 'wait' of 10 ms should make a crossFade of ~10 seconds. In
* practice, the other functions the code is performing slow this
* down to ~11 seconds on my board. YMMV.
* 3. If 'repeat' is set to 0, the program will loop indefinitely.
* if it is set to a number, it will loop that number of times,
* then stop on the last color in the sequence. (Set 'return' to 1,
* and make the last color black if you want it to fade out at the end.)
* 4. There is an optional 'hold' variable, which pasues the
* program for 'hold' milliseconds when a color is complete,
* but before the next color starts.
* 5. Set the DEBUG flag to 1 if you want debugging output to be
* sent to the serial monitor.
*
* The internals of the program aren't complicated, but they
* are a little fussy -- the inner workings are explained
* below the main loop.
*
* April 2007, Clay Shirky <clay.shirky@nyu.edu>
*/
int calculateStep(int prevValue, int endValue) {
int step = endValue - prevValue; // What's the overall gap?
if (step) { // If its non-zero,
step = 1020/step; // divide by 1020
}
return step;
}
/* The next function is calculateVal. When the loop value, i,
* reaches the step size appropriate for one of the
* colors, it increases or decreases the value of that color by 1.
* (R, G, and B are each calculated separately.)
*/
int calculateVal(int step, int val, int i)
{
if ((step) && i % step == 0) { // If step is non-zero and its time to change a value,
if (step > 0) { // increment the value if step is positive...
val += 1;
}
else if (step < 0) { // ...or decrement it if step is negative
val -= 1;
}
}
// Defensive driving: make sure val stays in the range 0-255
if (val > 255)
{
val = 255;
}
else if (val < 0) {
val = 0;
}
return val;
}
/* crossFade() converts the percentage colors to a
* 0-255 range, then loops 1020 times, checking to see if
* the value needs to be updated each time, then writing
* the color values to the correct pins.
*/
void crossFade(int color[3])
{
// Convert to 0-255
int R = (color[0] * 255) / 100;
int G = (color[1] * 255) / 100;
int B = (color[2] * 255) / 100;
buttonCheck();
int stepR = calculateStep(prevR, R);
int stepG = calculateStep(prevG, G);
int stepB = calculateStep(prevB, B);
for (int i = 0; i <= 1020; i++) {
redVal = calculateVal(stepR, redVal, i);
grnVal = calculateVal(stepG, grnVal, i);
bluVal = calculateVal(stepB, bluVal, i);
buttonCheck();
dmx_master.setChannelValue (1, redVal); // Write current values to LED pins (DMX Channels)
dmx_master.setChannelValue (2, grnVal);
dmx_master.setChannelValue (3, bluVal);
buttonCheck();
delay(wait); // Pause for 'wait' milliseconds before resuming the loop
}
// Update current values for next loop
prevR = redVal;
prevG = grnVal;
prevB = bluVal;
delay(hold); // Pause for optional 'wait' milliseconds before resuming the loop
}