Go Down

Topic: Need help cross-fading the new flora RGB lights (Read 798 times) previous topic - next topic

agent99s

I got the new flora (wearable technology similar to the lilypad) by adafruit and I am having trouble writing the code to cross-fade colors.
I'm new to electronics and don't really know how to write code. I've started with the basic "test" code (the floratest). I am looking to have blue and white slowly fade into each other. I was also wondering how to program each light separate. Example, while one is fading from blue to white, another is fading the opposite (white to blue). I was able to get it to blink blue and white from the test code:

#include "Adafruit_FloraPixel.h"

/*****************************************************************************
Example sketch for driving Adafruit Flora pixels
Connect the pixel(s) to Digital 6 (low right hand)
*****************************************************************************/

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_FloraPixel strip = Adafruit_FloraPixel(8);

void setup() {
   
  strip.begin();

  // Update the strip, to start they are all 'off'
  strip.show();
}


void loop() {
  // Some example procedures showing how to display to the pixels

  colorWipe(Color(0, 0, 255), 50);
  colorWipe(Color(255, 255, 255), 50);
  delay(1000);

// rainbowCycle(1);

}

/*void rainbow(uint8_t wait) {
  int i, j;
   
  for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 255));
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
 
  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}
*/
// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(RGBPixel c, uint8_t wait) {
  int i;
 
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
RGBPixel Color(byte r, byte g, byte b)
{
  RGBPixel p;
 
  p.red = r;
  p.green = g;
  p.blue = b;
 
  return p;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
RGBPixel Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

can someone please help?


Grumpy_Mike

Quote
I'm new to electronics and don't really know how to write code.

You don't know how to post it either. See the how to use this forum sticky post at the start of this section.

agent99s

Code: [Select]
#include "Adafruit_FloraPixel.h"

/*****************************************************************************
Example sketch for driving Adafruit Flora pixels
Connect the pixel(s) to Digital 6 (low right hand)
*****************************************************************************/

// Set the first variable to the NUMBER of pixels. 25 = 25 pixels in a row
Adafruit_FloraPixel strip = Adafruit_FloraPixel(8);

void setup() {
   
  strip.begin();

  // Update the strip, to start they are all 'off'
  strip.show();
}


void loop() {
  // Some example procedures showing how to display to the pixels

  colorWipe(Color(0, 0, 255), 50);
  colorWipe(Color(255, 255, 255), 50);
  delay(1000);

// rainbowCycle(1);

}

/*void rainbow(uint8_t wait) {
  int i, j;
   
  for (j=0; j < 256; j++) {     // 3 cycles of all 256 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 255));
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}

// Slightly different, this one makes the rainbow wheel equally distributed
// along the chain
void rainbowCycle(uint8_t wait) {
  int i, j;
 
  for (j=0; j < 256 * 5; j++) {     // 5 cycles of all 25 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 96-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 96 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 256 / strip.numPixels()) + j) % 256) );
    } 
    strip.show();   // write all the pixels out
    delay(wait);
  }
}
*/
// fill the dots one after the other with said color
// good for testing purposes
void colorWipe(RGBPixel c, uint8_t wait) {
  int i;
 
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

/* Helper functions */

// Create a 24 bit color value from R,G,B
RGBPixel Color(byte r, byte g, byte b)
{
  RGBPixel p;
 
  p.red = r;
  p.green = g;
  p.blue = b;
 
  return p;
}

//Input a value 0 to 255 to get a color value.
//The colours are a transition r - g -b - back to r
RGBPixel Wheel(byte WheelPos)
{
  if (WheelPos < 85) {
   return Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  } else if (WheelPos < 170) {
   WheelPos -= 85;
   return Color(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
   WheelPos -= 170;
   return Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

Grumpy_Mike

Thanks for posting correctly although you could have modified the original post.

Fading is just changing the RGB values from one set to another. Look at some pages on colour theory. Treat RGB numbers like coordnates in three dimensional space.

But most importantly of all learn how to read the code and what those statements actually do. That code is example code that shows you how to drive the hardware, can you seprate out what functions do what? I am sure you are not expecting other people to write your code for you, so look at the tutorials and especially the blink without delay technique, you will need this to do two or more things seemingly independently.

If you have any specific questions then please ask.

Go Up