Arduino and pixels V1

From diychristmas.org wiki
Jump to navigation Jump to search

This is untested code. Awaiting feedback. Serial may not support this many pixels at 115200.

// Basic Vixen "generic" serial 
//  - Setup a generic serial controller to use this sketch

#include <FastLED.h> //include the FastLED library in the Arduino project
#define LED_PIN1 2 //for first arc strip of 100 LEDs, pin 2 on the MEGA
#define LED_PIN2 3 //for second arc strip of 100 LEDs, pin 3 on the MEGA
#define LED_PIN3 4 //for third arc strip of 100 LEDs, pin 4 on the MEGA
#define LED_PIN4 5 //for fourth arc strip of 100 LEDs, pin 5 on the MEGA
#define LED_PIN5 6 //for fifth arc strip of 100 LEDs, pin 6 on the MEGA


// Vixen information
// speed for the com port for talking with player
const long BAUD_RATE = 115200L;
#define NUM_LEDS 100
// Generic Serial controller config - must be present, must match controller setup
const int CONTROLLER_HEADER[3] = {33, 34, 35}; // This is character !"# (hard to replicate in sequencer)

#define GRB_ORDER GRB //pixel strips is set to use GRB color codes

// these are program variables we need to use in multiple places
const int SIZE_OF_HEADER = sizeof(CONTROLLER_HEADER) / sizeof(int); // no need to change

#define LED_TYPE WS2811 //type of LED

//create arrays to hold the FastLED CRBG code for each of the 50 pixels in the:
CRGB leds1[NUM_LEDS]; //first arc.
CRGB leds2[NUM_LEDS]; //second arc.
CRGB leds3[NUM_LEDS]; //third arc.
CRGB leds4[NUM_LEDS]; //fourth arc.
CRGB leds5[NUM_LEDS]; //fifth arc.

int g; //a variable for loop section

void setup() {
  delay(1000);
  Serial.begin(BAUD_RATE);
  pinMode(LED_PIN1, OUTPUT); //set pins to output. PIN1 is pin 2 on the Arduino board.
  pinMode(LED_PIN2, OUTPUT);
  pinMode(LED_PIN3, OUTPUT);
  pinMode(LED_PIN4, OUTPUT);
  pinMode(LED_PIN5, OUTPUT);

  ////Set up the pixel strips to run using FastLED
  FastLED.addLeds<LED_TYPE, LED_PIN1, GRB_ORDER>(leds1, NUM_LEDS).setCorrection(TypicalLEDStrip); //for the first arc
  FastLED.addLeds<LED_TYPE, LED_PIN2, GRB_ORDER>(leds2, NUM_LEDS).setCorrection(TypicalLEDStrip); //for the second arc
  FastLED.addLeds<LED_TYPE, LED_PIN3, GRB_ORDER>(leds3, NUM_LEDS).setCorrection(TypicalLEDStrip); //for the third arc
  FastLED.addLeds<LED_TYPE, LED_PIN4, GRB_ORDER>(leds4, NUM_LEDS).setCorrection(TypicalLEDStrip); //for the fourth arc
  FastLED.addLeds<LED_TYPE, LED_PIN5, GRB_ORDER>(leds5, NUM_LEDS).setCorrection(TypicalLEDStrip); //for the fifth arc
  powerOnSelfTest(); // watch your lights to make sure they are all going on in order
}

void loop() {
  if (Serial.available() > 0) {
    waitForControllerHeader(CONTROLLER_HEADER);
    readSequenceData();
    FastLED.show(); //then we tell FastLED to show the pixels!
  }
}

void readSequenceData()
{
  // We assume that since we found the header, we can just read the rest of the bytes
  for (g = 0; g < NUM_LEDS; g++) {
    Serial.readBytes( ( char*)(&leds1[g]), 3);
  }
  for (g = 0; g < NUM_LEDS; g++) {    //then we read the next 300 bytes for the second arc of LEDs
    Serial.readBytes( ( char*)(&leds2[g]), 3);
  }
  for (g = 0; g < NUM_LEDS; g++) {    //then we read the next 300 bytes for the third arc of LEDs
    Serial.readBytes( ( char*)(&leds3[g]), 3);
  }
  for (g = 0; g < NUM_LEDS; g++) {    //then we read the next 300 bytes for the fourth arc of LEDs
    Serial.readBytes( ( char*)(&leds4[g]), 3);
  }
  for (g = 0; g < NUM_LEDS; g++) {    //then we read the next 300 bytes for the fifth arc of LEDs
    Serial.readBytes( ( char*)(&leds5[g]), 3);
  }
}

void waitForControllerHeader(int header[])
{
  for (int i = 0; i < SIZE_OF_HEADER; i++) {
    // wait for serial available
    while (!Serial.available()) {}
    // Check the byte until it matches the CONTROLLER_HEADER byte
    int inByte = Serial.read();
    if (inByte != CONTROLLER_HEADER[i]) {
      i = -1; // wrong data set to "zero"
    }
  }
  // found the header!
}

// powerOnSelfTest - does a couple of checks to make sure everything turns on and off
//  - watch your lights, they should go on in order
void powerOnSelfTest()
{
  // Let's test if all the pixels work.
  // This will be too fast for real discernment but they should all be on at end of this
  FastLED.clear();
  for (int channelIndex = 0; channelIndex < NUM_LEDS; channelIndex++) {
    leds1[channelIndex] = CRGB::Red;
    FastLED.show();
  }
  delay(500);
  for (int channelIndex = 0; channelIndex < NUM_LEDS; channelIndex++) {
    leds2[channelIndex] = CRGB::Red;
    FastLED.show();
  }
  delay(500);
  for (int channelIndex = 0; channelIndex < NUM_LEDS; channelIndex++) {
    leds3[channelIndex] = CRGB::Red;
    FastLED.show();
  }
  delay(500);
  for (int channelIndex = 0; channelIndex < NUM_LEDS; channelIndex++) {
    leds4[channelIndex] = CRGB::Red;
    FastLED.show();
  }
  delay(500);
  for (int channelIndex = 0; channelIndex < NUM_LEDS; channelIndex++) {
    leds5[channelIndex] = CRGB::Red;
    FastLED.show();
  }
  delay(2000); // slight pause to check all on
  FastLED.clear();
  FastLED.show();
}