SHARE
TWEET

Twinkle mic working

pchestna Apr 26th, 2015 28 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "FastLED/FastLED.h"
  2. FASTLED_USING_NAMESPACE;
  3.  
  4. #define bitRead(value, bit) (((value) >> (bit)) & 0x01)
  5. #define bitSet(value, bit) ((value) |= (1UL << (bit)))
  6. #define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
  7. #define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
  8.  
  9. #define LED_PIN     D0
  10. #define LED_TYPE    WS2811
  11. #define COLOR_ORDER GRB
  12. #define NUM_LEDS    512
  13. CRGB leds[NUM_LEDS];
  14.  
  15. #define MICROPHONE 12
  16. #define GAIN_CONTROL 11
  17.  
  18. void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount);
  19. CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter);
  20. CRGB makeDarker( const CRGB& color, fract8 howMuchDarker);
  21.  
  22. //  Twinkling 'holiday' lights that fade up and down in brightness.
  23. //  Colors are chosen from a palette; a few palettes are provided.
  24. //
  25. //  The basic operation is that all pixels stay black until they
  26. //  are 'seeded' with a relatively dim color.  The dim colors
  27. //  are repeatedly brightened until they reach full brightness, then
  28. //  are darkened repeatedly until they are fully black again.
  29. //
  30. //  A set of 'directionFlags' is used to track whether a given
  31. //  pixel is presently brightening up or darkening down.
  32. //
  33. //  For illustration purposes, two implementations of directionFlags
  34. //  are provided: a simple one-byte-per-pixel flag, and a more
  35. //  complicated, more compact one-BIT-per-pixel flag.
  36. //
  37. //  Darkening colors accurately is relatively easy: scale down the
  38. //  existing color channel values.  Brightening colors is a bit more
  39. //  error prone, as there's some loss of precision.  If your colors
  40. //  aren't coming our 'right' at full brightness, try increasing the
  41. //  STARTING_BRIGHTNESS value.
  42. //
  43. //  -Mark Kriegsman, December 2014
  44.  
  45. #define MASTER_BRIGHTNESS   200
  46.  
  47. #define STARTING_BRIGHTNESS 64
  48. #define FADE_IN_SPEED       32
  49. #define FADE_OUT_SPEED      60
  50. #define DENSITY            150
  51.  
  52. void setup() {
  53.   delay(3000);
  54.   FastLED.addLeds<LED_TYPE,LED_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  55.   FastLED.setBrightness(MASTER_BRIGHTNESS);
  56.   initMicrophone();
  57. //  analogReference(DEFAULT);
  58. //  Serial.begin(57600);
  59. }
  60.  
  61. void initMicrophone()
  62. {
  63.   pinMode(GAIN_CONTROL, OUTPUT);
  64.   digitalWrite(GAIN_CONTROL, LOW);
  65. }
  66.  
  67. CRGBPalette16 gPalette;
  68.  
  69. int readMaxSound( uint8_t ms)
  70. {
  71.   uint32_t startms = millis();
  72.  
  73.   while( millis() == startms) {};
  74.  
  75.   startms = millis();
  76.  
  77.   int maxvol = 0;
  78.   while( (startms + ms) > millis()) {
  79. //    int mic = analogRead(MICROPHONE)-512;
  80.     int mic = analogRead(MICROPHONE);
  81.     if( mic < 0) mic = 0-mic;
  82.     if( mic > maxvol) maxvol = mic;
  83.   }
  84.   return maxvol;
  85. }
  86.  
  87. void loop()
  88. {
  89.   chooseColorPalette();
  90.   colortwinkles();
  91. //  delay(2);
  92.   int mic = readMaxSound(10);
  93.   mic /= 2;
  94.   mic = dim8_raw( mic);
  95.   mic = dim8_raw( mic);
  96.  
  97. //  Serial.println(mic);
  98.  
  99.  
  100.   static int avgmic = 0;
  101.   avgmic = ((avgmic * 3) + mic) / 4;
  102.  
  103.   mic = avgmic;
  104.  
  105. //  delay(2);
  106.   if( mic > 0) {
  107.     for( int j = 0; j < mic; j++) {
  108.       leds[random16(NUM_LEDS)] = ColorFromPalette( gPalette, random8());
  109.     }
  110.   }
  111.  
  112.   FastLED.show();  
  113. //  FastLED.delay(1);
  114. }
  115.  
  116.  
  117. void chooseColorPalette()
  118. {
  119.   uint8_t numberOfPalettes = 9;
  120.   uint8_t secondsPerPalette = 60;
  121.   uint8_t whichPalette = (millis()/(1000*secondsPerPalette)) % numberOfPalettes;
  122.  
  123.   CRGB r(CRGB::Red), b(CRGB::Blue), w(85,85,85), g(CRGB::Green), W(CRGB::White), l(CRGB::FairyLight);
  124.  
  125. whichPalette = 8;
  126.   switch( whichPalette) {  
  127.     case 0: // Red, Green, and White
  128.       gPalette = CRGBPalette16( r,r,r,r, r,r,r,r, g,g,g,g, w,w,w,w );
  129.       break;
  130.     case 1: // Blue and White
  131.       //gPalette = CRGBPalette16( b,b,b,b, b,b,b,b, w,w,w,w, w,w,w,w );
  132.       gPalette = CloudColors_p; // Blues and whites!
  133.       break;
  134.     case 2: // Rainbow of colors
  135.       gPalette = RainbowColors_p;
  136.       break;
  137.     case 3: // Incandescent "fairy lights"
  138.       gPalette = CRGBPalette16( l,l,l,l, l,l,l,l, l,l,l,l, l,l,l,l );
  139.       break;
  140.     case 4: // Snow
  141.       gPalette = CRGBPalette16( W,W,W,W, w,w,w,w, w,w,w,w, w,w,w,w );
  142.       break;
  143.     case 5:
  144.       gPalette = LavaColors_p;
  145.       break;
  146.     case 6:
  147.       gPalette = ForestColors_p;
  148.       break;
  149.     case 7:
  150.       gPalette = PartyColors_p;
  151.       break;
  152.     case 8:
  153.       uint8_t h = beatsin8(1);
  154.       gPalette = CHSVPalette16( CHSV(h,255,255), CHSV(h+32, 255,255));
  155.       break;
  156.   }
  157. }
  158.  
  159. enum { GETTING_DARKER = 0, GETTING_BRIGHTER = 1 };
  160.  
  161. void colortwinkles()
  162. {
  163.   // Make each pixel brighter or darker, depending on
  164.   // its 'direction' flag.
  165.   brightenOrDarkenEachPixel( FADE_IN_SPEED, FADE_OUT_SPEED);
  166.  
  167.   // Now consider adding a new random twinkle
  168.   if( random8() < DENSITY ) {
  169.     int pos = random16(NUM_LEDS);
  170.     if( leds[pos] ) pos = random16(NUM_LEDS);
  171.     if( !leds[pos]) {
  172.       leds[pos] = ColorFromPalette( gPalette, random8(), STARTING_BRIGHTNESS, NOBLEND);
  173.       setPixelDirection(pos, GETTING_BRIGHTER);
  174.     }
  175.   }
  176. }
  177.  
  178. void brightenOrDarkenEachPixel( fract8 fadeUpAmount, fract8 fadeDownAmount)
  179. {
  180. for( uint16_t i = 0; i < NUM_LEDS; i++) {
  181.     if( getPixelDirection(i) == GETTING_DARKER) {
  182.       // This pixel is getting darker
  183.       leds[i] = makeDarker( leds[i], fadeDownAmount);
  184.     } else {
  185.       // This pixel is getting brighter
  186.       leds[i] = makeBrighter( leds[i], fadeUpAmount);
  187.       // now check to see if we've maxxed out the brightness
  188.       if( leds[i].r == 255 || leds[i].g == 255 || leds[i].b == 255) {
  189.         // if so, turn around and start getting darker
  190.         setPixelDirection(i, GETTING_DARKER);
  191.       }
  192.     }
  193.   }
  194. }
  195.  
  196. CRGB makeBrighter( const CRGB& color, fract8 howMuchBrighter)
  197. {
  198.   CRGB incrementalColor = color;
  199.   incrementalColor.nscale8( howMuchBrighter);
  200.   return color + incrementalColor;
  201. }
  202.  
  203. CRGB makeDarker( const CRGB& color, fract8 howMuchDarker)
  204. {
  205.   CRGB newcolor = color;
  206.   newcolor.nscale8( 255 - howMuchDarker);
  207.   return newcolor;
  208. }
  209.  
  210.  
  211. // For illustration purposes, there are two separate implementations
  212. // provided here for the array of 'directionFlags':
  213. // - a simple one, which uses one byte (8 bits) of RAM for each pixel, and
  214. // - a compact one, which uses just one BIT of RAM for each pixel.
  215.  
  216. // Set this to 1 or 8 to select which implementation
  217. // of directionFlags is used.  1=more compact, 8=simpler.
  218. #define BITS_PER_DIRECTION_FLAG 1
  219.  
  220.  
  221. #if BITS_PER_DIRECTION_FLAG == 8
  222. // Simple implementation of the directionFlags array,
  223. // which takes up one byte (eight bits) per pixel.
  224. uint8_t directionFlags[NUM_LEDS];
  225.  
  226. bool getPixelDirection( uint16_t i) {
  227.   return directionFlags[i];
  228. }
  229.  
  230. void setPixelDirection( uint16_t i, bool dir) {
  231.   directionFlags[i] = dir;
  232. }
  233. #endif
  234.  
  235.  
  236. #if BITS_PER_DIRECTION_FLAG == 1
  237. // Compact (but more complicated) implementation of
  238. // the directionFlags array, using just one BIT of RAM
  239. // per pixel.  This requires a bunch of bit wrangling,
  240. // but conserves precious RAM.  The cost is a few
  241. // cycles and about 100 bytes of flash program memory.
  242. uint8_t  directionFlags[ (NUM_LEDS+7) / 8];
  243.  
  244. bool getPixelDirection( uint16_t i) {
  245.   uint16_t index = i / 8;
  246.   uint8_t  bitNum = i & 0x07;
  247.   // using Arduino 'bitRead' function; expanded code below
  248.   return bitRead( directionFlags[index], bitNum);
  249.   // uint8_t  andMask = 1 << bitNum;
  250.   // return (directionFlags[index] & andMask) != 0;
  251. }
  252.  
  253. void setPixelDirection( uint16_t i, bool dir) {
  254.   uint16_t index = i / 8;
  255.   uint8_t  bitNum = i & 0x07;
  256.   // using Arduino 'bitWrite' function; expanded code below
  257.   bitWrite( directionFlags[index], bitNum, dir);
  258.   //  uint8_t  orMask = 1 << bitNum;
  259.   //  uint8_t andMask = 255 - orMask;
  260.   //  uint8_t value = directionFlags[index] & andMask;
  261.   //  if( dir ) {
  262.   //    value += orMask;
  263.   //  }
  264.   //  directionFlags[index] = value;
  265. }
  266. #endif
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