Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* one_sine_digital
- *
- * By: Andrew Tuline
- *
- * Date: March, 2021
- *
- * Two methods to move one or more sets of LED's up/down a strand without using delay statements.
- *
- * For CHSV colours, see: https://github.com/FastLED/FastLED/wiki/Pixel-reference
- *
- */
- #include <FastLED.h>
- #define qsubd(x, b) ((x>b)?255:0) // Digital unsigned subtraction macro. if result <0, then => 0. Otherwise, take on fixed value.
- #define LED_DT 12
- #define COLOR_ORDER GRB
- #define LED_TYPE WS2812
- #define NUM_LEDS 30
- struct CRGB leds[NUM_LEDS];
- #define BRIGHTNESS 64
- void setup() {
- Serial.begin(115200);
- LEDS.addLeds<LED_TYPE, LED_DT, COLOR_ORDER>(leds, NUM_LEDS);
- FastLED.setBrightness(BRIGHTNESS);
- } // setup()
- void loop () {
- /*
- EVERY_N_MILLISECONDS(30) { // FastLED based non-blocking delay to update/display the sequence.
- one_sine_digital();
- }
- */
- move_millis();
- FastLED.show();
- } // loop()
- void move_millis() {
- uint8_t totalLights = 12; // Length of a single off/on combination.
- uint8_t onLights = 1; // Number of LED's that are ON.
- uint16_t spdLights = 500; // How fast they're going.
- for (int i=0; i<NUM_LEDS; i++) {
- uint16_t val = millis()/spdLights + i; // Our trusty counter that also includes 'i'.
- bool lit = (val % totalLights) < onLights; // A bitwise & operator with a comparison.
- leds[i] = CHSV(0,255, lit*255);
- bool lita = ((val+2) % totalLights) < onLights; // 2 is the offset from the original animation.
- leds[i] += CHSV(128,255,lita*255);
- }
- } // move_millis()
- void one_sine_digital() { // This is the heart of this program. Sure is short.
- static int thisPhase = 0; // Phase change value gets calculated.
- const int8_t thisSpeed = 8; // You can change the speed of the wave, and use negative values.
- const uint8_t allFreq = 40; // You can change the frequency, thus distance between bars.
- const uint8_t thisCutoff = 248; // You can change the cutoff value to display this wave. Lower value = more led's lit per cycle.
- thisPhase += thisSpeed; // You can change direction and speed individually.
- 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:
- 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)..
- int bBright = qsubd(cubicwave8((k*allFreq)+thisPhase+64), thisCutoff);
- leds[k] = CHSV( 0, 255, aBright); // Then assign a hue to any that are bright enough. Red.
- leds[k] += CHSV( 96, 255, bBright); // Green.
- }
- } // one_sine_digital()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement