質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

Q&A

解決済

1回答

1298閲覧

arduino(Fast LED)でNeopixcelを特定の色〜特定の色間でグラデーションに光るようにしたい

mkrurk

総合スコア1

Arduino

Arduinoは、AVRマイコン、単純なI/O(入出力)ポートを備えた基板、C言語を元としたArduinoのプログラム言語と、それを実装した統合開発環境から構成されたシステムです。

0グッド

0クリップ

投稿2023/02/05 12:40

実現したいこと

  • arduino(Fast LED)でNeopixcelを特定の色〜特定の色間でグラデーションに光るようにしたい

前提

https://www.reddit.com/r/arduino/comments/o6y4b6/scaling_neopixelfastled_code_examples_by_16/
こちらの質問ページの動画にあるような、徐々にNeopixcelの色が変わるようなプログラムを考えています。
動画の中では虹色に色が変化していますが、虹色ではなくRGB(245, 219, 28)とRGB(245, 202, 37) の間で色を変化させたいです。

発生している問題・エラーメッセージ

サンプルコードをベースに色を変えようと、一日、いろんなサイトを見たのですが変え方が分からず困っています。。初歩的な質問で大変申し訳ありませんがご意見いただけますと嬉しいです。 ### 該当のソースコード ```C♯ /// @file ColorPalette.ino /// @brief Demonstrates how to use @ref ColorPalettes /// @example ColorPalette.ino #include <FastLED.h> #define LED_PIN 5 #define NUM_LEDS 12 #define BRIGHTNESS 64 #define LED_TYPE WS2811 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS]; #define UPDATES_PER_SECOND 100 // This example shows several ways to set up and use 'palettes' of colors // with FastLED. // // These compact palettes provide an easy way to re-colorize your // animation on the fly, quickly, easily, and with low overhead. // // USING palettes is MUCH simpler in practice than in theory, so first just // run this sketch, and watch the pretty lights as you then read through // the code. Although this sketch has eight (or more) different color schemes, // the entire sketch compiles down to about 6.5K on AVR. // // FastLED provides a few pre-configured color palettes, and makes it // extremely easy to make up your own color schemes with palettes. // // Some notes on the more abstract 'theory and practice' of // FastLED compact palettes are at the bottom of this file. CRGBPalette16 currentPalette; TBlendType currentBlending; extern CRGBPalette16 myRedWhiteBluePalette; extern const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM; void setup() { delay( 3000 ); // power-up safety delay FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } void loop() { ChangePalettePeriodically(); static uint8_t startIndex = 0; startIndex = startIndex + 1; /* motion speed */ FillLEDsFromPaletteColors( startIndex); FastLED.show(); FastLED.delay(1000 / UPDATES_PER_SECOND); } void FillLEDsFromPaletteColors( uint8_t colorIndex) { uint8_t brightness = 255; for( int i = 0; i < NUM_LEDS; ++i) { leds[i] = ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending); colorIndex += 3; } } // There are several different palettes of colors demonstrated here. // // FastLED provides several 'preset' palettes: RainbowColors_p, RainbowStripeColors_p, // OceanColors_p, CloudColors_p, LavaColors_p, ForestColors_p, and PartyColors_p. // // Additionally, you can manually define your own color palettes, or you can write // code that creates color palettes on the fly. All are shown here. void ChangePalettePeriodically() { uint8_t secondHand = (millis() / 1000) % 60; static uint8_t lastSecond = 99; if( lastSecond != secondHand) { lastSecond = secondHand; /*if( secondHand == 0) { currentPalette = RainbowColors_p; currentBlending = LINEARBLEND; } if( secondHand == 10) { currentPalette = RainbowStripeColors_p; currentBlending = NOBLEND; } if( secondHand == 15) { currentPalette = RainbowStripeColors_p; currentBlending = LINEARBLEND; } if( secondHand == 20) { SetupPurpleAndGreenPalette(); currentBlending = LINEARBLEND; } if( secondHand == 25) { SetupTotallyRandomPalette(); currentBlending = LINEARBLEND; } if( secondHand == 30) { SetupBlackAndWhiteStripedPalette(); currentBlending = NOBLEND; }*/ if( secondHand == 35) { SetupBlackAndWhiteStripedPalette(); currentBlending = LINEARBLEND; } /*if( secondHand == 40) { currentPalette = CloudColors_p; currentBlending = LINEARBLEND; } if( secondHand == 45) { currentPalette = PartyColors_p; currentBlending = LINEARBLEND; } if( secondHand == 50) { currentPalette = myRedWhiteBluePalette_p; currentBlending = NOBLEND; } if( secondHand == 55) { currentPalette = myRedWhiteBluePalette_p; currentBlending = LINEARBLEND; }*/ } } // This function fills the palette with totally random colors. /*void SetupTotallyRandomPalette() { for( int i = 0; i < 16; ++i) { currentPalette[i] = CHSV( random8(), 255, random8()); } }*/ // This function sets up a palette of black and white stripes, // using code. Since the palette is effectively an array of // sixteen CRGB colors, the various fill_* functions can be used // to set them up. void SetupBlackAndWhiteStripedPalette() { // 'black out' all 16 palette entries... fill_solid( currentPalette, 16, CRGB::Black); // and set every fourth one to white. currentPalette[0] = CRGB::White; currentPalette[4] = CRGB::White; currentPalette[8] = CRGB::White; currentPalette[12] = CRGB::White; } // This function sets up a palette of purple and green stripes. void SetupPurpleAndGreenPalette() { CRGB purple = CHSV( HUE_PURPLE, 255, 255); CRGB green = CHSV( HUE_GREEN, 255, 255); CRGB black = CRGB::Black; currentPalette = CRGBPalette16( green, green, black, black, purple, purple, black, black, green, green, black, black, purple, purple, black, black ); } // This example shows how to set up a static color palette // which is stored in PROGMEM (flash), which is almost always more // plentiful than RAM. A static PROGMEM palette like this // takes up 64 bytes of flash. const TProgmemPalette16 myRedWhiteBluePalette_p PROGMEM = { CRGB::Red, CRGB::Gray, // 'white' is too bright compared to red and blue CRGB::Blue, CRGB::Black, CRGB::Red, CRGB::Gray, CRGB::Blue, CRGB::Black, CRGB::Red, CRGB::Red, CRGB::Gray, CRGB::Gray, CRGB::Blue, CRGB::Blue, CRGB::Black, CRGB::Black }; // Additional notes on FastLED compact palettes: // // Normally, in computer graphics, the palette (or "color lookup table") // has 256 entries, each containing a specific 24-bit RGB color. You can then // index into the color palette using a simple 8-bit (one byte) value. // A 256-entry color palette takes up 768 bytes of RAM, which on Arduino // is quite possibly "too many" bytes. // // FastLED does offer traditional 256-element palettes, for setups that // can afford the 768-byte cost in RAM. // // However, FastLED also offers a compact alternative. FastLED offers // palettes that store 16 distinct entries, but can be accessed AS IF // they actually have 256 entries; this is accomplished by interpolating // between the 16 explicit entries to create fifteen intermediate palette // entries between each pair. // // So for example, if you set the first two explicit entries of a compact // palette to Green (0,255,0) and Blue (0,0,255), and then retrieved // the first sixteen entries from the virtual palette (of 256), you'd get // Green, followed by a smooth gradient from green-to-blue, and then Blue.

補足情報(FW/ツールのバージョンなど)

上記のコードにはこだわっておらず、上記以外でも実現方法がありましたら、ご教示いただけますと嬉しいです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

thkana

2023/02/05 14:53

単純にRGBそれぞれ一次式で補間(今回はR固定みたいですが)じゃだめなんでしょうか?
TakaiY

2023/02/06 02:59

サンプルコードで全てを光らせることはできましたか? サンプルコードでLEDの色を変更することはできましたか? 現在質問にあるコードは難しすぎるので、上記のことができてるのであれば、そのコードに差し替えてください。
mkrurk

2023/02/06 10:38

thkana様、TakaiY様 コメントしてくださり、ありがとうございます。 お二人のアドバイスを踏まえてサンプルコードを使いながら、一次式で補完してみます。
thkana

2023/02/06 14:18

一次補完はmap関数など使うと楽かも知れませんね。
guest

回答1

0

自己解決

みなさま
コメントくださり、ありがとうございました。

以下のコードを書いて解決しました!!!

#include <FastLED.h> #define LED_PIN 6 #define NUM_LEDS 12 #define BRIGHTNESS 40 #define FADE_SPEED 10 CRGB leds[NUM_LEDS]; #define LED_PIN_2 9//LEDが接続されたピン int i2=0; //カウントアップとダウンに使用 void setup() { FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS); pinMode(LED_PIN_2,OUTPUT);// LEDピンの出力に設定 } void loop() { // 2秒間、12個のLEDがRGB(255, 102, 0)、光の強さは120に光る。 for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB(255, 102, 0); leds[i].maximizeBrightness(BRIGHTNESS); } FastLED.show(); delay(2000); // 3秒かけて12個のLEDがRGB(255, 102, 0)からRGB(255, 160, 0)になる、光の強さは40から10になる。 int red = 255; int green = 102; int blue = 0; int brightness = BRIGHTNESS; int fade_steps = 300; // 色変化を1秒にするために、200ステップに分ける for (int step = 0; step < fade_steps; step++) { red = 255; green = 102 + (160 - 102) * (step / (float)fade_steps); blue = 0; brightness = BRIGHTNESS - (BRIGHTNESS - 5) * (step / (float)fade_steps); for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB(red, green, blue); leds[i].maximizeBrightness(brightness); } FastLED.show(); delay(10); // 色変化を1秒にするために、10msごとに表示する } // 1秒かけて12個のLEDが光の強さ5から6になる。 brightness = 5; fade_steps = 200; // 色変化を1秒にするために、200ステップに分ける for (int step = 0; step < fade_steps; step++) { brightness = 5 + (6 - 5) * (step / (float)fade_steps); for (int i = 0; i < NUM_LEDS; i++) { leds[i].maximizeBrightness(brightness); } delay(5); // 色変化を0.5秒にするために、5msごとに表示する } // 3秒かけて12個のLEDがRGB(255, 160, 0)からRGB(255, 102, 0)になる、光の強さは5から40になる。 red = 255; green = 160; blue = 0; brightness = 6; fade_steps = 500; // 色変化を1秒にするために、200ステップに分ける for (int step = 0; step < fade_steps; step++) { red = 255; green = 160 - (160 - 102) * (step / (float)fade_steps); blue = 0; brightness = 6 + (40 - 6) * (step / (float)fade_steps); for (int i = 0; i < NUM_LEDS; i++) { leds[i] = CRGB(red, green, blue); leds[i].maximizeBrightness(brightness); } FastLED.show(); delay(10); // 色変化を1秒にするために、10msごとに表示する } for ( i2 = 0 ; i2 < 255 ; i2++) { analogWrite(LED_PIN_2, i2); } for( i2=255; i2 > 0; i2--) { analogWrite(LED_PIN_2,i2); delay(10) ; } }

投稿2023/02/07 11:23

mkrurk

総合スコア1

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問