Triggering From Serial Input

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
ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Triggering From Serial Input

Post by ckeyes »

I seem to be able to get where I want to be with controlling my 160 LED LPD8806 strip
in individual sketches but I was hoping I could get a number of different effects in one
sketch that I could trigger each one using serial inputs.

I've seen some examples where button presses etc. will trigger various sections of a sketch
but I don't seem to be able to get anything working other than the simplest of effects.

I have this script working but I'd love to get a smooth flowing rainbow effect added to it:

Code: Select all

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

int dataPin  = 2;
int clockPin = 3;
const int numPixels = 160;

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

void setup() {                
  strip.begin();

  strip.show();
  Serial.begin(9600);  
}

// function prototypes, do not remove these!
void colorChase(uint32_t c, uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void dither(uint32_t c, uint8_t wait);
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait);
void wave(uint32_t c, int cycles, uint8_t wait);
void rainbowCycle(uint8_t wait);
uint32_t Wheel(uint16_t WheelPos);

void loop() {
  // read the sensor:
  if (Serial.available() > 0) {  
    char inByte = Serial.read();
    Serial.print("Command Received ");
    Serial.print(inByte);
    Serial.print("\n\r");
    int i;
          uint32_t c;
    
    switch (inByte) {
      
    case 'a':
		while (!Serial.available()) {
// Set all pixels to red
          c = strip.Color(255,0,0);
          for (i=0; i < strip.numPixels(); i++) {
              strip.setPixelColor(i, c);
          }
          strip.show();
          delay(100);
		}
		break;
    case 'b':    
      while (!Serial.available()) {	
			// Wipe from middle:
          c = strip.Color(255,255,255); // white
          for (i=strip.numPixels() / 2 - 1; i >= 0; i--) { // half of strip, count down to 0
            strip.setPixelColor(i, c); // left pixel
            strip.setPixelColor(strip.numPixels() - 1 - i, c); // right pixel
            strip.show();
            delay(100);
		}
      break;
      
    case 'c':    
     while (!Serial.available()) {	
			digitalWrite(13, HIGH);   // set the LED on
			delay(150);              // wait for a second
			digitalWrite(13, LOW);    // set the LED off
			delay(150);              // wait for a second
		}
      break;
      
      case 'd':    
      while (!Serial.available()) {	
			digitalWrite(13, HIGH);   // set the LED on
			delay(1000);              // wait for a second
			digitalWrite(13, LOW);    // set the LED off
			delay(1000);              // wait for a second
		}
      break;
      
      case 'e':    
      while (!Serial.available()) {	

			digitalWrite(13, LOW);    // set the LED off
			delay(20);              // wait for a second
		}
      break;
    
    default:
      // turn all the LEDs off:
      for (int thisPin = 13; thisPin < 13; thisPin++) {
        digitalWrite(thisPin, LOW);
      }
    } 
  }
}
}

When I copy code from another script and paste it into the section with the break I can never
get it to compile. Any pointers on inserting effects into the break areas would be greatly appreciated!

Thanks,

Carl

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: Triggering From Serial Input

Post by adafruit_support_bill »

You should be able to insert another 'case' into the 'switch' statement as long as you stick to the proper syntax. http://arduino.cc/en/Reference/SwitchCase

If you post the code that you are having problems with, we can help you figure out what is wrong with it.

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Triggering From Serial Input

Post by ckeyes »

Thanks. This project requires a lot of due diligence for a newbie :-) I got it working pretty well and am finally getting the flow of it all I think. (pun)

One thing that does escape me though is how to get the strip to do things like the comet effect while the strip is a solid color etc., what would appear
to be two things at once. The effects I see in the samples don't seem to go over any of those types of effects.

Can those be done with an UNO and LPD8806? Love to see some sample code if they can!

Here's a link to some effects I'd love to be able to do: http://www.youtube.com/watch?v=degZYv11rNc

Many thanks,

Carl

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: Triggering From Serial Input

Post by adafruit_support_bill »

how to get the strip to do things like the comet effect while the strip is a solid color etc.
This is just like the "colorChase()" function in the StrandTest example - except you use a color other than black (0) for the 'background color'.

