Pages: [1]   Go Down
Author Topic: MAP FUNCTION AND LPD8806  (Read 1363 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi, im trying to use the MAP function to gradually fade in-between colors. When I use this code it does the first fade, but then JUMPS back and does it over, without executing the second fade.

Any advice very appreciated, THANKS




#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; j++) {
    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);
  }
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  for (k=0; k < 140; j++) {

This will never increment 'k' so the loop will never end.  It will repeat with k=0 forever.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks!

is it the extra space in the code, k not being defined properly?

The code for j does seem to give me a gradual fade. Why not k?

really appreciate the guidance
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry, misplaced a j and k. still it skips when I run it
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

sorry, misplaced a j and k. still it skips when I run it
If you fixed that problem (changed "j++" to "k++") I see no reason why it wouldn't work.  Perhaps you should show your current code.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the patience and insight John.

When I run the current code here is what happens:

Red fade to Blue  -abrupt jump to red fade to blue fade to red -abrupt jump to blue fade to red fade to blue -abupt jump to red fade to blue fade to red -abrupt jump to blue, it just keeps repeating.

I want a smooth fade, back and forth, like a sine wave. Thanks for any insight.

Here is my code:

#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;

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

LPD8806 strip = LPD8806(4, 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;
 
  for (j=0; j < 100; j++) {
    r = map(j, 0, 100, COLOR1_R, COLOR2_R);
    g = map(j, 0, 100, COLOR1_G, COLOR2_G);
    b = map(j, 0, 100, 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(120);
  }

  for (k=0; k < 100; k++) {
    r = map(k, 0, 100, COLOR2_R, COLOR1_R);
    g = map(k, 0, 100, COLOR2_G, COLOR1_G);
    b = map(k, 0, 100, COLOR2_B, COLOR1_B);
   
    for (i=0; i < 4; i++) { 
      strip.setPixelColor(i,(strip.Color(r,g,b) ));
    } 
   strip.show();
    delay(120);
  }
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your code looks fine so I can only assume that the fault is in the library.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How might one discern this? reload the library? What other code options do I have for achieving this fade effect? smiley-lol
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14028
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Color cycling is easier in another color space, see - http://arduino.cc/forum/index.php/topic,95019.msg719982.html#msg719982 - for code
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey Robtillart,

Seems that code is for a RGB LED, but Im using the LPD8806 strips.

Any ideas?
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 227
Posts: 14028
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

use

Code:
strip.setPixelColor(i,(strip.Color(r,g,b) ));

where in the other code sets a single RGB led.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: