Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //fBlah
- #include <Adafruit_NeoPixel.h>
- #define PIN 3
- #define PINC 6
- #define NUM_LED 60
- #define NUM_LED_F 57 //Chassis lights only 57
- #define NUM_DATA 187 // NUM_LED * 3 + 2 176 + 3 + 2 (TOTAL DATA)
- #define RECON_TIME 100 // after x seconds idle time, send afk again.
- // Parameter 1 = number of pixels in strip
- // Parameter 2 = pin number (most are valid)
- // Parameter 3 = pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LED, PIN, NEO_GRB + NEO_KHZ800);
- Adafruit_NeoPixel chassis = Adafruit_NeoPixel(NUM_LED_F, PINC, NEO_GRB + NEO_KHZ800);
- //Adafruit_NeoPixel front = Adafruit_NeoPixel(NUM_LED_F, PINF, NEO_GRB + NEO_KHZ800);
- int iRed = 0;
- boolean bRed = true;
- uint8_t led_color[NUM_DATA];
- int index = 0;
- unsigned long last_afk = 0;
- unsigned long last_ack = 0;
- unsigned long cur_time = 0;
- String inputString = ""; // a string to hold incoming data
- boolean stringComplete = false; // whether the string is complete
- byte sleep = 255;
- byte mode = 0;
- byte mmode = 0;
- byte power = 1;
- byte stay = 0;
- void setup() {
- strip.begin();
- chassis.begin();
- //front.begin();
- strip.show(); // Initialize all pixels to 'off'
- chassis.show();
- inputString.reserve(NUM_DATA);
- Serial.begin(115200);
- Serial.print("ozy"); // Send ACK string to host
- strip.setBrightness(100);
- chassis.setBrightness(100);
- }
- void loop() {
- if (stringComplete) {
- //Serial.write('i');
- last_ack = last_afk = millis();
- index = 0;
- if ((inputString[0] == 'a') && (inputString[1] == 'd') && (stay == 0)){
- // update LEDs
- int led_index = 2;
- for(int i = NUM_LED - 1; i > -1; i--){
- //led_index = i*3 + 2;
- strip.setPixelColor(i, strip.Color(inputString[led_index++], inputString[led_index++], inputString[led_index++]));
- }
- for(int i = NUM_LED_F - 1; i > -1; i--){
- //led_index = i*3 + 2;
- chassis.setPixelColor(i, strip.Color(inputString[182], inputString[183], inputString[184]));
- }
- strip.setBrightness(inputString[185]);
- chassis.setBrightness(inputString[186]);
- }
- if ((inputString[0] == 'a') && (inputString[1] == 'm') || (stay == 1)){
- // update LEDs
- int led_index = 2;
- byte r,g,b,n,s;
- power = inputString[led_index++];
- mmode = inputString[led_index++];
- r = inputString[led_index++];
- g = inputString[led_index++];
- b = inputString[led_index++];
- n = inputString[led_index++];
- s = inputString[led_index++];
- stay = inputString[led_index++];
- switch(mmode)
- {
- case 0:
- // Some example procedures showing how to display to the pixels:
- colorWipe(strip.Color(255, 0, 0), 50); // Red
- break;
- case 1:
- colorWipe(strip.Color(0, 255, 0), 50); // Green
- break;
- case 2:
- colorWipe(strip.Color(0, 0, 255), 50); // Blue
- break;
- case 3:
- // Send a theater pixel chase in...
- theaterChase(strip.Color(127, 127, 127), 50); // White
- break;
- case 4:
- theaterChase(strip.Color(127, 0, 0), 50); // Red
- break;
- case 5:
- theaterChase(strip.Color( 0, 0, 127), 50); // Blue
- break;
- case 6:
- rainbow(20);
- break;
- case 7:
- rainbowCycle(20);
- break;
- case 8:
- theaterChaseRainbow(50);
- break;
- }
- chassis.setPixelColor(0, strip.Color(inputString[182], inputString[183], inputString[184]));
- strip.setBrightness(inputString[185]);
- chassis.setBrightness(inputString[186]);
- }
- if(power == 0)
- {
- strip.setBrightness(0);
- chassis.setBrightness(0);
- }
- strip.show();
- chassis.show();
- inputString = "";
- stringComplete = false;
- sleep = 255;
- }else{
- cur_time = millis();
- if (cur_time - last_afk > RECON_TIME){
- index = 0;
- inputString = "";
- Serial.write('y');
- //delay(5);
- last_afk = millis();
- if(last_afk - last_ack > 10000)
- {
- if(sleep > 20)
- {
- sleep--;
- strip.setBrightness(sleep);
- chassis.setBrightness(sleep+20);
- strip.show();
- chassis.show();
- }
- if(sleep == 20)
- {
- if(power == 0)
- {
- strip.setBrightness(0);
- chassis.setBrightness(0);
- }
- switch(mode++%9)
- {
- case 0:
- // Some example procedures showing how to display to the pixels:
- colorWipe(strip.Color(255, 0, 0), 50); // Red
- break;
- case 1:
- colorWipe(strip.Color(0, 255, 0), 50); // Green
- break;
- case 2:
- colorWipe(strip.Color(0, 0, 255), 50); // Blue
- break;
- case 3:
- // Send a theater pixel chase in...
- theaterChase(strip.Color(127, 127, 127), 50); // White
- break;
- case 4:
- theaterChase(strip.Color(127, 0, 0), 50); // Red
- break;
- case 5:
- theaterChase(strip.Color( 0, 0, 127), 50); // Blue
- break;
- case 6:
- rainbow(20);
- break;
- case 7:
- rainbowCycle(20);
- break;
- case 8:
- theaterChaseRainbow(50);
- break;
- }
- }
- }
- }
- }
- }
- void serialEvent() {
- while (Serial.available()) {
- // get the new byte:
- //char inChar = (char)Serial.read();
- index++;
- inputString += (char)Serial.read();
- // add it to the inputString:
- //inputString += inChar;
- // if the incoming character is a newline, set a flag
- // so the main loop can do something about it:
- if (index >= NUM_DATA) {
- stringComplete = true;
- }
- }
- }
- // Fill the dots one after the other with a color
- void colorWipe(uint32_t c, uint8_t wait) {
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, c);
- strip.show();
- chassisIdle();
- delay(wait);
- }
- }
- void rainbow(uint8_t wait) {
- uint16_t i, j;
- for(j=0; j<256; j++) {
- for(i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, Wheel((i+j) & 255));
- }
- chassisIdle();
- strip.show();
- delay(wait);
- }
- }
- // Slightly different, this makes the rainbow equally distributed throughout
- void rainbowCycle(uint8_t wait) {
- uint16_t i, j;
- for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
- for(i=0; i< strip.numPixels(); i++) {
- strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
- }
- chassisIdle();
- strip.show();
- delay(wait);
- }
- }
- //Theatre-style crawling lights.
- void theaterChase(uint32_t c, uint8_t wait) {
- for (int j=0; j<10; j++) { //do 10 cycles of chasing
- for (int q=0; q < 3; q++) {
- for (int i=0; i < strip.numPixels(); i=i+3) {
- strip.setPixelColor(i+q, c); //turn every third pixel on
- }
- strip.show();
- chassisIdle();
- delay(wait);
- for (int i=0; i < strip.numPixels(); i=i+3) {
- strip.setPixelColor(i+q, 0); //turn every third pixel off
- }
- }
- }
- }
- //Theatre-style crawling lights with rainbow effect
- void theaterChaseRainbow(uint8_t wait) {
- for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
- for (int q=0; q < 3; q++) {
- for (int i=0; i < strip.numPixels(); i=i+3) {
- strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
- }
- strip.show();
- chassisIdle();
- delay(wait);
- for (int i=0; i < strip.numPixels(); i=i+3) {
- strip.setPixelColor(i+q, 0); //turn every third pixel off
- }
- }
- }
- }
- // Input a value 0 to 255 to get a color value.
- // The colours are a transition r - g - b - back to r.
- uint32_t Wheel(byte WheelPos) {
- if(WheelPos < 85) {
- return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
- } else if(WheelPos < 170) {
- WheelPos -= 85;
- return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
- } else {
- WheelPos -= 170;
- return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
- }
- }
- void chassisIdle()
- {
- if(iRed > 254)
- bRed = false;
- else if(iRed < 1)
- bRed = true;
- if(bRed)
- {
- for(int i = NUM_LED_F - 1; i > -1; i--){
- //led_index = i*3 + 2;
- chassis.setPixelColor(i,iRed,0,0);
- }
- iRed++;
- }
- else
- {
- for(int i = NUM_LED_F - 1; i > -1; i--){
- //led_index = i*3 + 2;
- chassis.setPixelColor(i,iRed,0,0);
- }
- iRed--;
- }
- chassis.show();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement