MAP FUNCTION AND LPD8806

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

MAP FUNCTION AND LPD8806

Post by slurry bowl »

I am attempting to gradually fade colors, without any abrupt changes or edges.

I am currently trying to fade from blue to red and then red back to blue, etc.........

When I upload the code, it does the first fade then jumps back to blue !! :(

Ive tried changing the int and various timing differences. Here is the sample code, any help appreciated ! :D :D

Code: Select all

#include "LPD8806.h"
#include "SPI.h"

// Example to control LPD8806-based RGB LED Modules in a strip

/*****************************************************************************/

// Number of RGB LEDs in strand:


int nLEDs = 4;

// Chose 2 pins for output; can be any valid output pins:
int dataPin  = 11;
int clockPin = 13;

// Magenta:
#define COLOR1_R 0
#define COLOR1_G 0
#define COLOR1_B 255
// Blue:
#define COLOR2_R 255
#define COLOR2_G 0
#define COLOR2_B 0

// First parameter is the number of LEDs in the strand.  The LED strips
// are 32 LEDs per meter but you can extend or cut the strip.  Next two
// parameters are SPI data and clock pins:
LPD8806 strip = LPD8806(4, dataPin, clockPin);

// You can optionally use hardware SPI for faster writes, just leave out
// the data and clock pin parameters.  But this does limit use to very
// specific pins on the Arduino.  For "classic" Arduinos (Uno, Duemilanove,
// etc.), data = pin 11, clock = pin 13.  For Arduino Mega, data = pin 51,
// clock = pin 52.  For 32u4 Breakout Board+ and Teensy, data = pin B2,
// clock = pin B1.  For Leonardo, this can ONLY be done on the ICSP pins.
//LPD8806 strip = LPD8806(nLEDs);

void setup() {
  // Start up the LED strip
  strip.begin();

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

void loop() {
  int r,g,b;
  uint16_t i, j, k;
  
  for (j=0; j < 140; j++) {
    r = map(j, 0, 140, COLOR1_R, COLOR2_R);
    g = map(j, 0, 140, COLOR1_G, COLOR2_G);
    b = map(j, 0, 140, COLOR1_B, COLOR2_B);
    
    for (i=0; i < 4; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }  
    
    strip.show();   // write all the pixels out
    delay(50);
  }

  for (k=0; k < 140; k++) {
    r = map(k, 0, 140, COLOR2_R, COLOR1_R);
    g = map(k, 0, 140, COLOR2_G, COLOR1_G);
    b = map(k, 0, 140, COLOR2_B, COLOR1_B);
    
    for (i=0; i < 4; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }  
    
   strip.show();   // write all the pixels out
    delay(100);
  }
}
Last edited by adafruit_support_bill on Mon Jan 28, 2013 6:03 pm, edited 1 time in total.
Reason: Use 'code' button for submitting code

User avatar
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

Re: MAP FUNCTION AND LPD8806

Post by slurry bowl »

Ive tried everything and still this glitches, maybe there is an error in the library?

User avatar
pburgess
 
Posts: 4161
Joined: Sun Oct 26, 2008 2:29 am

Re: MAP FUNCTION AND LPD8806

Post by pburgess »

It's possible the map() function doesn't play well with an inverted scale like that, I'm not sure.

Try making the 'k' loop count down, with COLOR1 and COLOR2 in order, see if that helps. You'll need to make k a signed int in that case. Something like this:

Code: Select all

  int k;

  for (k=140; k >= 0; k--) {
    r = map(k, 0, 140, COLOR1_R, COLOR2_R);
    g = map(k, 0, 140, COLOR1_G, COLOR2_G);
    b = map(k, 0, 140, COLOR1_B, COLOR2_B);
    
    for (i=0; i < 4; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }

User avatar
mohawkpiper
 
Posts: 138
Joined: Sat Oct 13, 2012 3:28 am

Re: MAP FUNCTION AND LPD8806

Post by mohawkpiper »

the same thing goes for this thread as your other thread slurry bowl...

the map function is fine the way it is (as far as i can tell) and it plays fine with an inverted scale.

the problem you are having here is that the led strip again only goes to 127 not 255.

try taking your last code posted and changing only two values, the COLOR1_B ad COLOR2_R to 127 instead of 255 and you will find your led working the way you want it to.

again here, while it loops, the remap of 0-140 to 0-255... it works from 0-127 (in the remapped values) but once its past 128(again, remapped value) it starts all over. so its doing each for loop twice essentially.

User avatar
slurry bowl
 
Posts: 156
Joined: Sun Nov 11, 2012 6:37 pm

Re: MAP FUNCTION AND LPD8806

Post by slurry bowl »

Thanks Mohawk and pburgess.

The code works great and Im excited !!!!!!!

8)

I have 80 leds in my strip.

I want to control the fade of leds 1-28 with a certain fade and 28-80 with another.
I updated the code and want to take the next step. I recognize I could probably only use 2 ints. (j,k), but where do I change the led # to address?

Code: Select all

#include "LPD8806.h"
#include "SPI.h"

// Example to control LPD8806-based RGB LED Modules in a strip

/*****************************************************************************/

// Number of RGB LEDs in strand:


int nLEDs = 80;

// Chose 2 pins for output; can be any valid output pins:
int dataPin  = 11;
int clockPin = 13;

// Magenta:
#define COLOR1_R 40
#define COLOR1_G 0
#define COLOR1_B 120
// Blue:
#define COLOR2_R 120
#define COLOR2_G 0
#define COLOR2_B 40


LPD8806 strip = LPD8806(80, dataPin, clockPin);


void setup() {
  // Start up the LED strip
  strip.begin();

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

void loop() {
  int r,g,b;
  uint16_t i, j, k, l, m;
  
  for (j=0; j < 140; j++) {
    r = map(j, 0, 140, COLOR1_R, COLOR2_R);
    g = map(j, 0, 140, COLOR1_G, COLOR2_G);
    b = map(j, 0, 140, COLOR1_B, COLOR2_B);
    
    for (i=0; i < 28; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }  
    
    strip.show();   // write all the pixels out
    delay(10);
  }

  for (k=0; k < 140; k++) {
    r = map(k, 0, 140, COLOR2_R, COLOR1_R);
    g = map(k, 0, 140, COLOR2_G, COLOR1_G);
    b = map(k, 0, 140, COLOR2_B, COLOR1_B);
    
    for (i=0; i < 28; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }  
    
   strip.show();   // write all the pixels out
    delay(10);
    
  }
 
for (l=0; l < 140; j++) {
    r = map(l, 0, 140, COLOR2_R, COLOR1_R);
    g = map(l, 0, 140, COLOR2_G, COLOR1_G);
    b = map(l, 0, 140, COLOR2_B, COLOR1_B);
    
    for (i=29; i < 80; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }  
    
    strip.show();   // write all the pixels out
    delay(10);
    
    for (m=0; m < 140; j++) {
    r = map(m, 0, 140, COLOR1_R, COLOR2_R);
    g = map(m, 0, 140, COLOR1_G, COLOR2_G);
    b = map(m, 0, 140, COLOR1_B, COLOR2_B);
    
    for (i=29; i < 80; i++) {  
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    }  
    
    strip.show();   // write all the pixels out
    delay(10);
}
}
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”