LoginSignup
1
3

More than 5 years have passed since last update.

1/fゆらぎでフルカラーLチカ

Last updated at Posted at 2018-06-13

1/fゆらぎをフルカラーLEDに使ってLチカする

LEDをチカチカさせるLチカ。 応用形でロウソクのような不規則なちらつきを表現する1/fがよく作られているようです。 そのまま真似しても面白くないのでフルカラーLEDを使ってみた。

環境

  • Mac OS High Sierra 10.13.4
  • Arduino IDE 1.8.5
  • Digispark互換品
  • マイコンWS2811内蔵フルカラーLED PL9823-F8 (秋月電子の例)

動作

1fFluctuation.gif

回路図

配線3本しかありませんが。
DIGISPARK-WS2811.jpeg

ソースコード

  1. 0.05以下や0.995以上になると揺らぎにくくなるらしいので近辺でランダムに。
  2. RGBそれぞれ1/f揺らぐだけだと明る過ぎて面白くないので全体の明るさも揺らがせた。
#include <Adafruit_NeoPixel.h>

#define PIN 0       //信号端子のピン番号

Adafruit_NeoPixel ledtape = Adafruit_NeoPixel(LED_NUM, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  ledtape.begin();
  ledtape.show();   //一旦全てOFFの状態で反映
}

int blue;
float redRatio = 0.8;
float greenRatio = 0.8;
int delayMsec = 100;
int minimum = 20;
float fIntensity = random(10, 90) / 100.0;;
float fRed       = random(10, 90) / 100.0;;
float fGreen     = random(10, 90) / 100.0;;
float fBlue      = random(10, 90) / 100.0;;

void loop() {
  fIntensity = f1Fluctuation(fIntensity);
  fRed       = f1Fluctuation(fRed);
  fGreen     = f1Fluctuation(fGreen);
  fBlue      = f1Fluctuation(fBlue);
  int intensity = 255 * fIntensity;
  light(fRed * intensity, fGreen * intensity, fBlue * intensity);
  delay(delayMsec);
}

void light( int red, int green, int blue ) {
  int number = 0;
  ledtape.setPixelColor(number, red, green, blue);
  ledtape.show();
}

float f1Fluctuation(float x){
  if( x < 0.5 ){
    x = x + 2 * x * x;
  } else {
    x = x - 2 * (1.0 - x) * (1.0 - x);
  }
  if( x < 0.05 ){
    x = random( 5, 10) / 100.0;
  }
  if( x > 0.995 ){
    x = random(90, 99) / 100.0;
  }
  return x;
}

参考

  1. http://satotoshio.net/blog/?p=1348
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3