标题:
用Adafruit_NeoPixel-1.3.4的全彩灯条 Arduino源程序
[打印本页]
作者:
hai911
时间:
2020-3-10 14:13
标题:
用Adafruit_NeoPixel-1.3.4的全彩灯条 Arduino源程序
经实物实验60颗全部点亮。多种显示方式。
Arduino源程序如下:
/*
strip.setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
第一个参数n是彩带中LED的编号,最接近单片机引脚的编号为0;接下来的三个参数描述像素颜色,分别表示红色,绿色和蓝色的亮度级别,0为最暗,255是最大亮度;
strip.setPixelColor(uint16_t n, uint32_t c);
n是彩带中LED的编号,颜色color是一种32位类型,将红色,绿色和蓝色值合并为一个数字,有时这样做能提高程序的效率.通过下面的方法,可以将红色,绿色和蓝色值转换为32位类型。
uint32_t magenta = strip.Color(red, green, blue);
strip.setBrightness(uint8_t);
一般只在setup()中调用,以保证在整个程序执行过程中LED颜色亮度的一致性,其实在程序中通过合适的逻辑控制各像素的亮度值可能动画效果更好.
strip.show();
该方法更新彩带上的全部LED。一个好的习惯是先利用setPixelColor()设置好整个彩带的颜色,然后再调用show()方法,以防止出现动画跳跃而不平滑。
*/
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
//int pinInterrupt = 2; //接中断信号的脚
//void onChange()
//{
// if ( digitalRead(pinInterrupt) == LOW )
// Serial.println("Key Down");
// else
// Serial.println("Key UP");
//}
/******************************************key
#define BUTTON 3
int val=0;
int old_val=0;
int state=0;
****************************************/
#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:参数 3 = 像素类型标志,根据需要添加在一起:
// 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.
// This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
void setup()
{
#if defined (__AVR_ATtiny85__)
if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
// End of trinket special code
// Serial.begin(9600); //打开串口
// pinMode( pinInterrupt, INPUT);//设置管脚为输入
//Enable中断管脚, 中断服务程序为onChange(), 监视引脚变化
//attachInterrupt( digitalPinToInterrupt(pinInterrupt), onChange, CHANGE);
// pinMode(BUTTON,INPUT);//key
strip.begin();
strip.show(); // Initialize all pixels to 'off'
}
void loop()
{
/****************************************** key
val = digitalRead(BUTTON);
if((val==HIGH)&&(old_val==LOW))
{
state=1-state;
delay(10);
}
old_val = val;
if(state==1)
{
********************************************/
// Some example procedures showing how to display to the pixels:显示如何显示到像素的一些示例过程:
colorWipe(strip.Color(255, 0, 0), 50); // Red delay50
colorWipe(strip.Color(0, 255, 0), 50); // Green
colorWipe(strip.Color(0, 0, 225), 50); // Blue
colorWipe(strip.Color(255, 0, 255), 50); // purple
colorWipe(strip.Color(128, 0, 175), 50); // purple
colorWipe(strip.Color(0, 128, 255), 50);
colorWipe(strip.Color(0, 255, 255), 50);
//colorWipe(strip.Color(0, 0, 0, 255), 50); // White RGBW
// Send a theater pixel chase in...发送剧院像素追逐.
theaterChase(strip.Color(127, 127, 127), 50); // White
theaterChase(strip.Color(255, 0, 0), 50); // Red
theaterChase(strip.Color(0, 0, 255), 50); // Blue剧院追逐
theaterChase(strip.Color(0, 255, 0), 50);
theaterChase(strip.Color(255, 0, 255), 50);
theaterChase(strip.Color(255, 255, 0), 50);
theaterChase(strip.Color(0, 255, 255), 50);
rainbow(20);
rainbowCycle(20);
theaterChaseRainbow(50);
rainbowCycle1(20);
rainbowCycle2(20);
/************************************************** key
}
else
{
colorWipe(strip.Color(0, 255, 0), 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);
}
}
void rainbowCycle1(uint8_t wait)
{
uint16_t i, j;
for(j=256*5; j>=0; 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);
}
}
void rainbowCycle2(uint8_t wait)
{
uint16_t i, j;
for(j=256*5; j>=0; j--) // 5 cycles of all colors on wheel
{
for(i=strip.numPixels(); i>=0; 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);
}
复制代码
所有资料51hei提供下载:
sketch_RGB.rar
(2.96 KB, 下载次数: 37)
2020-3-10 14:13 上传
点击文件名下载附件
RGB全彩灯
下载积分: 黑币 -5
作者:
joyist
时间:
2021-12-18 13:42
为什么要回复需要审核,请等待通过
作者:
哮河谷
时间:
2022-2-12 13:11
资源不好用
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1