(I haven't tested this, but it should be close)

Code: Select all

void colorOverColorChase(uint32_t color, uint32_t backgroundColor, uint8_t wait) {
  int i;
  
  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);  // turn all pixels off
  } 
  
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, color);
      if (i == 0) { 
        strip.setPixelColor(strip.numPixels()-1, backgroundColor);
      } else {
        strip.setPixelColor(i-1, backgroundColor);
      }
      strip.show();
      delay(wait);
  }
}

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Triggering From Serial Input

Post by ckeyes »

Not sure what I'm doing wrong here but here's the code I have so far. Can't get it to compile.

Really appreciate your help with this!

Code: Select all

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

int dataPin  = 2;
int clockPin = 3;
const int numPixels = 160;

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

void setup() {                
  strip.begin();

  strip.show();
  Serial.begin(9600);  
}


void loop() {
  // read the sensor:
  if (Serial.available() > 0) {  
    char inByte = Serial.read();
    Serial.print("Command Received ");
    Serial.print(inByte);
    Serial.print("\n\r");
    int i;
          uint32_t c;
    
    switch (inByte) {
      
    case 'a':
		while (!Serial.available()) {
// Set all pixels to red
          c = strip.Color(255,0,0);
          for (i=0; i < strip.numPixels(); i++) {
              strip.setPixelColor(i, c);
          }
          strip.show();
          //delay(100);
		}
		break;
    case 'b':    
      while (!Serial.available()) {	
			//green to red wave
  wave(strip.Color(0,127,0),60,0,100); //color, cycles, delay, repetitions
  wave(strip.Color(20,107,0),50,0,50); //color, cycles, delay, repetitions
  wave(strip.Color(40,87,0),40,0,50); //color, cycles, delay, repetitions
  wave(strip.Color(80,47,0),30,0,50); //color, cycles, delay, repetitions
  wave(strip.Color(107,27,0),20,0,100); //color, cycles, delay, repetitions
  wave(strip.Color(127,0,0),5,0,200); //color, cycles, delay, repetitions
		}
      break;
      
    case 'c':    
     while (!Serial.available()) {	
	twoColorSparkle(strip.Color(127,0,0), strip.Color(0,127,0), 50, 10); //color 1, color 2, density, delay - random sparkles in two colors

		}
      break;
      
      case 'd':    
      while (!Serial.available()) {	
			 rainbowCycle(0);  // make it go through the cycle fairly fast
  
		}
      break;
      
      case 'e':    
      while (!Serial.available()) {	
              turnAllOff();
		}
      break;
      
      case 'f':    
      while (!Serial.available()) {	
  
  chaseMultipleLinesLToH(10,10,0,152); //number of leds in line, delay, low, 
		}
      break;
      
      case 'g':    
      while (!Serial.available()) {	
  turnAllOn(strip.Color(0,0,80),200); 
  dither(strip.Color(127,0,0), 10); //color, delay - random fills up the strip
  dither(strip.Color(0,0,80), 10); //color, delay - random fills up the strip
  //fadeUp(0,0,127,30); //red, green, blue, delay - fade up all pixels one color
		}
      break;
      
      case 'h':    
      while (!Serial.available()) {	
  randomSparkleUpSegment(strip.Color(0,127,0),15,10,0,100); //color, density, delay, low point, high point
  turnSegmentOn(strip.Color(0,127,0),200,0,100); //color, delay, start of segment, end of segment
  delay(500);
		}
      break;
      
      case 'i':    
      while (!Serial.available()) {	
  fillUpTheStar(strip.Color(127,0,0),30); //color, delay
  fillUpCenterStar(strip.Color(0,127,0), 30); //color, delay
  fadeDown(127,0,0,30);
		}
      break;
      
      case 'j':    
      while (!Serial.available()) {	
  colorOverColorChase(strip.Color(127,  0,  0), 100); // Red
  colorOverColorChase(strip.Color(  0,127,  0), 100); // Green
  colorOverColorChase(strip.Color(  0,  0,127), 100); // Blue
  colorOverColorChase(strip.Color(127,127,127), 100); // White
		}
      break;
    
      }
    } 
  }


// An "ordered dither" fills every pixel in a sequence that looks
// sparkly and almost random, but actually follows a specific order.
void dither(uint32_t c, uint8_t wait) {
  // Determine highest bit needed to represent pixel index
  int hiBit = 0;
  int n = strip.numPixels() - 1;
  for(int bit=1; bit < 0x8000; bit <<= 1) {
    if(n & bit) hiBit = bit;
  }
  int bit, reverse;
  for(int i=0; i<(hiBit << 1); i++) {
    // Reverse the bits in i to create ordered dither:
    reverse = 0;
    for(bit=1; bit <= hiBit; bit <<= 1) {
      reverse <<= 1;
      if(i & bit) reverse |= 1;
    }
    strip.setPixelColor(reverse, c);
    strip.show();
    delay(wait);
  }
  delay(250); // Hold image for 1/4 sec
}

// "Larson scanner" = Cylon/KITT bouncing light effect
void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
  int i, j, pos, dir;
  pos = 0;
  dir = 1;
  for(i=0; i<((strip.numPixels()-1) * 8); i++) {
    // Draw 5 pixels centered on pos. setPixelColor() will clip
    // any pixels off the ends of the strip, no worries there.
    // we'll make the colors dimmer at the edges for a nice pulse
    // look
    strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4));
    strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2));
    strip.setPixelColor(pos, strip.Color(r, g, b));
    strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2));
    strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4));

    strip.show();
    delay(wait);
    // If we wanted to be sneaky we could erase just the tail end
    // pixel, but it's much easier just to erase the whole thing
    // and draw a new one next time.
    for(j=-2; j<= 2; j++)
        strip.setPixelColor(pos+j, strip.Color(0,0,0));
    // Bounce off ends of strip
    pos += dir;
    if(pos < 0) {
      pos = 1;
      dir = -dir;
    } else if(pos >= strip.numPixels()) {
      pos = strip.numPixels() - 2;
      dir = -dir;
    }
  }
}

// Sine wave effect
#define PI 3.14159265
void wave(uint32_t c, int cycles, uint8_t wait, int repetitions) {
  float y;
  byte r, g, b, r2, g2, b2;
  // Need to decompose color into its r, g, b elements
  g = (c >> 16) & 0x7f;
  r = (c >> 8) & 0x7f;
  b = c & 0x7f;
  for(int x=0; x<repetitions; x++)
  {
    for(int i=0; i<strip.numPixels(); i++) {
      y = sin(PI * (float)cycles * (float)(x + i) / (float)strip.numPixels());
        y = 1.0 - y; // Translate Y to 0.0 (top) to 1.0 (center)
        r2 = (byte)((float)r * y);
        g2 = (byte)((float)g * y);
        b2 = (byte)((float)b * y);
      
      strip.setPixelColor(i, r2, g2, b2);
    }
    strip.show();
    delay(wait);
  }
}

void chaseUpTheStar(uint32_t c, uint8_t wait) {
  int i,j;
      strip.setPixelColor(80, c);
      delay(wait);
      strip.setPixelColor(80, strip.Color(0,0,0));
         for (i = 1; i <= 20; i++) {
           strip.setPixelColor(80+i, c);
           strip.setPixelColor(80-i, c);
           strip.show();   // write all the pixels out
           delay(wait);
           strip.setPixelColor(80+i, strip.Color(0,0,0));
           strip.setPixelColor(80-i, strip.Color(0,0,0));
           strip.show();   // write all the pixels out
         }
         for (j=0; j<=29; j++) {
           strip.setPixelColor(j, c);
           strip.setPixelColor(59-j, c);
           strip.show();   // write all the pixels out
           delay(wait);
           strip.setPixelColor(j, strip.Color(0,0,0));
           strip.setPixelColor(59-j, strip.Color(0,0,0));
           strip.show();   // write all the pixels out
         }  
}

