pdf-icon

Arduino 上手教程

2. 设备开发 & 案例程序

Fire RGB LED

Fire RGB LED案例程序。本案例基于Adafruit NeoPixel库实现, 使用前请通过库管理安装Adafruit NeoPixel依赖库。

案例程序

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <M5Unified.h>
#include <Adafruit_NeoPixel.h>

#define LED_PIN    15
#define NUM_LEDS   10

Adafruit_NeoPixel strip(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  auto cfg = M5.config();
  M5.begin(cfg);
  M5.Display.setTextDatum(middle_center);
  M5.Display.setTextFont(&fonts::Orbitron_Light_24);
  M5.Display.setTextSize(1);
  M5.Display.drawString("RGB LED Test", M5.Display.width() / 2, M5.Display.height() / 2);
  strip.begin();
  strip.show(); 
}

void loop() {
    //红色
    for(char i = 0; i <= NUM_LEDS; i++)
    {strip.setPixelColor(i, strip.Color(255, 0, 0)); }    
    strip.show();
    delay(1000);
  
    //绿色
    for(char i = 0; i <= NUM_LEDS; i++)
    {strip.setPixelColor(i, strip.Color(0, 255, 0)); }
    strip.show();
    delay(1000);
  
    //蓝色
    for(char i = 0; i <= NUM_LEDS; i++)
    {strip.setPixelColor(i, strip.Color(0, 0, 255)); }
    strip.show();
    delay(1000);

}  

例程效果如下:

On This Page