Advertisement
atuline

one_sine_digital.ino

Mar 26th, 2021 (edited)
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.31 KB | None | 0 0
  1. /* one_sine_digital
  2.  *
  3.  * By: Andrew Tuline
  4.  *
  5.  * Date: March, 2021
  6.  *
  7.  * Two methods to move one or more sets of LED's up/down a strand without using delay statements.
  8.  *
  9.  * For CHSV colours, see: https://github.com/FastLED/FastLED/wiki/Pixel-reference
  10.  *
  11.  */
  12.  
  13.  
  14. #include <FastLED.h>
  15.  
  16. #define qsubd(x, b)  ((x>b)?255:0)                     // Digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
  17.  
  18.  
  19. #define LED_DT 12
  20. #define COLOR_ORDER GRB
  21. #define LED_TYPE WS2812
  22. #define NUM_LEDS 30
  23.  
  24. struct CRGB leds[NUM_LEDS];
  25.  
  26. #define BRIGHTNESS 64
  27.  
  28.  
  29.  
  30. void setup() {
  31.   Serial.begin(115200);
  32.   LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
  33.   FastLED.setBrightness(BRIGHTNESS);
  34. } // setup()
  35.  
  36.  
  37.  
  38. void loop () {
  39.  
  40. /*  
  41.   EVERY_N_MILLISECONDS(30) {                           // FastLED based non-blocking delay to update/display the sequence.
  42.     one_sine_digital();
  43.   }
  44. */
  45.  
  46.   move_millis();
  47.  
  48.   FastLED.show();  
  49. } // loop()
  50.  
  51.  
  52.  
  53. void move_millis() {
  54.  
  55.   uint8_t totalLights = 12;                                           // Length of a single off/on combination.
  56.   uint8_t onLights = 1;                                               // Number of LED's that are ON.
  57.   uint16_t spdLights = 500;                                           // How fast they're going.
  58.  
  59.   for (int i=0; i<NUM_LEDS; i++) {
  60.    
  61.     uint16_t val = millis()/spdLights + i;                            // Our trusty counter that also includes 'i'.
  62.  
  63.     bool lit = (val % totalLights) < onLights;                        // A bitwise & operator with a comparison.
  64.     leds[i] = CHSV(0,255, lit*255);
  65.    
  66.     bool lita = ((val+2) % totalLights) < onLights;                   // 2 is the offset from the original animation.
  67.     leds[i] += CHSV(128,255,lita*255);
  68.   }
  69.  
  70. } // move_millis()
  71.  
  72.  
  73.  
  74. void one_sine_digital() {                                             // This is the heart of this program. Sure is short.
  75.  
  76.   static int thisPhase = 0;                                           // Phase change value gets calculated.
  77.  
  78.   const int8_t thisSpeed = 8;                                         // You can change the speed of the wave, and use negative values.
  79.   const uint8_t allFreq = 40;                                         // You can change the frequency, thus distance between bars.
  80.   const uint8_t thisCutoff = 248;                                     // You can change the cutoff value to display this wave. Lower value = more led's lit per cycle.
  81.  
  82.   thisPhase += thisSpeed;                                                     // You can change direction and speed individually.
  83.  
  84.   for (int k=0; k<NUM_LEDS; k++) {                                          // For each of the LED's in the strand, set a brightness based on a wave as follows:
  85.     int aBright = qsubd(cubicwave8((k*allFreq)+thisPhase), thisCutoff);    // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
  86.     int bBright = qsubd(cubicwave8((k*allFreq)+thisPhase+64), thisCutoff);
  87.    
  88.     leds[k]  = CHSV(  0, 255, aBright);                                    // Then assign a hue to any that are bright enough. Red.
  89.     leds[k] += CHSV( 96, 255, bBright);                                    // Green.
  90.   }
  91.  
  92. } // one_sine_digital()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement