Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Reciever Code for LED Panels using WS2812b strips.
- * incoming data over serial connection is passed out to next unit
- * Format for transitions from sender is:
- * F,rrr,ggg,bbb,XX - "F"ade where target color is RRR,GGG,BBB and time of fade is XX in seconds
- * X = immediate blackout
- */
- //Libraries
- #include <FastLED.h>
- //#include <SoftwareSerial.h>
- //SoftwareSerial inSerial(12,13); //Set Serial Data IN pins from controller/previous unit
- //SoftwareSerial outSerial(10,11); //Set Serial Data OUT pins to next unit
- // Preset Variables
- // FastLED Setup
- #define NUM_LEDS 15 //LEDs Per Panel
- #define DATA_PIN 2 //Data Pin for LEDS
- CRGB leds[NUM_LEDS]; //Build LED Array
- // Color Handling
- uint8_t endColor[3] = {0, 0, 0}; //New Color RGB (Starts in Black)
- uint8_t startColor[3] = {10, 150, 250}; //Previous Color RGB (Non-Zero for Debug)
- //uint8_t startColor[3] = {0, 0, 0}; //Previous Color RGB (Starts in Black)
- uint8_t curColor[3] = {0, 0, 0}; //Transitional Color Holder
- //Time Handling
- int cueTime = 0; //Cue Run Time
- float startTime = millis(); //Cue Start
- float curTime = millis(); //Cue Elapsed
- //Math Handling
- float colStep[3] = {0, 0, 0}; //Number of steps between start/end color
- float stepTime[3] = {0, 0, 0}; //Millis per step
- bool cueRunning = false; //Skip the math if it's already done
- float timeStep = 0; //Number of steps at time
- uint8_t t = 0; //Type conversion of timeStep
- //Serial Buffer Handling
- const int buflen = 32; //Serial Buffer Length Maximum
- char inBuffer[buflen]; //Serial Buffer
- static int inlength = 0; //Set beginning of buffer
- void setup() {
- // initialize serial:
- Serial.begin(9600); // USB serial for debugging
- // inSerial.begin(9600); // Input from controller or previous board
- // outSerial.begin(9600); // Output to next board
- Serial.setTimeout(3600000); // Set all serial timeouts to 1 hour
- // inSerial.setTimeout(3600000);
- // outSerial.setTimeout(3600000);
- //Set Up LEDs
- FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
- Serial.println(F("Setup Complete"));
- //Populate buffer array for later manipulation
- for (int i=0; i<buflen; i++) {
- inBuffer[buflen] = 0;
- }
- }
- void loop() {
- // Incoming Serial Data
- if (Serial.available()) { // Read Input
- int data = Serial.read(); // Read incoming byte
- if (data == '\n') { // Newline means end of Command
- Serial.println("Command");
- inBuffer[inlength] = '\0'; // Terminate Command
- Serial.print("inBuffer Array: ");
- for(int i = 0; i < inlength; i++){
- Serial.print(inBuffer[i]);
- }
- Serial.println(" ");
- // Serial.print("inBuffer: ");
- // Serial.println(inBuffer);
- Serial.print("inBuffer0: ");
- Serial.println(inBuffer[0]);
- if (inlength) { //Check for actual data
- if (inBuffer[0]== 'F') { //Command is for firing a fade to fade to solid color
- // Serial.println(inBuffer);
- int ci = 0; //color increment
- uint8_t colors[5];
- char *val = strtok (inBuffer,","); //break the buffer in to its RGB Components
- // Serial.println(val);
- colors[ci] = atoi(val);
- while ((val = strtok(NULL,",")) != NULL) { //populate color array
- // Serial.println(val);
- colors[++ci] = atoi(val);
- }
- // Serial.print("Colors: R");
- // Serial.print(colors[1]);
- // Serial.print(" G");
- // Serial.print(colors[2]);
- // Serial.print(" B");
- // Serial.print(colors[3]);
- // Serial.print(" - Time:");
- // Serial.println(colors[4]);
- // Serial.print("End Color: ");
- for (int ei=1; ei<4; ei++) {
- endColor[ei] = colors[ei];
- // Serial.print(ei);
- // Serial.print("-");
- // Serial.print(endColor[ei]);
- // Serial.print(" ");
- }
- // Serial.println(" ");
- // Serial.print("startColor: 1-");
- // Serial.print(startColor[0]);
- // Serial.print(" G");
- // Serial.print(startColor[1]);
- // Serial.print(" B");
- // Serial.println(startColor[2]);
- cueTime = colors[4];
- if (cueRunning == false) {
- Serial.println(F("Cue Running..."));
- // Compute Fade Amounts
- for (int i = 0; i < 3; i++) {
- // Serial.print(F("Color Math... "));
- colStep[i] = startColor[i] - endColor[i+1];
- if (colStep[i] == 0) {
- stepTime[i] = 0;
- }
- else {
- stepTime[i] = ((cueTime * 1000) / colStep[i]);
- }
- // Serial.print (F("Step Time: "));
- // Serial.println (stepTime[i]);
- }
- startTime = millis();
- cueRunning = true;
- }
- //Display Fades
- curTime = millis();
- // Serial.print("startTime: ");
- // Serial.print(startTime);
- // Serial.print(" curTime: ");
- // Serial.println(curTime);
- while (curTime <= (startTime+(cueTime*1000))){
- for (int j = 0; j < 3; j++) {
- // Serial.print (F(" "));
- // Serial.print (j);
- if (stepTime[j] == 0) {
- curColor[j] = startColor[j];
- // Serial.print (F("t=0"));
- }
- else {
- timeStep = ((curTime-startTime) / stepTime[j]);
- t=(uint8_t) timeStep;
- // Serial.print (F("t="));
- // Serial.print (t);
- curColor[j] = startColor[j] - t;
- }
- // Serial.println(" ");
- }
- for(int i = (NUM_LEDS)-1; i >= 0; i--) {
- leds[i] = CRGB(curColor[0],curColor[1],curColor[2]);
- FastLED.show();
- }
- // Serial.print("LED show: ");
- // Serial.print(curColor[0]);
- // Serial.print("-");
- // Serial.print(curColor[1]);
- // Serial.print("-");
- // Serial.println(curColor[2]);
- curTime = millis();
- }
- //else {
- cueRunning = false;
- Serial.println("End Cue");
- memset(inBuffer,0,buflen);
- Serial.println("Cue Buffer Clear");
- //}
- }
- else if (inBuffer[0]== 'X') { //Blackout Shortcut
- Serial.println(F("Blackout"));
- for(int i = (NUM_LEDS)-1; i >= 0; i--) {
- leds[i] = CRGB(0,0,0);
- FastLED.show();
- memset(inBuffer,0,buflen);
- Serial.println("BO Buffer Clear");
- }
- }
- else{ //We don't know what to do with this data
- Serial.println(F("*** BAD DATA ***"));
- memset(inBuffer,0,buflen);
- Serial.println("XX Buffer Clear");
- }
- }
- }
- else if (inlength < buflen - 1) { //still data coming in? Add it to the buffer
- inBuffer[inlength++] = data;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement