Advertisement
Guest User

Arduino Code

a guest
Dec 7th, 2014
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.64 KB | None | 0 0
  1. //fBlah
  2. #include <Adafruit_NeoPixel.h>
  3.  
  4. #define PIN 3
  5. #define PINC 6
  6.  
  7. #define NUM_LED 60
  8. #define NUM_LED_F 57 //Chassis lights only 57
  9. #define NUM_DATA 187 // NUM_LED * 3 + 2 176 + 3 + 2 (TOTAL DATA)
  10. #define RECON_TIME 100 // after x seconds idle time, send afk again.
  11.  
  12. // Parameter 1 = number of pixels in strip
  13. // Parameter 2 = pin number (most are valid)
  14. // Parameter 3 = pixel type flags, add together as needed:
  15. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  16. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  17. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  18. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  19. Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LED, PIN, NEO_GRB + NEO_KHZ800);
  20. Adafruit_NeoPixel chassis = Adafruit_NeoPixel(NUM_LED_F, PINC, NEO_GRB + NEO_KHZ800);
  21. //Adafruit_NeoPixel front = Adafruit_NeoPixel(NUM_LED_F, PINF, NEO_GRB + NEO_KHZ800);
  22. int iRed = 0;
  23. boolean bRed = true;
  24. uint8_t led_color[NUM_DATA];
  25. int index = 0;
  26. unsigned long last_afk = 0;
  27. unsigned long last_ack = 0;
  28. unsigned long cur_time = 0;
  29.  
  30.  
  31. String inputString = "";         // a string to hold incoming data
  32. boolean stringComplete = false;  // whether the string is complete
  33.  
  34. byte sleep = 255;
  35. byte mode = 0;
  36. byte mmode = 0;
  37. byte power = 1;
  38. byte stay = 0;
  39. void setup() {
  40.     strip.begin();
  41.     chassis.begin();
  42.     //front.begin();
  43.     strip.show(); // Initialize all pixels to 'off'
  44.         chassis.show();
  45.         inputString.reserve(NUM_DATA);
  46.     Serial.begin(115200);
  47.     Serial.print("ozy"); // Send ACK string to host
  48.         strip.setBrightness(100);
  49.         chassis.setBrightness(100);
  50. }
  51.  
  52. void loop() {
  53.   if (stringComplete) {
  54.     //Serial.write('i');
  55.     last_ack = last_afk = millis();
  56.     index = 0;     
  57.     if ((inputString[0] == 'a') && (inputString[1] == 'd') && (stay == 0)){
  58.     // update LEDs
  59.         int led_index = 2;
  60.     for(int i = NUM_LED - 1; i > -1; i--){
  61.       //led_index = i*3 + 2;
  62.       strip.setPixelColor(i, strip.Color(inputString[led_index++], inputString[led_index++], inputString[led_index++]));
  63.         }
  64.         for(int i = NUM_LED_F - 1; i > -1; i--){
  65.       //led_index = i*3 + 2;
  66.       chassis.setPixelColor(i, strip.Color(inputString[182], inputString[183], inputString[184]));
  67.         }
  68.        
  69.         strip.setBrightness(inputString[185]);
  70.         chassis.setBrightness(inputString[186]);
  71.     }
  72.     if ((inputString[0] == 'a') && (inputString[1] == 'm') || (stay == 1)){
  73.     // update LEDs
  74.         int led_index = 2;
  75.     byte r,g,b,n,s;
  76.     power = inputString[led_index++];
  77.     mmode = inputString[led_index++];
  78.         r = inputString[led_index++];
  79.         g = inputString[led_index++];
  80.         b = inputString[led_index++];
  81.         n = inputString[led_index++];
  82.         s = inputString[led_index++];
  83.         stay = inputString[led_index++];
  84.         switch(mmode)
  85.         {
  86.           case 0:
  87.           // Some example procedures showing how to display to the pixels:
  88.           colorWipe(strip.Color(255, 0, 0), 50); // Red
  89.           break;
  90.          
  91.           case 1:
  92.           colorWipe(strip.Color(0, 255, 0), 50); // Green
  93.           break;
  94.          
  95.           case  2:
  96.           colorWipe(strip.Color(0, 0, 255), 50); // Blue
  97.           break;                
  98.          
  99.           case 3:
  100.           // Send a theater pixel chase in...
  101.           theaterChase(strip.Color(127, 127, 127), 50); // White
  102.           break;
  103.          
  104.           case 4:
  105.           theaterChase(strip.Color(127,   0,   0), 50); // Red
  106.           break;
  107.          
  108.           case 5:                
  109.           theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  110.           break;
  111.  
  112.           case 6:
  113.           rainbow(20);
  114.           break;
  115.          
  116.           case 7:
  117.           rainbowCycle(20);
  118.           break;
  119.          
  120.           case 8:
  121.           theaterChaseRainbow(50);
  122.           break;
  123.         }  
  124.         chassis.setPixelColor(0, strip.Color(inputString[182], inputString[183], inputString[184]));
  125.         strip.setBrightness(inputString[185]);
  126.         chassis.setBrightness(inputString[186]);
  127.     }
  128.     if(power == 0)
  129.     {
  130.       strip.setBrightness(0);
  131.       chassis.setBrightness(0);
  132.     }    
  133.     strip.show();
  134.     chassis.show();
  135.     inputString = "";
  136.     stringComplete = false;
  137.     sleep = 255;
  138.   }else{
  139.       cur_time = millis();
  140.         if (cur_time - last_afk > RECON_TIME){
  141.           index = 0;
  142.           inputString = "";
  143.           Serial.write('y');
  144.           //delay(5);
  145.       last_afk = millis();
  146.           if(last_afk - last_ack > 10000)
  147.           {
  148.             if(sleep > 20)
  149.             {
  150.               sleep--;
  151.               strip.setBrightness(sleep);
  152.               chassis.setBrightness(sleep+20);
  153.               strip.show();
  154.               chassis.show();
  155.             }
  156.             if(sleep == 20)
  157.             {
  158.               if(power == 0)
  159.               {
  160.                 strip.setBrightness(0);
  161.                 chassis.setBrightness(0);
  162.               }
  163.               switch(mode++%9)
  164.               {
  165.                 case 0:
  166.                 // Some example procedures showing how to display to the pixels:
  167.                 colorWipe(strip.Color(255, 0, 0), 50); // Red
  168.                 break;
  169.                
  170.                 case 1:
  171.                 colorWipe(strip.Color(0, 255, 0), 50); // Green
  172.                 break;
  173.                
  174.                 case  2:
  175.                 colorWipe(strip.Color(0, 0, 255), 50); // Blue
  176.                 break;                
  177.                
  178.                 case 3:
  179.                 // Send a theater pixel chase in...
  180.                 theaterChase(strip.Color(127, 127, 127), 50); // White
  181.                 break;
  182.                
  183.                 case 4:
  184.                 theaterChase(strip.Color(127,   0,   0), 50); // Red
  185.                 break;
  186.                
  187.                 case 5:                
  188.                 theaterChase(strip.Color(  0,   0, 127), 50); // Blue
  189.                 break;
  190.  
  191.                 case 6:
  192.                 rainbow(20);
  193.                 break;
  194.                
  195.                 case 7:
  196.                 rainbowCycle(20);
  197.                 break;
  198.                
  199.                 case 8:
  200.                 theaterChaseRainbow(50);
  201.                 break;
  202.               }
  203.             }
  204.  
  205.           }
  206.     }
  207.   }
  208. }
  209.  
  210. void serialEvent() {
  211.   while (Serial.available()) {
  212.     // get the new byte:
  213.     //char inChar = (char)Serial.read();
  214.     index++;
  215.     inputString += (char)Serial.read();
  216.     // add it to the inputString:
  217.     //inputString += inChar;
  218.     // if the incoming character is a newline, set a flag
  219.     // so the main loop can do something about it:
  220.     if (index >= NUM_DATA) {
  221.       stringComplete = true;
  222.     }
  223.   }
  224. }
  225.  
  226.  
  227. // Fill the dots one after the other with a color
  228. void colorWipe(uint32_t c, uint8_t wait) {
  229.   for(uint16_t i=0; i<strip.numPixels(); i++) {
  230.       strip.setPixelColor(i, c);
  231.       strip.show();
  232.       chassisIdle();
  233.       delay(wait);
  234.   }
  235. }
  236.  
  237. void rainbow(uint8_t wait) {
  238.   uint16_t i, j;
  239.  
  240.   for(j=0; j<256; j++) {
  241.     for(i=0; i<strip.numPixels(); i++) {
  242.       strip.setPixelColor(i, Wheel((i+j) & 255));
  243.     }
  244.     chassisIdle();
  245.     strip.show();
  246.     delay(wait);
  247.   }
  248. }
  249.  
  250. // Slightly different, this makes the rainbow equally distributed throughout
  251. void rainbowCycle(uint8_t wait) {
  252.   uint16_t i, j;
  253.  
  254.   for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
  255.     for(i=0; i< strip.numPixels(); i++) {
  256.       strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
  257.     }
  258.     chassisIdle();
  259.     strip.show();
  260.     delay(wait);
  261.   }
  262. }
  263.  
  264. //Theatre-style crawling lights.
  265. void theaterChase(uint32_t c, uint8_t wait) {
  266.   for (int j=0; j<10; j++) {  //do 10 cycles of chasing
  267.     for (int q=0; q < 3; q++) {
  268.       for (int i=0; i < strip.numPixels(); i=i+3) {
  269.         strip.setPixelColor(i+q, c);    //turn every third pixel on
  270.       }
  271.       strip.show();
  272.       chassisIdle();    
  273.       delay(wait);
  274.      
  275.       for (int i=0; i < strip.numPixels(); i=i+3) {
  276.         strip.setPixelColor(i+q, 0);        //turn every third pixel off
  277.       }
  278.     }
  279.   }
  280. }
  281.  
  282. //Theatre-style crawling lights with rainbow effect
  283. void theaterChaseRainbow(uint8_t wait) {
  284.   for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
  285.     for (int q=0; q < 3; q++) {
  286.         for (int i=0; i < strip.numPixels(); i=i+3) {
  287.           strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
  288.         }
  289.         strip.show();
  290.         chassisIdle();      
  291.         delay(wait);
  292.        
  293.         for (int i=0; i < strip.numPixels(); i=i+3) {
  294.           strip.setPixelColor(i+q, 0);        //turn every third pixel off
  295.         }
  296.     }
  297.   }
  298. }
  299.  
  300. // Input a value 0 to 255 to get a color value.
  301. // The colours are a transition r - g - b - back to r.
  302. uint32_t Wheel(byte WheelPos) {
  303.   if(WheelPos < 85) {
  304.    return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
  305.   } else if(WheelPos < 170) {
  306.    WheelPos -= 85;
  307.    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  308.   } else {
  309.    WheelPos -= 170;
  310.    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  311.   }
  312. }
  313.  
  314. void chassisIdle()
  315. {
  316. if(iRed > 254)
  317.       bRed = false;
  318.     else if(iRed < 1)
  319.       bRed = true;              
  320.     if(bRed)
  321.     {
  322.       for(int i = NUM_LED_F - 1; i > -1; i--){
  323.       //led_index = i*3 + 2;
  324.             chassis.setPixelColor(i,iRed,0,0);
  325.         }
  326.         iRed++;
  327.     }
  328.     else
  329.     {
  330.       for(int i = NUM_LED_F - 1; i > -1; i--){
  331.       //led_index = i*3 + 2;
  332.             chassis.setPixelColor(i,iRed,0,0);
  333.         }
  334.         iRed--;
  335.     }
  336.      
  337.     chassis.show();
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement