SHARE
TWEET

Arduino Hyperion

y1ruma1 Nov 24th, 2015 175 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* t4a_boblight
  2.  * (C) 2014 Hans Luijten, www.tweaking4all.com
  3.  *
  4.  * t4a_boblight is free software and can be distributed and/or modified
  5.  * freely as long as the copyright notice remains in place.
  6.  * Nobody is allowed to charge you for this code.
  7.  * Use of this code is entirely at your own risk.
  8.  */
  9.  
  10. #include "Adafruit_NeoPixel.h"
  11.  
  12. // DEFINITIONS
  13.  
  14. #define STARTCOLOR 0xFFFFFF  // LED colors at start
  15. #define BLACK      0x000000  // LED color BLACK
  16.  
  17. #define DATAPIN    5        // Datapin
  18. #define LEDCOUNT   176       // Number of LEDs used for boblight
  19. #define SHOWDELAY  200       // Delay in micro seconds before showing
  20. #define BAUDRATE   460800    // Serial port speed, 460800 tested with Arduino Uno R3
  21. #define BRIGHTNESS 100        // Max. brightness in %
  22.  
  23. const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0x18, 0x4D};  // Start prefix
  24. char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data
  25.  
  26. // Init LED strand, WS2811/WS2912 specific
  27.  
  28. // These might work for other configurations:
  29. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  30. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  31. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  32. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  33.  
  34. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
  35.  
  36. int state;                   // Define current state
  37. #define STATE_WAITING   1    // - Waiting for prefix
  38. #define STATE_DO_PREFIX 2    // - Processing prefix
  39. #define STATE_DO_DATA   3    // - Handling incoming LED colors
  40.  
  41. int readSerial;           // Read Serial data (1)
  42. int currentLED;           // Needed for assigning the color to the right LED
  43.  
  44. void setup()
  45. {
  46.   strip.begin();            // Init LED strand, set all black, then all to startcolor
  47.  
  48.   strip.setBrightness( (255 / 100) * BRIGHTNESS );
  49.  
  50.   setAllLEDs(BLACK, 0);
  51.   setAllLEDs(STARTCOLOR, 5);
  52.  
  53.   Serial.begin(BAUDRATE);   // Init serial speed
  54.  
  55.   state = STATE_WAITING;    // Initial state: Waiting for prefix
  56. }
  57.  
  58.  
  59. void loop()
  60. {
  61.   switch(state)
  62.   {
  63.     case STATE_WAITING:                  // *** Waiting for prefix ***
  64.       if( Serial.available()>0 )
  65.       {
  66.         readSerial = Serial.read();      // Read one character
  67.        
  68.         if ( readSerial == prefix[0] )   // if this character is 1st prefix char
  69.           { state = STATE_DO_PREFIX; }   // then set state to handle prefix
  70.       }
  71.       break;
  72.      
  73.      
  74.     case STATE_DO_PREFIX:                // *** Processing Prefix ***
  75.       if( Serial.available() > sizeof(prefix) - 2 )
  76.       {
  77.           Serial.readBytes(buffer, sizeof(prefix) - 1);
  78.          
  79.           for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
  80.           {
  81.             if( buffer[Counter] == prefix[Counter+1] )
  82.             {
  83.               state = STATE_DO_DATA;     // Received character is in prefix, continue
  84.               currentLED = 0;            // Set current LED to the first one
  85.             }
  86.             else
  87.             {
  88.               state = STATE_WAITING;     // Crap, one of the received chars is NOT in the prefix
  89.               break;                     // Exit, to go back to waiting for the prefix
  90.             } // end if buffer
  91.           } // end for Counter
  92.       } // end if Serial
  93.       break;
  94.      
  95.      
  96.     case STATE_DO_DATA:                  // *** Process incoming color data ***
  97.       if( Serial.available() > 2 )       // if we receive more than 2 chars
  98.       {
  99.         Serial.readBytes( buffer, 3 );   // Abuse buffer to temp store 3 charaters
  100.         strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]);  // and assing to LEDs
  101.       }
  102.  
  103.       if( currentLED > LEDCOUNT )        // Reached the last LED? Display it!
  104.       {
  105.           strip.show();                  // Make colors visible
  106.           delayMicroseconds(SHOWDELAY);  // Wait a few micro seconds
  107.          
  108.           state = STATE_WAITING;         // Reset to waiting ...
  109.           currentLED = 0;                // and go to LED one
  110.          
  111.           break;                         // and exit ... and do it all over again
  112.       }
  113.       break;
  114.   } // switch(state)
  115.  
  116. } // loop
  117.  
  118.  
  119. // Sets the color of all LEDs in the strand to 'color'
  120. // If 'wait'>0 then it will show a swipe from start to end
  121. void setAllLEDs(uint32_t color, int wait)
  122. {
  123.   for ( int Counter=0; Counter < LEDCOUNT; Counter++ )      // For each LED
  124.   {
  125.     strip.setPixelColor( Counter, color );      // .. set the color
  126.  
  127.     if( wait > 0 )                        // if a wait time was set then
  128.     {
  129.       strip.show();                     // Show the LED color
  130.       delay(wait);                      // and wait before we do the next LED
  131.     } // if wait
  132.    
  133.   } // for Counter
  134.  
  135.   strip.show();                         // Show all LEDs
  136. } // setAllLEDs
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top