1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NeoPixel をよりエモくピカらせる

Last updated at Posted at 2019-09-28

みなさんはネオピクセル使ってますよね?
ビカビカっ⚡️⚡️とピカらせればテンションMAX🤩ですもんね。
そんなハイテンション⤴️⤴️高速フラッシュ✨✨もいいですが、
たまにはエモくホワーっと光らせてみませんか?

ネオピクセル使っているみなさんは当然
#include <Adafruit_NeoPixel.h>
とかやってますよね?で、
``Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, SIG_PIN);```
とかやってらっしゃる。ですよね?

もし、「そう・・・だったかな・・・?」という人は
スケッチ>ライブラリをインクルード>ライブラリを管理...
からAdafruit_NeoPixelをインストールしていただくとよいです。

で、ネオピクライブラリは内部で
_NeoPixelSineTable[256]
という表を持っていて、

trip.sine8(n); // n:0-255
みたいに使えるんです。グラフにするとこんな感じ。

スクリーンショット 2019-09-28 7.23.30.png

つまり0-255,0-255,...でフワフワっと明滅させることができるんです。
128から始まっているのはSinテーブルだからですかね?
NeoPixelSineTable[256]のSineはシャイン?サインe?シne?
まあなんでもいいです。
ただ消灯状態からフワっと最大輝度まで明るくするためには

for(int i=0;i<128;++i){
  n=(i+192)&0xff;
   :
}

みたいにn=192からはじめて63で終わる必要があります。

千里の道もコピペから↑↑↑↑⛰⛰⛰⛰⛰
ということで、とりあえずフワフワっと色が変わっていくサンプルをアゲておきますね。

https://youtu.be/7uGSeAORE7M

# include <Adafruit_NeoPixel.h>
# define SIG_PIN (13)
# define NUM_LEDS (24)
# define BRIGHTNESS (32) // 0-255

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, SIG_PIN);

unsigned int ptrR=0;
unsigned int ptrG=0;
unsigned int ptrB=0;

void setup() {
  Serial.begin(115200);
  pinMode(SIG_PIN, OUTPUT);  
  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.clear();
}

void loop() {
  ptrR = (ptrR+1)&0xff;
  ptrG = (ptrG+2)&0xff;
  ptrB = (ptrB+3)&0xff;
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    unsigned int valR = strip.sine8((ptrR+i)&0xff);
    unsigned int valG = strip.sine8((ptrG+i)&0xff);
    unsigned int valB = strip.sine8((ptrB+i)&0xff);
    uint32_t c = strip.Color(valR,valG,valB);
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(10);
}

#define NUM_LEDS (24)
は使っているネオピクセルのLED数に合わせて変更してください。
#define BRIGHTNESS (32) // 0-255
は最大輝度です。明るさマックスより暗めの方がエモいっしょ。

ptrR = (ptrR+1)&0xff;
とかの+1とかを変えるとフワフワの速度を変えられるのでお好みで。

いや、もっと、フワ・・・フワ・・・と したいんや!という人は
シャイン?サインe?シne?テーブルを補間して究極のフワフワ感を。

# include <Adafruit_NeoPixel.h>
# define SIG_PIN (13)
# define NUM_LEDS (24)
# define BRIGHTNESS (32) // 0-255

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, SIG_PIN);

unsigned int ptrR=0;
unsigned int ptrG=0;
unsigned int ptrB=0;

void setup() {
  Serial.begin(115200);
  pinMode(SIG_PIN, OUTPUT);  
  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.clear();
}

void loop() {
  ptrR = (ptrR+11)%(1<<10);
  ptrG = (ptrG+12)%(2<<10);
  ptrB = (ptrB+13)%(4<<10);
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    unsigned int ofsv = (1<<12)*i/strip.numPixels();
    unsigned int dR = (((ptrR+ofsv)%(1<<10)));
    unsigned int dG = (((ptrG+ofsv)%(1<<10)));
    unsigned int dB = (((ptrB+ofsv)%(1<<10)));
    unsigned int valR = getSinVal(dR);
    unsigned int valG = getSinVal(dG);
    unsigned int valB = getSinVal(dB);
    uint32_t c = strip.Color(valR,valG,valB);
    //uint32_t c = strip.Color(valR,0,0);
    strip.setPixelColor(i, c);
  }
  strip.show();
  delay(10);
}

int getSinVal(unsigned int _d1024){
    unsigned int r = (_d1024>>2); // 実数部 0-255
    unsigned int p = (_d1024&0x04); // 小数部 0-3
    unsigned int val = ((strip.sine8(r)*(3-p)+strip.sine8((r+1)%256)*p)/3)&0xff;
    val = min(val,255);
    return val;
}

動画
2019-09-30_160558.png

1
3
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?