Advertisement
marmil

lighthouse_beacon_v2_anti-aliased

Nov 17th, 2015
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. //***************************************************************
  2. // rotating beacon for lighthouse emulation, Anti-aliased version
  3. // Marc Miller, Nov 2015
  4. //
  5. //Based on Mark Kriegsman's Anti-aliased light bar example here:
  6. //http://pastebin.com/yAgKs0Ay#
  7. //***************************************************************
  8.  
  9. #include "FastLED.h"
  10. #define LED_TYPE NEOPIXEL
  11. #define DATA_PIN 6
  12. //#define CLOCK_PIN 13
  13. #define NUM_LEDS 12
  14. //#define COLOR_ORDER GRB
  15. #define BRIGHTNESS 255
  16. CRGB leds[NUM_LEDS];
  17.  
  18. int8_t hue = 42;  // Light color
  19. int8_t sat = 190;  // Saturation
  20. int8_t val = BRIGHTNESS;  // Max brightness of light
  21. uint8_t pixelPos = 0; // position of the "fraction-based bar"
  22. int delta = -1; // Number of 16ths of a pixel to move.  (Use negative value for reverse.)
  23. uint8_t width  = 3; // width of light in pixels
  24. uint16_t holdTime = 80;  // Milliseconds to hold between microsteps.
  25. int8_t fadeRate = 40;  // Fade ends a bit.
  26.  
  27.  
  28. //---------------------------------------------------------------
  29. void setup() {
  30.   //FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  31.   FastLED.addLeds<LED_TYPE, DATA_PIN>(leds, NUM_LEDS);
  32.   FastLED.setBrightness(BRIGHTNESS);
  33.   //Serial.begin(115200);
  34. }
  35.  
  36.  
  37. //---------------------------------------------------------------
  38. // Fractional bar funtion
  39. void drawFractionalBar( uint8_t pos16, uint8_t width, uint8_t hue)
  40. {
  41.   uint8_t i = pos16 / 16; // convert from pos to raw pixel number
  42.   uint8_t frac = pos16 & 0x0F; // extract the 'factional' part of the position
  43.   uint8_t firstpixelbrightness = 255 - (frac * 16);
  44.   uint8_t lastpixelbrightness  = 255 - firstpixelbrightness;
  45.   uint8_t bright;
  46.   for( uint8_t n = 0; n <= width; n++) {
  47.     if( n == 0) {
  48.       bright = firstpixelbrightness;
  49.       leds[i] += CHSV( hue, sat, bright);
  50.       //fadeToBlackBy( leds, NUM_LEDS, fadeRate );  // creates outward fade
  51.     }
  52.     else if (n == width) {
  53.       bright = lastpixelbrightness;
  54.       leds[i] += CHSV( hue, sat, bright);
  55.       fadeToBlackBy( leds, NUM_LEDS, fadeRate );  // creates outward fade
  56.     }
  57.     else {
  58.       bright = 255;  // center pixel full bright
  59.       leds[i] += CHSV( hue, sat, bright);
  60.     }
  61.     i++;
  62.     if( i == NUM_LEDS) i = 0; // wrap around
  63.   }
  64. }
  65.  
  66. //---------------------------------------------------------------
  67. void loop() {  // START MAIN LOOP
  68.   if (delta > 0) {
  69.     pixelPos += delta;
  70.   } else {
  71.     pixelPos = (pixelPos + delta + (NUM_LEDS *16)) % (NUM_LEDS * 16);
  72.   }
  73.  
  74.   if( pixelPos >= (NUM_LEDS * 16)) {
  75.     pixelPos -= (NUM_LEDS * 16);
  76.   }
  77.  
  78.   static byte countdown = 0;
  79.   if( countdown == 0) { countdown = 16; } // reset countdown
  80.   countdown -= 1;  // decrement once each loop though
  81.  
  82.   memset8( leds, 0, NUM_LEDS * sizeof(CRGB));  // Clear the pixel buffer
  83.   drawFractionalBar( pixelPos, width, hue);  // Draw the pixels
  84.   FastLED.show();  // Show the pixels
  85.   delay(holdTime);  // Delay a bit before micro advancing
  86.  
  87. }  // END MAIN LOOP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement