Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!




Test topic
 
Share:
Notifications
Clear all

Test topic

1 Posts
1 Users
0 Likes
62 Views
 Hans
(@hans)
Famed Member Admin
Joined: 10 years ago
Posts: 2547
Topic starter  

Code that seems to crash the forum:

#include <FastLED.h>

#define NUM_LEDS 48
#define buttonPin1 5
#define buttonPin2 3

CRGB leds[NUM_LEDS];

void setup() {
  FastLED.addLeds < NEOPIXEL, PB1 > (leds, NUM_LEDS);
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);

}

void loop() {

  if (digitalRead(buttonPin1) == LOW) {

    CRGB pattern[] = {
      CRGB(255, 255, 255),
      CRGB(255, 255, 255),
      CRGB(255, 0, 0),
      CRGB(255, 0, 0),

    };
    size_t pattern_sz = sizeof(pattern) / sizeof( * pattern);

    // a 8.8 fixed point position (so 256 = 1.0, 512 = 2.0, 65535 = 255.996)
    static uint16_t basepos = 0;
    // a copy of basepos to be modified over the length of the strip
    uint16_t ledpos = basepos;
    for (uint8_t ledno = 0; ledno < NUM_LEDS; ledno++) {
      // find the integer and fractional parts for this LED 
      uint8_t intpos = ledpos >> 8;
      uint8_t fracpos = ledpos & 0xff;
      // select the two colours to be blended for this LED
      CRGB colA = pattern[intpos % pattern_sz];
      CRGB colB = pattern[(intpos + 1) % pattern_sz];
      // fade colA towards colB by the fractional position
      leds[ledno] = fadeTowardColor(colA, colB, fracpos);
      // move along the source pattern by some fraction of a pixel
      ledpos -= 128;
    }
    FastLED.show();
    delay(10);
    // move the base position by some fractional amount
    basepos += beatsin8(5, 1, 128);

  }

  if (digitalRead(buttonPin2) == LOW) {

    CRGB pattern[] = {
      CRGB(255, 255, 255),
      CRGB(255, 255, 255),
      CRGB(0, 0, 255),
      CRGB(0, 0, 255),

    };
    size_t pattern_sz = sizeof(pattern) / sizeof( * pattern);

    // a 8.8 fixed point position (so 256 = 1.0, 512 = 2.0, 65535 = 255.996)
    static uint16_t basepos = 0;
    // a copy of basepos to be modified over the length of the strip
    uint16_t ledpos = basepos;
    for (uint8_t ledno = 0; ledno < NUM_LEDS; ledno++) {
      // find the integer and fractional parts for this LED 
      uint8_t intpos = ledpos >> 8;
      uint8_t fracpos = ledpos & 0xff;
      // select the two colours to be blended for this LED
      CRGB colA = pattern[intpos % pattern_sz];
      CRGB colB = pattern[(intpos + 1) % pattern_sz];
      // fade colA towards colB by the fractional position
      leds[ledno] = fadeTowardColor(colA, colB, fracpos);
      // move along the source pattern by some fraction of a pixel
      ledpos -= 128;
    }
    FastLED.show();
    delay(10);
    // move the base position by some fractional amount
    basepos += beatsin8(5, 1, 128);
  }

  if (digitalRead(buttonPin1 == LOW) && (buttonPin2 == LOW)) {

    CRGB pattern[] = {
      CRGB(255, 255, 255),
      CRGB(255, 255, 255),
      CRGB(0, 255, 0),
      CRGB(0, 255, 0),

    };
    size_t pattern_sz = sizeof(pattern) / sizeof( * pattern);

    // a 8.8 fixed point position (so 256 = 1.0, 512 = 2.0, 65535 = 255.996)
    static uint16_t basepos = 0;
    // a copy of basepos to be modified over the length of the strip
    uint16_t ledpos = basepos;
    for (uint8_t ledno = 0; ledno < NUM_LEDS; ledno++) {
      // find the integer and fractional parts for this LED 
      uint8_t intpos = ledpos >> 8;
      uint8_t fracpos = ledpos & 0xff;
      // select the two colours to be blended for this LED
      CRGB colA = pattern[intpos % pattern_sz];
      CRGB colB = pattern[(intpos + 1) % pattern_sz];
      // fade colA towards colB by the fractional position
      leds[ledno] = fadeTowardColor(colA, colB, fracpos);
      // move along the source pattern by some fraction of a pixel
      ledpos -= 128;
    }
    FastLED.show();
    delay(10);
    // move the base position by some fractional amount
    basepos += beatsin8(5, 1, 128);
  }
}

// Mark Kriegsman's fadeTowardColor from:
//  https://gist.github.com/kriegsman/d0a5ed3c8f38c64adcb4837dafb6e690 

// Helper function that blends one uint8_t toward another by a given amount
void nblendU8TowardU8(uint8_t & cur,
  const uint8_t target, uint8_t amount) {
  if (cur == target) return;

  if (cur < target) {
    uint8_t delta = target - cur;
    delta = scale8_video(delta, amount);
    cur += delta;
  } else {
    uint8_t delta = cur - target;
    delta = scale8_video(delta, amount);
    cur -= delta;
  }
}

// Blend one CRGB color toward another CRGB color by a given amount.
// Blending is linear, and done in the RGB color space.
// This function modifies 'cur' in place.
CRGB fadeTowardColor(CRGB & cur,
  const CRGB & target, uint8_t amount) {
  nblendU8TowardU8(cur.red, target.red, amount);
  nblendU8TowardU8(cur.green, target.green, amount);
  nblendU8TowardU8(cur.blue, target.blue, amount);
  return cur;
}

 

This topic was modified 2 weeks ago 2 times by Hans

   
ReplyQuote

Like what you see and you'd like to help out? 

The best way to help is of course by assisting others with their questions here in the forum, but you can also help us out in other ways:

- Do your shopping at Amazon, it will not cost you anything extra but may generate a small commission for us,
- send a cup of coffee through PayPal ($5, $10, $20, or custom amount),
- become a Patreon,
- donate BitCoin (BTC), or BitCoinCash (BCH).

Share: