arduino neo 定位不可用_arduino霹雳七彩灯

本文介绍了如何使用arduino控制WS2812单总线LED灯,包括其数据手册、电气参数和连接方式。通过示例展示了三种不同的显示效果,并提供了arduino接线图及库的下载和添加方法。
摘要由CSDN通过智能技术生成

2595a2685ce0d98db6c8c0229e287e0c.png

我们首先来看下视频效果:

知乎视频​www.zhihu.com
zhihu-card-default.svg

开始之前我们先简单科普下WS2812这种带芯片单总线LED灯

172cab6c7872aff82b8b07495137f9d4.png
WS2812灯带

数据手册:

zhimg_answer_editor_file_pdf.svg
WS2812_5050RGB.PDF
282.5K
·
百度网盘

aab71088db69dc65073cbd116a38d783.png
按照30fps的刷新率最高可以级联1024个点,实际800个以内没问题

外观尺寸和引脚定义:

b0dba09e80de5928b00267c20e6d3e23.png

LED灯使用环境、电气性能参数以及电流消耗情况:

电压范围:3.5~5.3V

电流:每个灯最大亮度下电流都是20mA

工作温度范围:-25~+80℃

cf5f44a9369c54b16a8d5df784ab7156.png

dbfbe9f56818307c35a156bd7818ae09.png

实际使用中的电路连接方式 DI与DO首尾相接:

6f5f2b1b8df565e76f56443b752629c0.png

56777e78c64be5a04dc445cd0f3d38ef.png

arduino接线示意图:

cf9acc827a17c7658c50bfe564503434.png

代码:

#include <Adafruit_NeoPixel.h>

#ifdef __AVR__
  #include <avr/power.h>
#endif

#define PIN 6

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel.  Avoid connecting
// on a live circuit...if you must, connect GND first.

void theaterChase(uint32_t c, uint8_t wait);
void colorWipe(uint32_t c, uint8_t wait);
void rainbow(uint8_t wait);
void rainbowCycle(uint8_t wait);
void theaterChase(uint32_t c, uint8_t wait);
void theaterChaseRainbow(uint8_t wait);
uint32_t Wheel(byte WheelPos);

void setup() {
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
  #if defined (__AVR_ATtiny85__)
    if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
  #endif
  // End of trinket special code


  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
/*  strip.setPixelColor(0,255,255,255);
  strip.show();
  delay(200);
  strip.setPixelColor(0,0,0, 0);
  strip.show();
  delay(2000);*/


  //rainbow(20);
  //rainbowCycle(20);
  theaterChaseRainbow(50);
}

// Fill the dots one after the other with a color
void colorWipe(uint32_t c, uint8_t wait) {
  for(uint16_t i=0; i<strip.numPixels(); i++) {
    strip.setPixelColor(i, c);
    strip.show();
    delay(wait);
  }
}

void rainbow(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256; j++) {
    for(i=0; i<strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel((i+j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

// Slightly different, this makes the rainbow equally distributed throughout
void rainbowCycle(uint8_t wait) {
  uint16_t i, j;

  for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
    for(i=0; i< strip.numPixels(); i++) {
      strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
    }
    strip.show();
    delay(wait);
  }
}

//Theatre-style crawling lights.
void theaterChase(uint32_t c, uint8_t wait) {
  for (int j=0; j<10; j++) {  //do 10 cycles of chasing
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, c);    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

//Theatre-style crawling lights with rainbow effect
void theaterChaseRainbow(uint8_t wait) {
  for (int j=0; j < 256; j++) {     // cycle all 256 colors in the wheel
    for (int q=0; q < 3; q++) {
      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, Wheel( (i+j) % 255));    //turn every third pixel on
      }
      strip.show();

      delay(wait);

      for (uint16_t i=0; i < strip.numPixels(); i=i+3) {
        strip.setPixelColor(i+q, 0);        //turn every third pixel off
      }
    }
  }
}

// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
  WheelPos = 255 - WheelPos;
  if(WheelPos < 85) {
    return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
  }
  if(WheelPos < 170) {
    WheelPos -= 85;
    return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
  }
  WheelPos -= 170;
  return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}

我这里用的是RT-Thread studio,需要先添加“Adafruit_NeoPixel”库

库下载地址:adafruit/Adafruit_NeoPixel

b1604130f2808bddbdf8c9fd28f59351.gif

下载下来后直接将里面的文件复制到你的PlatformIO库目录下就好了

c453c72c982e7d08c2ac5250d7a5a74e.gif

5a9b0f3c8eb60c12f104a8c103b4cc99.png

我这里是这个目录:C:UsersAdministrator.platformiolib


这里面有三种显示效果,我们一一来操作下:

第一个效果:

c9f4af7d45ab40bcbca7b213f48fef2b.png

b0ad4537941fcc5d32651442637bcc3c.gif

第二个效果:

21a7fc2fb819bd4909454f1964489787.png

1ffbdacdc968abe797b94e68770bb236.gif

第三个效果:

8eecf6238fd0c95ea421ce4c64c2789d.png

fe2899826a151b943d3cb42c02ba2fa2.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值