void chaseUpCenterStar(uint32_t c, uint8_t wait) {
  int i,j;
         for (i = 1; i <= 6; i++) {
           strip.setPixelColor(146+i, c);
           strip.setPixelColor(147-i, c);
           strip.show();   // write all the pixels out
           delay(wait);
           strip.setPixelColor(146+i, strip.Color(0,0,0));
           strip.setPixelColor(147-i, strip.Color(0,0,0));
           strip.show();   // write all the pixels out
         }
         for (j=0; j<=18; j++) {
           strip.setPixelColor(103+j, c);
           strip.setPixelColor(140-j, c);
           strip.show();   // write all the pixels out
           delay(wait);
           strip.setPixelColor(103+j, strip.Color(0,0,0));
           strip.setPixelColor(140-j, strip.Color(0,0,0));
           strip.show();   // write all the pixels out
         }  
}
void fillUpTheStar(uint32_t c, uint8_t wait) {
  int i,j;
      strip.setPixelColor(80, c);
      delay(wait);
         for (i = 1; i <= 20; i++) {
           strip.setPixelColor(80+i, c);
           strip.setPixelColor(80-i, c);
           strip.show();   // write all the pixels out
           delay(wait);
           }
         for (j=0; j<=29; j++) {
           strip.setPixelColor(j, c);
           strip.setPixelColor(59-j, c);
           strip.show();   // write all the pixels out
           delay(wait);
           }  
}

void fillUpCenterStar(uint32_t c, uint8_t wait) {
  int i,j;
         for (i = 1; i <= 6; i++) {
           strip.setPixelColor(146+i, c);
           strip.setPixelColor(147-i, c);
           strip.show();   // write all the pixels out
           delay(wait);
           }
         for (j=0; j<=18; j++) {
           strip.setPixelColor(103+j, c);
           strip.setPixelColor(140-j, c);
           strip.show();   // write all the pixels out
           delay(wait);
           }  
}

void chaseBetweenLToH(uint32_t c, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i;
         for (i = pointL; i <= pointH; i++) {
           strip.setPixelColor(i, c);
           strip.show();   // write all the pixels out
           delay(wait);
           strip.setPixelColor(i, strip.Color(0,0,0));
           strip.show();   // write all the pixels out
           }  
}

void chaseBetweenHToL(uint32_t c, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i;
         for (i = pointH; i >= pointL; i--) {
           strip.setPixelColor(i, c);
           strip.show();   // write all the pixels out
           delay(wait);
           strip.setPixelColor(i, strip.Color(0,0,0));
           strip.show();   // write all the pixels out
           }  
}

void chaseLineLToH(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i, j;
         for(i = pointL; i < pointH+lineNumber; i++) {
           for(j = 0; j < lineNumber; j++) {
             strip.setPixelColor(i-j, c);
             }
         strip.show();   // write all the pixels out
         delay(wait);
         turnAllOff();
         }  
}

void chaseLineHToL(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i, j;
         for(i = pointH; i > pointL; i--) {
           for(j = 0; j<lineNumber; j++){
             strip.setPixelColor(i+j, c);
           }
          strip.show();   // write all the pixels out
          delay(wait);
          turnAllOff();
          }  
}

void chaseToCenter(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i, j, k;
  i = pointL;
  k = pointH;
  while(i < k && k > i) {
           for(j = 0; j < lineNumber; j++) {
             strip.setPixelColor(i-j, c);
             strip.setPixelColor(k+j, c);
           }
  strip.show();   // write all the pixels out
  delay(wait);
  i++;
  k--;
  turnAllOff();
  }
}

void chasePastCenter(uint32_t c, uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i, j, k;
  i = pointL;
  k = pointH;
  while(i < pointH && k > pointL) {
           for(j = 0; j < lineNumber; j++) {
             strip.setPixelColor(i-j, c);
             strip.setPixelColor(k+j, c);
           }
  strip.show();   // write all the pixels out
  delay(wait);
  i++;
  k--;
  turnAllOff();
  }
}

void turnAllOn(uint32_t c, uint32_t wait) {
  int i;  
  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, c);  // turn all pixels on
  }
  strip.show();   // write all the pixels out
  delay(wait); 
}

void turnSegmentOn(uint32_t c, uint32_t wait, uint8_t startofseg, uint8_t endofseg) {
  int i;  
  for (i=startofseg; i <= endofseg; i++) {
    strip.setPixelColor(i, c);  // turn all pixels on
  }
  strip.show();   // write all the pixels out
  delay(wait); 
}


