I'm working on a sort of DMX Lighting Controller, with several different outputs. One of which is a RGB fading program. I've got the fading part figured out, thanks to some code I found on here. However, I want to be able to turn it on and off. I discovered this other framework for mode switching. The problem I'm having, is that while I can get the fade to start with the mode (1) button press, after pressing the button for a mode (2) that turns the fade off, the fade doesn't stop until the fading sequence/function is complete. How can I interrupt the crossfade at any point, and switch modes? I'd also like to be able to process other button inputs that control things unrelated to the fading while the crossfade is running, if possible. Thanks for all your help!
(Sorry, had to cut some in-code documentation to fit the limit, I've attached the full file. )
#include <Conceptinetics.h>
boolean button[]= {3,4,5,6,7}; //pin assignment for buttons
boolean buttonstate = 0;
int mode = 0;
// 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};
//int dimWhite[3] = { 30, 30, 30 };
// etc.
// 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 = 200; // Optional hold when a color is complete, before the next crossFade
int DEBUG = 1; // DEBUG counter; if set to 1, will write values back via serial
int loopCount = 60; // How often should DEBUG report?
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;
DMX_Master dmx_master (512,2); //DMX Initialize
void setup()
{
for(int x=0; x<5; x++) //starts a loop to set the pin modes for all button pins
{
pinMode(button[x], INPUT);
}
for(int x=0; x<5; x++) //starts a loop to set the buttons to a default of HIGH
{
pinMode(button[x], LOW);
}
pinMode (2,OUTPUT); //This is for the DMX Shield
dmx_master.enable ();
}
void loop(){
buttoncheck(); //calls to the void buttoncheck() section for checking the buttons
if(mode == 1)
{
mode1(); //directs teh program to teh section with the flashes you selected
}
if(mode == 2)
{
mode2(); //directs teh program to teh section with the flashes you selected
}
if(mode == 3)
{
mode3(); //directs teh program to teh section with the flashes you selected
}
if(mode == 4)
{
mode4(); //directs teh program to teh section with the flashes you selected
}
if(mode == 5)
{
mode5(); //directs teh program to teh section with the flashes you selected
}
}
void buttoncheck()
{
for(int x=0; x<5; x++) //loop for checking all the buttons
{
buttonstate = digitalRead(button[x]);
if(buttonstate == HIGH && button[x] == 3) //number equals the buttoon the pin is connected too
{
mode = 1; // number equals the mode you want to run
}
if(buttonstate == HIGH && button[x] == 4)
{
mode = 2;
}
if(buttonstate == HIGH && button[x] == 5)
{
mode = 3;
}
if(buttonstate == HIGH && button[x] == 6)
{
mode = 4;
}
if(buttonstate == HIGH && button[x] == 7)
{
mode = 5;
}
}
}
void mode1() //RGB FADE
{
buttoncheck();
crossFade(red);
buttoncheck();
crossFade(orange);
buttoncheck();
crossFade(yellow);
buttoncheck();
crossFade(green);
buttoncheck();
crossFade(teal);
buttoncheck();
crossFade(blue);
buttoncheck();
crossFade(purple);
buttoncheck();
crossFade(pink);
buttoncheck();
if (repeat) { // Do we loop a finite number of times?
j += 1;
if (j >= repeat) { // Are we there yet?
exit(j); // If so, stop.
}
}
}
void mode2() //BLACKOUT
{
buttoncheck();
dmx_master.setChannelValue (1,0);
dmx_master.setChannelValue (2,0);
dmx_master.setChannelValue (3,0);
buttoncheck();
}
void mode3() //ALL@FULL
{
buttoncheck();
dmx_master.setChannelValue (1,255);
dmx_master.setChannelValue (2,255);
dmx_master.setChannelValue (3,255);
buttoncheck();
}
void mode4()
{
}
void mode5()
{
}
//LED FADE PROGRAM ...................................................................................
//...
//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;
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);
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
}
console_modes.ino (9.98 KB)