DigitMagazine

Arduino Ambilight Sketch

Apr 24th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.73 KB | None | 0 0
  1. /* Modified and commented by ai.rs
  2. * t4a_boblight
  3. * (C) 2014 Hans Luijten, www.tweaking4all.com
  4. *
  5. * t4a_boblight is free software and can be distributed and/or modified
  6. * freely as long as the copyright notice remains in place.
  7. * Nobody is allowed to charge you for this code.
  8. * Use of this code is entirely at your own risk.
  9. */
  10.  
  11. #include "Adafruit_NeoPixel.h"
  12.  
  13. // DEFINITIONS
  14.  
  15. #define STARTCOLOR 0x333333  // LED colors at start
  16. #define BLACK      0x000000  // LED color BLACK
  17.  
  18. #define DATAPIN    13        // Datapin
  19. #define LEDCOUNT   300       //  Number of LEDs used for boblight
  20. // LEDCOUNT value is local value in Arduino sketch, for hyperion it doesn't matter it sends prefx characters according to hyperion config
  21. #define SHOWDELAY  200       // Delay in micro seconds before showing default 200
  22. //#define BAUDRATE   500000// Serial port speed, 460800 tested with Arduino Uno R3 23400 za MEGA, 115200 nano
  23. #define BAUDRATE   115200
  24.  
  25. #define BRIGHTNESS 40        // Max. brightness in %
  26. //Hyperion sends prefix characters based on number of LEDs in config file
  27. // e.g. for 181 LEDs it will send 0xB4 and cheksum 0xE1
  28. // keep in mind if you are using boblight config to calculate prefix that Boblight counts diodes from 1 and Hyperion from 0
  29. // if you have problems try +1 or -1 diodes when generating prefix characters
  30. // values to save some time: 178 B1 E4, 180 B3E6, 181 B4E1, 182 B5E0
  31. //hyperion code
  32. //_ledBuffer[3] = ((ledValues.size() - 1) >> 8) & 0xFF; // LED count high byte
  33. //    _ledBuffer[4] = (ledValues.size() - 1) & 0xFF;        // LED count low byte
  34. //    _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
  35. const char prefix[] = {0x41, 0x64, 0x61, 0x00, 0xB4, 0xE1};  // Start prefix ADA
  36. char buffer[sizeof(prefix)]; // Temp buffer for receiving prefix data
  37.  
  38. // Init LED strand, WS2811/WS2912 specific
  39.  
  40. // These might work for other configurations:
  41. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  42. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  43. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  44. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  45.  
  46. Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDCOUNT, DATAPIN, NEO_GRB + NEO_KHZ800);
  47.  
  48. int state;                   // Define current state
  49. #define STATE_WAITING   1    // - Waiting for prefix
  50. #define STATE_DO_PREFIX 2    // - Processing prefix
  51. #define STATE_DO_DATA   3    // - Handling incoming LED colors
  52.  
  53. int readSerial;           // Read Serial data (1)
  54. int currentLED;           // Needed for assigning the color to the right LED
  55.  
  56. void setup()
  57. {
  58.   strip.begin();            // Init LED strand, set all black, then all to startcolor
  59.  
  60.   strip.setBrightness( (255 / 100) * BRIGHTNESS );
  61.  
  62.   setAllLEDs(BLACK, 0);
  63.   setAllLEDs(STARTCOLOR, 5);
  64.  
  65.   Serial.begin(BAUDRATE);   // Init serial speed
  66.  
  67.   state = STATE_WAITING;    // Initial state: Waiting for prefix
  68. }
  69.  
  70.  
  71. void loop()
  72. {
  73.   switch(state)
  74.   {
  75.     case STATE_WAITING:                  // *** Waiting for prefix ***
  76.       if( Serial.available()>0 )
  77.       {
  78.         readSerial = Serial.read();      // Read one character
  79.        
  80.         if ( readSerial == prefix[0] )   // if this character is 1st prefix char
  81.           { state = STATE_DO_PREFIX; }   // then set state to handle prefix
  82.       }
  83.       break;
  84.      
  85.      
  86.     case STATE_DO_PREFIX:                // *** Processing Prefix ***
  87.       if( Serial.available() > sizeof(prefix) - 2 )
  88.       {
  89.           Serial.readBytes(buffer, sizeof(prefix) - 1);
  90.          
  91.           for( int Counter = 0; Counter < sizeof(prefix) - 1; Counter++)
  92.           {
  93.             if( buffer[Counter] == prefix[Counter+1] )
  94.             {
  95.               state = STATE_DO_DATA;     // Received character is in prefix, continue
  96.               currentLED = 0;            // Set current LED to the first one
  97.             }
  98.             else
  99.             {
  100.               state = STATE_WAITING;     // Crap, one of the received chars is NOT in the prefix
  101.               break;                     // Exit, to go back to waiting for the prefix
  102.             } // end if buffer
  103.           } // end for Counter
  104.       } // end if Serial
  105.       break;
  106.      
  107.      
  108.     case STATE_DO_DATA:                  // *** Process incoming color data ***
  109.       if( Serial.available() > 2 )       // if we receive more than 2 chars
  110.       {
  111.         Serial.readBytes( buffer, 3 );   // Abuse buffer to temp store 3 charaters
  112.         strip.setPixelColor( currentLED++, buffer[0], buffer[1], buffer[2]);  // and assing to LEDs
  113.       }
  114.  
  115.       if( currentLED > LEDCOUNT )        // Reached the last LED? Display it!
  116.       {
  117.           strip.show();                  // Make colors visible
  118.           delayMicroseconds(SHOWDELAY);  // Wait a few micro seconds
  119.          
  120.           state = STATE_WAITING;         // Reset to waiting ...
  121.           currentLED = 0;                // and go to LED one
  122.          
  123.           break;                         // and exit ... and do it all over again
  124.       }
  125.       break;
  126.   } // switch(state)
  127.  
  128. } // loop
  129.  
  130.  
  131. // Sets the color of all LEDs in the strand to 'color'
  132. // If 'wait'>0 then it will show a swipe from start to end
  133. void setAllLEDs(uint32_t color, int wait)
  134. {
  135.   for ( int Counter=0; Counter < LEDCOUNT; Counter++ )      // For each LED
  136.   {
  137.     strip.setPixelColor( Counter, color );      // .. set the color
  138.  
  139.     if( wait > 0 )                        // if a wait time was set then
  140.     {
  141.       strip.show();                     // Show the LED color
  142.       delay(wait);                      // and wait before we do the next LED
  143.     } // if wait
  144.    
  145.   } // for Counter
  146.  
  147.   strip.show();                         // Show all LEDs
  148. } // setAllLEDs
Add Comment
Please, Sign In to add comment