Almost done, thanks again for the help.
I have everything working as planned except for the thing I didn't plan for.
If you look at the code section titled
"void DualChaseToTarget()"
It's close to the bottom and you'll see that I'm incrementing/decrementing two strip.setPixelColor commands by one for every iteration until they reach LEDLoc1, or the 'center' LED of both chases, then repeating a certain number of times.
This is great except when the two chases need to start different distances from the center of the chases. If the number of LEDs to the RIGHT is greater than the number of LEDs to the LEFT then the chase over-runs. If the case is reversed it under-runs.
- Strip_Array.jpg (39.8 KiB) Viewed 1651 times
I tried using 'if' statements but I ended up neck-deep in curly braces. I'm not really sure how to go at this. Any advice? Ideally I'd like to stop whichever chase has reached the center before the other but let the other to complete normally.
I have this idea that it might be possible to determine which is a greater distance form the center then iterate that number of times, then use an 'if' statement inside the loop to determine if one chase has already reached the center but my brain doesn't quite wrap around that.
If you want to test this you'll need a five meter strip, connect to pins 11 and 12, open the serial monitor and paste/send this:
Adjust the starting points of the chases by changing the third and fourth group of numbers (33 and 53). More details about this data string in the comments at the top of the sketch.
I'm sure that there is a LOT of good advice to be given about changing other things in this sketch and I definitely want to hear what anyone has to say if they can explain WHY they would make the change. This IS the way it IS because it makes sense to me and (but for this issue) works well for me. Let me know if I've broken any major rules or can benefit from you suggestion.
Thanks.
Code: Select all
#include "LPD8806.h"
#include "SPI.h"
/*****************************************************************************/
// Name the strings used.
String stringBinLoc, stringLEDLoc1, stringLEDLoc2, stringLEFT, stringRIGHT, stringRED, stringGRN, stringBLU, stringBlinkDelay, stringChaseDelay, stringChaseRepetitions;
// LED color and location information comes in from the computer to the Arduino as
// BinLocation,LEDLocation,LEFTLocation,RIGHTLocation,REDValue,GreenValue,BlueValue,BlinkDelay,ChaseDelay,ChaseRepetitions
// ex.: 312,43,33,53,0,127,0,350,75,5
int nLEDs = 160; // Number of RGB LEDs in strip
int BinLoc = 0; // Value for Bin Location
int LEDLoc1 = 0; // Value for LED Location
int LEDLoc2 = 0; // Value for second LED Location
int RED = 0; // Value for Red
int GRN = 0; // Value for Green
int BLU = 0; // Value for Blue
int i = 0; // Current chase loop iteration
int j = 0; // LEFT value for chase
int k = 0; // RIGHT value for chase
int LEFT = 0; // Fill from left starting pixel
int RIGHT = 0; // Fill from right starting pixel
int REPS = 5; // Number of times to chase to target
int waitBlink = 350; // Delay between blink shifts
int waitChase = 75; // Delay between chase shifts
int incomingByte = 0; // for incoming serial data
// Choose 2 pins for output; can be any valid output pins:
int dataPin = 11;
int clockPin = 12;
// 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(nLEDs, dataPin, clockPin);
void setup() {
stringBinLoc = String("Bin Location: ");
stringLEDLoc1 = String("LED Location 1: ");
stringLEDLoc2 = String("LED Location 2: ");
stringLEFT = String("Left Location: ");
stringRIGHT = String("Right Location: ");
stringRED = String("Red Value: ");
stringGRN = String("Green Value: ");
stringBLU = String("Blue Value: " );
stringBlinkDelay = String("Blink Delay: ");
stringChaseDelay = String("Chase Delay: ");
stringChaseRepetitions = String ("Chase Repetitions: ");
strip.begin(); // Start up the LED strip
strip.show(); // Update the strip, to start they are all 'off'
Serial.begin(9600); // Open the serial port at 9600 bps:
}
void loop()
{
Serial.println(RED);
Serial.println(GRN);
Serial.println(BLU);
if (Serial.available() > 0) // if there's any serial data available...
{
////////////////////////////////////////////////////////////////////
// Parse all the data from the PC, look for the next valid integer
// in the incoming serial stream:
int BinLoc = Serial.parseInt(); // Assign this value to BinLoc
// do it again:
LEDLoc1 = Serial.parseInt(); // Assign this value to LEDLoc1
LEFT = Serial.parseInt(); // Assign this value to LEFT
RIGHT = Serial.parseInt(); //Assign this value to RIGHT
RED = Serial.parseInt(); // Assign this value to RED
GRN = Serial.parseInt(); // Assign this value to GRN
BLU = Serial.parseInt(); // Assign this value to BLU
waitBlink = Serial.parseInt(); // Assign this value to waitBlink
waitChase = Serial.parseInt(); // Assign this value to waitChase
REPS = Serial.parseInt(); // Assign this value to REPS
LEDLoc2 = LEDLoc1++; // Assign second LED location value by
// incrementing LEDLoc1 by 1.
if (Serial.read() == '\n'); // Newline is the end of the sentence:
/////////////////////////////////////////////////////////////////////
// Return the values received to the computer for debugging purposes.
Serial.println(stringBinLoc+BinLoc);
Serial.println(stringLEDLoc1+LEDLoc1);
Serial.println(stringLEDLoc2+LEDLoc2);
Serial.println(stringLEFT+LEFT);
Serial.println(stringRIGHT+RIGHT);
Serial.println(stringRED+RED);
Serial.println(stringGRN+GRN);
Serial.println(stringBLU+BLU);
Serial.println(stringBlinkDelay+waitBlink);
Serial.println(stringChaseDelay+waitChase);
Serial.println(stringChaseRepetitions+REPS);
DualChaseToTarget(); // Perform the "Attention Getter"
}
else
{
Blink(); // Indicate desired location for the product.
}
}
////////////////////////////////////////////////////////////////
// This subroutine executes the blinking function
void Blink()
{
strip.setPixelColor(LEDLoc1, RED,GRN,BLU); // set pixel # ON
// Serial.println ("Pixel 1 On");
strip.setPixelColor(LEDLoc2, 0,0,0); // set pixel # OFF
// Serial.println ("Pixel 2 Off");
strip.show(); // Apply the above changes
delay(waitBlink);
// Alternate
strip.setPixelColor(LEDLoc1, 0,0,0); // set pixel # OFF
// Serial.println ("Pixel 1 Off");
strip.setPixelColor(LEDLoc2, RED,GRN,BLU); // set pixel # ON
// Serial.println ("Pixel 2 On");
strip.show(); // Apply the above changes
delay(waitBlink);
}
////////////////////////////////////////////////////////////////
// This subroutine executes the chase function
void DualChaseToTarget()
{
// Serial.println(REPS);
i = 0;
while (i < REPS) // Perform the loop a number of times = to REPS
{
j = LEFT; // Assign the value of LEFT to j so we can increment it but not lose the value of LEFT
k = RIGHT; // Assign the value of RIGHT to k so we can decrement it but not lose the value of RIGHT
while (j < LEDLoc1) // Chase until j is not less than the pixel number of LEDLoc1
{
strip.setPixelColor(j++, RED,GRN,BLU); // Turn on pixel j+1
strip.setPixelColor(k--, RED,GRN,BLU); // Turn on pixel k-1
strip.show(); // Apply
strip.setPixelColor(j-1, 0,0,0); // Turn off pixel j-1 so we don't leave a trail
strip.setPixelColor(k+1, 0,0,0); // Turn off pixel k+1 so we don't leave a trail
}
delay(waitChase);
i = i + 1;
Serial.println (i);
}
}