Crossfader Arduino Tutorial


hello i'm super newb please don't bash me much. i'm using led's , i've connected them correctly (finally) , wondering why program here ( http://www.arduino.cc/en/tutorial/colorcrossfader ) doesn't work? say's haven't declared crossfade. read directions on top , said crossfade needs used list order of colors. isn't declared in main part (void loop() )? or need declared before? nevermind void on bottom , needed before voidloop. lol
anyways playing rgb led's wondering if has specific way of making them fade in , out different colors? (brightness , color mixing)
code: [select]

/*
* code cross-fading 3 leds, red, green , blue (rgb)
* create fades, need 2 things:
*  1. describe colors want displayed
*  2. list order want them fade in
*
* describing color:
* color array of 3 percentages, 0-100,
*  controlling red, green , blue leds
*
* red red led @ full, blue , green off
*   int red = { 100, 0, 0 }
* dim white 3 leds @ 30%
*   int dimwhite = {30, 30, 30}
* etc.
*
* common colors provided below, or make own
*
* listing order:
* in main part of program, need list order
*  you want colors appear in, e.g.
*  crossfade(red);
*  crossfade(green);
*  crossfade(blue);
*
* colors appear in order, fading out of
*    one color , next  
*
* in addition, there 5 optional settings can adjust:
* 1. initial color set black (so first color fades in),
*    you can set initial color other color
* 2. internal loop runs 1020 interations; 'wait' variable
*    sets approximate duration of single crossfade. in theory,
*    a 'wait' of 10 ms should make crossfade of ~10 seconds. in
*    practice, other functions code performing slow
*    down ~11 seconds on board. ymmv.
* 3. if 'repeat' set 0, program loop indefinitely.
*    if set number, loop number of times,
*    then stop on last color in sequence. (set 'return' 1,
*    and make last color black if want fade out @ end.)
* 4. there optional 'hold' variable, pasues
*    program 'hold' milliseconds when color complete,
*    but before next color starts.
* 5. set debug flag 1 if want debugging output be
*    sent serial monitor.
*
*    the internals of program aren't complicated, they
*    are little fussy -- inner workings explained
*    below main loop.
*
* april 2007, clay shirky <clay.shirky@nyu.edu>
*/

// output
int redpin = 9;   // red led,   connected digital pin 9
int grnpin = 10;  // green led, connected digital pin 10
int blupin = 11;  // blue led,  connected digital pin 11

// 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] = { 40, 95, 0 };
int dimwhite[3] = { 30, 30, 30 };
// etc.

// set initial color
int redval = black[0];
int grnval = black[1];
int bluval = black[2];

int wait = 10;      // 10ms internal crossfade delay; increase slower fades
int hold = 0;       // optional hold when color complete, before next crossfade
int debug = 1;      // debug counter; if set 1, write values via serial
int loopcount = 60; // how should debug report?
int repeat = 3;     // how many times should loop before stopping? (0 no stop)
int j = 0;          // loop counter repeat

// initialize color variables
int prevr = redval;
int prevg = grnval;
int prevb = bluval;

// set led outputs
void setup()
{
 pinmode(redpin, output);   // sets pins output
 pinmode(grnpin, output);  
 pinmode(blupin, output);

 if (debug) {           // if want see values debugging...
   serial.begin(9600);  // ...set serial ouput
 }
}

// main program: list order of crossfades
void loop()
{
 crossfade(red);
 crossfade(green);
 crossfade(blue);
 crossfade(yellow);

 if (repeat) { // loop finite number of times?
   j += 1;
   if (j >= repeat) { // there yet?
     exit(j);         // if so, stop.
   }
 }
}

/* below line math -- shouldn't need change basics
*
* program works this:
* imagine crossfade moves red led 0-10,
*   green 0-5, , blue 10 7, in
*   ten steps.
*   we'd want count 10 steps , increase or
*   decrease color values in evenly stepped increments.
*   imagine + indicates raising value 1, , -
*   equals lowering it. our 10 step fade like:
*
*   1 2 3 4 5 6 7 8 9 10
* r + + + + + + + + + +
* g   +   +   +   +   +
* b     -     -     -
*
* red rises 0 10 in ten steps, green
* 0-5 in 5 steps, , blue falls 10 7 in 3 steps.
*
* in real program, color percentages converted
* 0-255 values, , there 1020 steps (255*4).
*
* figure out how big step there should between 1 up- or
* down-tick of 1 of led values, call calculatestep(),
* calculates absolute gap between start , end values,
* , divides gap 1020 determine size of step  
* between adjustments in value.
*/

int calculatestep(int prevvalue, int endvalue) {
 int step = endvalue - prevvalue; // what's overall gap?
 if (step) {                      // if non-zero,
   step = 1020/step;              //   divide 1020
 }
 return step;
}

/* next function calculateval. when loop value, i,
*  reaches step size appropriate 1 of the
*  colors, increases or decreases value of color 1.
*  (r, g, , b each calculated separately.)
*/

int calculateval(int step, int val, int i) {

 if ((step) && % step == 0) { // if step non-zero , time change value,
   if (step > 0) {              //   increment value if step positive...
     val += 1;          
   }
   else if (step < 0) {         //   ...or decrement if step negative
     val -= 1;
   }
 }
 // defensive driving: make sure val stays in range 0-255
 if (val > 255) {
   val = 255;
 }
 else if (val < 0) {
   val = 0;
 }
 return val;
}

/* crossfade() converts percentage colors
*  0-255 range, loops 1020 times, checking see if  
*  the value needs updated each time, writing
*  the color values correct pins.
*/

void crossfade(int color[3]) {
 // convert 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 = 0; <= 1020; i++) {
   redval = calculateval(stepr, redval, i);
   grnval = calculateval(stepg, grnval, i);
   bluval = calculateval(stepb, bluval, i);

   analogwrite(redpin, redval);   // write current values led pins
   analogwrite(grnpin, grnval);      
   analogwrite(blupin, bluval);

   delay(wait); // pause 'wait' milliseconds before resuming loop

   if (debug) { // if want serial output, print @
     if (i == 0 or % loopcount == 0) { // beginning, , every loopcount times
       serial.print("loop/rgb: #");
       serial.print(i);
       serial.print(" | ");
       serial.print(redval);
       serial.print(" / ");
       serial.print(grnval);
       serial.print(" / ");  
       serial.println(bluval);
     }
     debug += 1;
   }
 }
 // update current values next loop
 prevr = redval;
 prevg = grnval;
 prevb = bluval;
 delay(hold); // pause optional 'wait' milliseconds before resuming loop
}
(code tags added moderaror)

moderator tell edit post , put code in special window (use # tool in toolbar).

the declaration crossfade function defined, after loop() function ended. can put exact error message get?


Arduino Forum > Using Arduino > LEDs and Multiplexing > Crossfader Arduino Tutorial


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping