02. fastLed 基本用法

Basic usage 基本用法

Basic usage · FastLED/FastLED Wiki · GitHub

This documentation will walk your through the setup of a FastLED program, as well as provide some information on basic usage of the library, and also provides some basic information on writing code in general. The documentation here assumes a simple setup of a single strand of leds. For more involved uses with multiple strands of leds, or mixing up the types of leds, see the advanced documentation.
本文档将引导您完成 FastLED 程序的设置,并提供有关该库基本用法的一些信息,还提供有关编写代码的一些基本信息。此处的文档假设简单设置单串 LED。有关多串 LED 的更多用途,或混合 LED 类型,请参阅高级文档。

Before you begin 准备工作

Before you begin working on the code, there’s some things that you’ll want to have and know. The first is what LEDs are you using and how many of them will there be? Next is what pin, or pins will you be putting the leds onto? For purposes of our example here, let’s say we’re using Adafruit’s Neopixels, and we have a strip of 60 of them, and we’re going to use pin 6.
在开始编写代码之前,您需要了解和了解一些事情。首先是您使用的是什么 LED,它们会有多少个?接下来是您将把 LED 放在哪个引脚或哪个引脚上?为了说明我们的例子,假设我们使用的是 Adafruit 的 Neopixels,我们有 60 个像素,我们将使用引脚 6。

Setting up the leds 设置 LED

When writing programs for leds, and when writing code in general, I find it quite helpful to use named constants rather than bare numbers for things. What is easier to read and understand what’s going on - FastLED.addLeds<4,6>(leds,60); or FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);? There’s a couple of reasons for doing this. One is, as just shown, it makes code a little bit easier to read. The other is that it means you only have to make changes in one place if you, say, change how many leds you’re working with.
在为 LED 编写程序时,以及在编写一般代码时,我发现使用命名常量而不是裸数字非常有帮助。什么更容易阅读和理解正在发生的事情 - FastLED.addLeds<4,6>(leds,60); 或者 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); ?这样做有几个原因。一个是,如上所述,它使代码更容易阅读。另一个是,这意味着你只需要在一个地方进行更改,比如说,改变你正在使用的LED的数量。

So, with that said, let’s throw in the beginning lines of our .ino file:
因此,话虽如此,让我们加入 .ino 文件的开头行:

#include <FastLED.h>
#define NUM_LEDS 60
#define DATA_PIN 6

So far, what’s been written includes the library, sets a macro definition so that anywhere that NUM_LEDS is seen later in the code gets replaced by 60 and the DATA_PIN is set to 6. Next, we need to set up the block of memory that will be used for storing and manipulating the led data:
到目前为止,所写的内容包括库,设置了一个宏定义,以便代码中稍后看到的任何NUM_LEDS都会被替换为 60,DATA_PIN设置为 6。接下来,我们需要设置用于存储和操作 LED 数据的内存块:

CRGB leds[NUM_LEDS];

This sets up an array that we can manipulate to set/clear led data. Now, let’s actually setup our leds, which is a single line of code in our setup function:
这设置了一个数组,我们可以操纵该数组来设置/清除 LED 数据。现在,让我们实际设置我们的 LED,这是我们设置函数中的一行代码:

void setup() {
  FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS);
}

This tells the library that there’s a strand of NEOPIXEL’s on pin 6 (remember, the value that DATA_PIN was set to), and those leds will use the led array leds, and there are NUM_LEDS (aka 60) of them.
这告诉库,引脚 6 上有一条 NEOPIXEL(记住,DATA_PIN 设置为的值),这些 LED 将使用 LED 阵列 leds ,并且有 NUM_LEDS(又名 60)。

For four wire chipsets you have a couple of options. If you are using the hardware SPI pins for the device that you’re building for, then you don’t even have to specify the pins:
对于四线芯片组,您有几个选择。如果要为要构建的器件使用硬件 SPI 引脚,则甚至不必指定引脚:

    void setup() { 
        FastLED.addLeds<APA102>(leds, NUM_LEDS);
    }

If you are specifying your own pins, you have to specify a clock and a data pin:
如果要指定自己的引脚,则必须指定时钟和数据引脚:

    void setup() { 
      FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN>(leds, NUM_LEDS);
    }

Finally, sometimes you may want to change the data rate that you are running your leds at. In this case, you’ll also need to specify the RGB ordering and the data and clock pins:
最后,有时您可能想要更改运行 LED 的数据速率。在这种情况下,您还需要指定 RGB 排序以及数据和时钟引脚:

void setup() { 
    FastLED.addLeds<APA102, DATA_PIN, CLOCK_PIN, RGB, DATA_RATE_MHZ(12)>(leds, NUM_LEDS);
}

The above example tells the library to run the APA102’s at a 12Mhz data rate instead of the 24Mhz data rate that it will prefer to try for.
上面的示例告诉库以 12Mhz 数据速率运行 APA102,而不是它希望尝试的 24Mhz 数据速率。

Writing an led 编写 LED

Making your leds actually show colors is a two part process with this library. First, you set the values of the entries in the leds array to whatever colors you want. Then you tell the library to show your data. Your animation/code/patterns will pretty much consist of this cycle. Decide what you want everything to display, set it, then tell the led strip to display it. Let’s do something very simple, and set the first led to red:
使用此库,使 LED 实际显示颜色是一个由两部分组成的过程。首先,将 leds 数组中条目的值设置为所需的任何颜色。然后你告诉库显示你的数据。你的动画/代码/图案几乎都包含这个循环。决定您希望所有内容显示的内容,设置它,然后告诉 LED 灯条显示它。让我们做一些非常简单的事情,将第一个 led 设置为红色:

    void loop() { 
        leds[0] = CRGB::Red; 
        FastLED.show(); 
        delay(30); 
    }

That’s pretty simple, isn’t it? Now, you have a program that when you push it out to your arduino will set the first led to red. Over and over again.
这很简单,不是吗?现在,您有一个程序,当您将其推送到您的 arduino 时,它会将第一个 LED 设置为红色。一遍又一遍。

Hmmm, that’s kind of boring. Let’s make it blink!
嗯,这有点无聊。让我们让它眨眼!

Changing an led 更换 LED

You can change the value that you set to an led between calls to show, and the next time you call show the new value will get written out. So, we can set the value of that first led to Red, let it sit there for a second, then set it back to black, let it sit there for a second, and just keep it looping like that, over and over.
您可以在调用之间更改设置为 led 的值以显示,下次调用 show 时,新值将被写出。所以,我们可以将第一个导致的值设置为红色,让它在那里停留一秒钟,然后将其设置回黑色,让它在那里停留一秒钟,然后就这样一遍又一遍地循环。

void loop() {
  // Turn the first led red for 1 second
  leds[0] = CRGB::Red;
  FastLED.show();
  delay(1000);

  // Set the first led back to black for 1 second
  leds[0] = CRGB::Black;
  FastLED.show();
  delay(1000);
}

‘Moving’ an LED “移动”LED

Now the led is blinking. That’s all well and good, but there’s 60 leds in our strip! The other leds are probably feeling neglected. What if we moved an LED dot down the length of the strip? Remember, we have an array of 60 leds, we should be able to do things with them. Here’s a quick thought for having our traveling dot. We want to set the first led to, say, Blue, show the leds, set that led back to Black, delay a little bit, then start this over with the second led. And so on, down the line, until we’ve shown all the leds:
现在 LED 正在闪烁。这一切都很好,但我们的灯带中有 60 个 LED!其他 LED 可能感觉被忽视了。如果我们沿着灯带的长度向下移动一个 LED 点会怎样?请记住,我们有 60 个 LED 阵列,我们应该能够用它们做事。这里有一个关于我们的旅行点的快速想法。我们想将第一个 LED 设置为蓝色,显示 LED,将 LED 设置为黑色,延迟一点,然后从第二个 LED 重新开始。依此类推,直到我们显示所有 LED:

void loop() {
  for (int dot = 0; dot < NUM_LEDS; dot++) {
    leds[dot] = CRGB::Blue;
    FastLED.show();
    // clear this led for the next time around the loop
    leds[dot] = CRGB::Black;
    delay(30);
  }
}

Bringing in external controls 引入外部控件

Let’s do something a little different. Let’s say you have a potentiometer hooked up to your arduino on analog pin 2. That gives a value from 0-1023. What if we used the value from there to decide how many leds to have on? We can use the arduino map function to go from 0-1023 to 0-NUM_LEDS. Here’s what that loop function might look like:
让我们做一些不同的事情。假设您有一个电位计连接到Arduino的模拟引脚 2 上。它给出了一个介于 0-1023 之间的值。如果我们使用那里的值来决定要打开多少个 LED 会怎样?我们可以使用 arduino 映射函数从 0-1023 变为 0-NUM_LEDS。以下是该循环函数的样子:

    void loop() {
        int val = analogRead(2);
        int numLedsToLight = map(val, 0, 1023, 0, NUM_LEDS);

        // First, clear the existing led values
        FastLED.clear();
        for(int led = 0; led < numLedsToLight; led++) { 
            leds[led] = CRGB::Blue; 
        }
        FastLED.show();
    }

Now you have something that will change the number of leds that are on based on what your knob is set to.
现在,您有一些东西可以根据旋钮的设置来更改亮起的 LED 数量。

相关推荐

  1. 02. fastLed 基本

    2024-06-11 20:24:03       23 阅读
  2. Vue3实战笔记(06)--- Axios 基本

    2024-06-11 20:24:03       44 阅读
  3. Tinyxml基本

    2024-06-11 20:24:03       62 阅读
  4. ansible 基本

    2024-06-11 20:24:03       72 阅读
  5. Vue基本

    2024-06-11 20:24:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-11 20:24:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-11 20:24:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-11 20:24:03       87 阅读
  4. Python语言-面向对象

    2024-06-11 20:24:03       96 阅读

热门阅读

  1. angular2网页前端执行流程

    2024-06-11 20:24:03       31 阅读
  2. 制作手机IOS苹果ipa应用的重签名工具

    2024-06-11 20:24:03       30 阅读
  3. golang生成根证书,服务端证书,用于 tls

    2024-06-11 20:24:03       31 阅读
  4. WEB前端三大主流框架

    2024-06-11 20:24:03       33 阅读
  5. Docker面试整理-如何进行Docker镜像的构建和发布?

    2024-06-11 20:24:03       32 阅读
  6. es6基础语法

    2024-06-11 20:24:03       22 阅读
  7. React框架基础教程

    2024-06-11 20:24:03       28 阅读
  8. 电商财务管理---云账户系统

    2024-06-11 20:24:03       29 阅读
  9. C++多线程并发

    2024-06-11 20:24:03       20 阅读