marmil

beat8_plus_random_freeze.ino

Mar 14th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. //***************************************************************
  2. // beat8 example with random freeze experiment for Erin.
  3. // Open the serial monitor to see the random times.
  4. //
  5. // -Marc Miller, March 2017
  6. //***************************************************************
  7.  
  8. #include "FastLED.h"
  9. #define LED_TYPE LPD8806
  10. #define DATA_PIN 11
  11. #define CLOCK_PIN 13
  12. #define NUM_LEDS 32
  13. #define COLOR_ORDER GRB
  14. #define BRIGHTNESS 128
  15. CRGB leds[NUM_LEDS];
  16.  
  17. uint8_t moveSpeed = 6;  // Higher value moves pixel faster.
  18. uint8_t fadeRate = 30;  // Use lower value to give a fading tail.
  19. const uint16_t startDelay = 5000;  // Number of milliseconds before re-start
  20. int analogSeedPin = 20;  // Any un-used analog pin.  Used for generating seed for random16.
  21. int analogSeedPinB = 21;  // Any un-used analog pin.  Used for generating seed for random16.
  22.  
  23.  
  24. uint8_t gHue = 0;  // Used to cycle through rainbow.
  25. unsigned long timeDelay = startDelay;
  26. unsigned long freezeTime = 10000000;  // Just something really large to prevent triggering on startup
  27.  
  28.  
  29. //---------------------------------------------------------------
  30. void setup()
  31. {
  32.   Serial.begin(115200);  // Allows serial monitor output (check baud rate)
  33.   delay(3000);  // 3 second delay
  34.   FastLED.addLeds<LED_TYPE, DATA_PIN, CLOCK_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  35.   FastLED.setBrightness(BRIGHTNESS);
  36. }
  37.  
  38.  
  39. //---------------------------------------------------------------
  40. void loop()
  41. {
  42.   EVERY_N_MILLISECONDS( 50 ) { gHue++; }  // Slowly cycle through the rainbow
  43.   static boolean frozen = true;  // [Initially set true!]
  44.  
  45.   if (frozen) {  // [FROZEN]
  46.     EVERY_N_MILLISECONDS(500){
  47.       static boolean getReady = false;
  48.       getReady = !getReady;
  49.       if (getReady) {
  50.         leds[0] = CHSV(42,255,255);
  51.       } else {
  52.         leds[0] = CRGB::Black;
  53.       }
  54.     }
  55.  
  56.     // This if statement delays the start of the moving pixel.
  57.     // It could be changed so it would be true after a "start" button was been pressed.
  58.     if (millis() > timeDelay) {
  59.       frozen = false;  // toggle frozen variable
  60.       uint16_t seed = random16_get_seed();
  61.       seed = seed + analogRead(analogSeedPin) + analogRead(analogSeedPinB);  // create a random seed
  62.       random16_add_entropy(seed);
  63.       seed = random16_get_seed();
  64.       freezeTime = random16(3000,23000);  // random number of milliseconds before freezing
  65.       Serial.print("freezeTime = "); Serial.print(freezeTime/1000.0); Serial.println(" seconds.");
  66.       freezeTime = millis() + freezeTime;
  67.     }
  68.   }//end_of_FROZEN
  69.  
  70.  
  71.   if (!frozen) {  // [IS NOT FROZEN]
  72.     beat8_tail();  // Subroutine to move the pixel!
  73.   }
  74.  
  75.   FastLED.show();  // Display the pixels.
  76.  
  77.  
  78.   // Check to see if the random freeze time has been reached.
  79.   // If true then stop the animation.
  80.   if (millis() > freezeTime){
  81.     frozen = true;  // toggle frozen variable
  82.     timeDelay = millis() + startDelay;  // set new start time
  83.     freezeTime = millis() + 1000000;  // To prevent re-triggering
  84.   }
  85.  
  86. }//end main loop
  87.  
  88.  
  89. //===============================================================
  90. void beat8_tail()
  91. {
  92.   EVERY_N_MILLISECONDS( 5 ) {
  93.     fadeToBlackBy( leds, NUM_LEDS, fadeRate);  // Fade out pixels.
  94.   }
  95.   uint16_t pos = beat8(moveSpeed) % NUM_LEDS;  // modulo the position to be within NUM_LEDS
  96.   leds[pos] = CHSV( gHue, 200, 255);
  97. }
Add Comment
Please, Sign In to add comment