arduino通过自定义协议控制ws2812led全彩彩灯(转)
1、原文地址:https://blog.csdn.net/weixin_46097074/article/details/103719295
-
-
#
-
#
-
#
-
#
-
#
-
-
#
-
#
-
#
-
-
-
char temp;
-
char recvData[1024];
-
int nRecvDataLen;
-
int FlashFlag;
-
char FlashValue;
-
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN_LED1_DI, NEO_GRB + NEO_KHZ800);//8代表有8个小灯
-
//如果一个板子控制多个灯圈,在这新建,组成数组,然后做处理
-
void setup() {
-
// put your setup code here, to run once:
-
Serial.begin(115200); //设置串口波特率115200
-
while (Serial.read() >= 0) {} //清除缓存
-
pinMode(PIN_LED1_DI, OUTPUT);
-
-
memset(recvData, 0, sizeof(recvData));
-
nRecvDataLen = 0;
-
FlashFlag=0;
-
}
-
-
// 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 (int i=0; i < strip.numPixels(); i=i+3) {
-
strip.setPixelColor(i+q, c); //turn every third pixel on
-
}
-
strip.show();
-
-
delay(wait);
-
-
for (int 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 (int 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 (int 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);
-
}
-
-
void FlashControl(char value) {
-
if (value == 'R') {
-
theaterChase(strip.Color(127, 0, 0), 10); // Red
-
} else if (value == 'G') {
-
theaterChase(strip.Color(127, 127, 127), 10);// White
-
} else if (value == 'B') {
-
theaterChase(strip.Color(0, 0, 127), 10); // Blue
-
}else{
-
theaterChaseRainbow(0);//七色轮闪光
-
}
-
}
-
void SetColor(char value) {
-
if (value == 'R') {
-
colorWipe(strip.Color(255, 0, 0), 0);
-
} else if (value == 'G') {
-
colorWipe(strip.Color(0, 255, 0), 0);
-
} else if (value == 'B') {
-
colorWipe(strip.Color(0, 0, 255), 0);
-
}else if (value == '0') {
-
colorWipe(strip.Color(0, 0, 0), 0);
-
}
-
}
-
int processOneCommand()
-
{
-
int nLedNo = recvData[1] - 0x30;
-
char nCmdNo = recvData[2];
-
char cValue = recvData[3];
-
// 看是哪种命令,并调用相应的接口
-
if (nCmdNo == LED_COMMAND_SET_FLASH)
-
{
-
FlashFlag=1;
-
FlashValue=cValue;
-
}
-
else if (nCmdNo == LED_COMMAND_SET_COLOR)
-
{
-
FlashFlag=0;
-
SetColor(cValue);
-
}else if(nCmdNo == LED_COMMAND_STOP_FLASH){
-
FlashFlag=0;
-
SetColor('0');
-
}
-
}
-
void loop() {
-
// put your main code here, to run repeatedly:
-
-
while (Serial.available() > 0)
-
{
-
temp = Serial.read();//获取串口接收到的数据一个一个收
-
if (temp == '$')
-
{
-
nRecvDataLen = 0;
-
}
-
if (nRecvDataLen < 6)
-
{
-
recvData[nRecvDataLen++] = temp;
-
}
-
if (recvData[0] == '$' && recvData[5] == '@')
-
{
-
processOneCommand();
-
memset(recvData, 0, sizeof(recvData));
-
nRecvDataLen = 0; // 已有的数据缓冲区清空
-
}
-
}
-
if(FlashFlag){
-
FlashControl(FlashValue);
-
}
-
-
}
-
- 功能:led灯模块和电脑PC通讯的协议
1、控制灯闪烁
2、设置颜色
协议定义,PC请求和返回结果都通过该协议。共6个字节: $ LEDNO CONTROLNO VALUE CheckSUM @
发送或请求示例:$1AR0@
头部标志:一个字节 :$
LEDNO: 一个字节灯号,0,1,2,3....
CMDNO: 一个字节控制命令号 AB ,A--设置灯闪烁 ,B--设置灯颜色
VALUE:1个字节,颜色值RGB,红R,绿G,蓝B...无色‘0’
CheckSUM:按位相加后取1个字节
结束标志:@
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。