void fadeUpSeg(uint32_t r, uint32_t g, uint32_t b, uint32_t wait, uint8_t startofseg, uint8_t endofseg) {
  int i, j;
  for (j=0; j <= 100; j++) {
    for (i=startofseg; i<=endofseg; i++) {
      strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100));
    }
  strip.show();
  }
  delay(wait);
}

void fadeDownSeg(uint32_t r, uint32_t g, uint32_t b, uint32_t wait, uint8_t startofseg, uint8_t endofseg) {
  int i, j;
  for (j=100; j >= 0; j--) {
    for (i=startofseg; i<=endofseg; i++) {
      strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100));  // turn all pixels on
    }
  strip.show();
  }
  delay(wait);
}

void fadeUp(uint32_t r, uint32_t g, uint32_t b, uint32_t wait) {
  int i, j;
  for (j=0; j <= 100; j++) {
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100));
    }
  strip.show();
  }
  delay(wait);
}

void fadeDown(uint32_t r, uint32_t g, uint32_t b, uint32_t wait) {
  int i, j;
  for (j=100; j >= 0; j--) {
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, strip.Color((r*j)/100,(g*j)/100,(b*j)/100));  // turn all pixels on
    }
  strip.show();
  }
  delay(wait);
}

void chaseMultipleLinesLToH(uint8_t lineNumber, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i, j;
         for(i = pointL; i <= pointH; i++) {
           for(j = 0; j < lineNumber; j++) {
             strip.setPixelColor(i, strip.Color(0,0,127));
             strip.setPixelColor(i+j+(2*lineNumber), strip.Color(127,0,0));
             strip.setPixelColor(i+j+(3*lineNumber), strip.Color(0,127,0));
             strip.setPixelColor(i+j+(4*lineNumber), strip.Color(127,127,0));
             strip.setPixelColor(i+j+(5*lineNumber), strip.Color(0,127,127));
             strip.setPixelColor(i+j+(6*lineNumber), strip.Color(127,0,127));
             strip.setPixelColor(i+j+(7*lineNumber), strip.Color(0,0,127));
             strip.setPixelColor(i+j+(8*lineNumber), strip.Color(127,0,0));
             strip.setPixelColor(i+j+(9*lineNumber), strip.Color(0,0,0));
             
           }
         strip.show();   // write all the pixels out
         delay(wait);
         }  
}

void turnAllOff() {
  int i;  
  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);  // turn all pixels off
  }
  strip.show();   // write all the pixels out 
}

void randomSparkleUpSegment(uint32_t c, uint32_t density, uint8_t wait, uint8_t pointL, uint8_t pointH) {
  int i,j,location;
    for (j=pointL; j<=pointH; j++) {
      for (i=0; i <= density; i++) {
      location = random(pointL,j);
      strip.setPixelColor(location, c);
      }
    strip.show();   // write all the pixels out
    delay(wait);
    turnAllOff();
    strip.show();   // write all the pixels out
    }  
}

void randomSparkleUpStrip(uint32_t c, uint32_t density, uint8_t wait) {
  int i,j,location;
    for (j=density; j<strip.numPixels(); j++) {
      for (i=0; i <= density; i++) {
      location = random(0,j);
      strip.setPixelColor(location, c);
      }
    strip.show();   // write all the pixels out
    delay(wait);
    turnAllOff();
    strip.show();   // write all the pixels out
    }  
}

void randomSparkle(uint32_t c, uint32_t density, uint8_t wait) {
  int i,j,location;
  for (j=0; j < 200; j++) {
      for (i=0; i <= density; i++) {
         location = random(0,strip.numPixels()); //random location in first 5th of strip
         strip.setPixelColor(location, c);
      }
    strip.show();   // write all the pixels out
    delay(wait);
    turnAllOff();
    strip.show();   // write all the pixels out
         }
}

void randomColorSparkle(uint8_t wait) {
  int i;
  int location1, location2, location3, location4, location5;
  int color1, color2, color3;
       for (i=0; i < 200; i++) {
         location1 = random(0,32); //random location in first 5th of strip
         location2 = random(33,64); //random location in 2nd 5th of strip
         location3 = random(65,96); //random location in 3rd 5th of strip
         location4 = random(97,128); //random location in 4th 5th of strip
         location5 = random(129,159); //randon location in last 5th of strip
         
         color1 = random(127);
         color2 = random(127);
         color3 = random(127);
         
         strip.setPixelColor(location1, strip.Color(color1,color2,0));
         strip.setPixelColor(location2, strip.Color(color3,0,color1));
         strip.setPixelColor(location3, strip.Color(0,color2,color3));
         strip.setPixelColor(location4, strip.Color(color1,color2,0));
         strip.setPixelColor(location5, strip.Color(color3,0,color1));
         strip.show();   // write all the pixels out
         delay(wait);
         strip.setPixelColor(location1, strip.Color(0,0,0));
         strip.setPixelColor(location2, strip.Color(0,0,0));
         strip.setPixelColor(location3, strip.Color(0,0,0));
         strip.setPixelColor(location4, strip.Color(0,0,0));
         strip.setPixelColor(location5, strip.Color(0,0,0));
         strip.show();   // write all the pixels out
         }
}

void twoColorSparkle(uint32_t c1, uint32_t c2, uint32_t density, uint8_t wait) {
  int i,j,location1,location2;
  for (j=0; j < 1; j++) {
      for (i=0; i <= density; i++) {
         location1 = random(0,strip.numPixels()); //random location number 1
         location2 = random(0,strip.numPixels()); //random location number 2
         strip.setPixelColor(location1, c1);
         strip.setPixelColor(location2, c2);
      }
    strip.show();   // write all the pixels out
    delay(wait);
    turnAllOff();
    strip.show();   // write all the pixels out
         }
}

void rainbow(uint8_t wait) {
  int i, j;
   
  for (j=0; j < 384; j++) {     // 3 cycles of all 384 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel( (i + j) % 384));
    }  
    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) {
  uint16_t i, j;
  
  for (j=0; j < 384 * 1; j++) {     // 5 cycles of all 384 colors in the wheel
    for (i=0; i < strip.numPixels(); i++) {
      // tricky math! we use each pixel as a fraction of the full 384-color wheel
      // (thats the i / strip.numPixels() part)
      // Then add in j which makes the colors go around per pixel
      // the % 384 is to make the wheel cycle around
      strip.setPixelColor(i, Wheel( ((i * 384 / strip.numPixels()) + j) % 384) );
    }  
    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(uint32_t c, uint8_t wait) {
  int i;
  
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      strip.show();
      delay(wait);
  }
}

// Chase a dot down the strip
// good for testing purposes
void colorChase(uint32_t c, uint8_t wait) {
  int i;
  
  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);  // turn all pixels off
  } 
  
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, c);
      if (i == 0) { 
        strip.setPixelColor(strip.numPixels()-1, 0);
      } else {
        strip.setPixelColor(i-1, 0);
      }
      strip.show();
      delay(wait);
  }
}

void colorOverColorChase(uint32_t color, uint32_t backgroundColor, uint8_t wait) {
  int i;
  
  for (i=0; i < strip.numPixels(); i++) {
    strip.setPixelColor(i, 0);  // turn all pixels off
  } 
  
  for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, color);
      if (i == 0) { 
        strip.setPixelColor(strip.numPixels()-1, backgroundColor);
      } else {
        strip.setPixelColor(i-1, backgroundColor);
      }
      strip.show();
      delay(wait);
  }
}

/* Helper functions */

//Input a value 0 to 384 to get a color value.
//The colours are a transition r - g -b - back to r

uint32_t Wheel(uint16_t WheelPos)
{
  byte r, g, b;
  switch(WheelPos / 128)
  {
    case 0:
      r = 127 - WheelPos % 128;   //Red down
      g = WheelPos % 128;      // Green up
      b = 0;                  //blue off
      break; 
    case 1:
      g = 127 - WheelPos % 128;  //green down
      b = WheelPos % 128;      //blue up
      r = 0;                  //red off
      break; 
    case 2:
      b = 127 - WheelPos % 128;  //blue down 
      r = WheelPos % 128;      //red up
      g = 0;                  //green off
      break; 
  }
  return(strip.Color(r,g,b));
}

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Triggering From Serial Input

Post by ckeyes »

I think the problem I'm having is in adding the effect in the "break j" area of the script.

I tried some variations of this: colorOverColorChase(strip.Color( 0,127, 0), 100);
but am really only guessing.

Might you know how I should add the code there as in the above script I posted?

Thanks,

Carl

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Triggering From Serial Input

Post by ckeyes »

I found that using this: colorOverColorChase(strip.Color(100,10,30),100,10);
in the break section works, but simply chases and fills the strip with one color.

Carl

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: Triggering From Serial Input

Post by adafruit_support_bill »

I found that using this: colorOverColorChase(strip.Color(100,10,30),100,10);
in the break section works, but simply chases and fills the strip with one color.
ColorOverColorChase takes 2 colors (the chase color and the background color). You are passing it '100' for the background color.

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Triggering From Serial Input

Post by ckeyes »

Gotcha, thanks. Can the scanner code here be changed to have the "comet" travel the strip over a background color?
The colorchase is very similar to what I did have working.

Reason I ask is that I have yet to see anywhere, and I've searched quite a bit, any videos or sample
code that would demonstrate anything near the effects in the video I posted above.
I have no idea what strip is used or what's driving it, but there are some very cool effects in it
that I've never seen an LPD8806 do....hope it can...not looking forward to starting over with something
else.

Getting a nice comet traveling over a background color might indicate that the other effects
are at least possible...then on to white sparkles over a smooth flowing rainbow :D

Thanks,

Carl

Code: Select all

void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t wait) {
  int i, j, pos, dir;
  pos = 0;
  dir = 1;
  for(i=0; i<((strip.numPixels()-1) * 2); i++) {
    // Draw 5 pixels centered on pos. setPixelColor() will clip
    // any pixels off the ends of the strip, no worries there.
    // we'll make the colors dimmer at the edges for a nice pulse
    // look
    strip.setPixelColor(pos - 2, strip.Color(r/30, g/30, b/30));
    strip.setPixelColor(pos - 1, strip.Color(r/4, g/4, b/4));
    strip.setPixelColor(pos, strip.Color(r, g, b));
    strip.setPixelColor(pos + 1, strip.Color(r/4, g/4, b/4));
    strip.setPixelColor(pos + 2, strip.Color(r/30, g/30, b/30));

    strip.show();
    delay(wait);
    // If we wanted to be sneaky we could erase just the tail end
    // pixel, but it's much easier just to erase the whole thing
    // and draw a new one next time.
    for(j=-2; j<= 2; j++)
        strip.setPixelColor(pos+j, strip.Color(0,0,0));
    // Bounce off ends of strip
    pos += dir;
    if(pos < 0) {
      pos = 1;
      dir = -dir;
    } else if(pos >= strip.numPixels()) {
      pos = strip.numPixels() - 2;
      dir = -dir;
    }
  }
}

User avatar
adafruit_support_bill
 
Posts: 88144
Joined: Sat Feb 07, 2009 10:11 am

Re: Triggering From Serial Input

Post by adafruit_support_bill »

It's possible, but you would need to do a bit of math. This code assumes black as a background color, and simply divides the R, G and B components so that they fade in and out. You could probably just add the R, G & B components of the background color to that, but you would have to constrain the results to valid values (255 or less). You will get different effects as you are mixing colors. You will probably have to do some experimentation to fine-tune the effect.

Code: Select all

    strip.setPixelColor(pos - 2, strip.Color(r/30, g/30, b/30));
    strip.setPixelColor(pos - 1, strip.Color(r/4, g/4, b/4));
    strip.setPixelColor(pos, strip.Color(r, g, b));
    strip.setPixelColor(pos + 1, strip.Color(r/4, g/4, b/4));
    strip.setPixelColor(pos + 2, strip.Color(r/30, g/30, b/30));

ckeyes
 
Posts: 36
Joined: Tue Oct 11, 2011 2:19 am

Re: Triggering From Serial Input

Post by ckeyes »

Appreciate all the help, but I'm not much of a coder and learn a lot more from
working with examples. It wouldn't appear they aren't any of the multi or layered
effects sketches around to learn from so I'm thinking of going the DMX route and design
effects using a GUI.

Many thanks,

Carl

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

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