arduino通过自定义协议控制ws2812led全彩彩灯(转)


 

      1、原文地址:https://blog.csdn.net/weixin_46097074/article/details/103719295

      1. #include <Adafruit_NeoPixel.h>
      2.   # ifdef __AVR__
      3.   # include <avr/power.h>
      4.   # endif
      5.   # define PIN_LED1_DI 1 // led1 di口
      6.   # define PIN_LED2_DI 2 // led2 di口
      7.   
      8.   # define LED_COMMAND_SET_FLASH 'A' // 设置灯闪烁
      9. # define LED_COMMAND_STOP_FLASH 'B' // 停止灯闪烁
      10.   # define LED_COMMAND_SET_COLOR 'C' // 设置灯颜色
      11.   
      12.   
      13.   char temp;
      14.   char recvData[ 1024];
      15.   int nRecvDataLen;
      16.   int FlashFlag;
      17.   char FlashValue;
      18.   Adafruit_NeoPixel strip = Adafruit_NeoPixel( 8, PIN_LED1_DI, NEO_GRB + NEO_KHZ800);//8代表有8个小灯
      19.   //如果一个板子控制多个灯圈,在这新建,组成数组,然后做处理
      20.   void setup() {
      21.   // put your setup code here, to run once:
      22.   Serial.begin( 115200); //设置串口波特率115200
      23.   while (Serial.read() >= 0) {} //清除缓存
      24.   pinMode(PIN_LED1_DI, OUTPUT);
      25.   
      26.   memset(recvData, 0, sizeof(recvData));
      27.   nRecvDataLen = 0;
      28.   FlashFlag= 0;
      29.   }
      30.   
      31.   // Fill the dots one after the other with a color
      32.   void colorWipe(uint32_t c, uint8_t wait) {
      33.   for( uint16_t i=0; i<strip.numPixels(); i++) {
      34.   strip.setPixelColor(i, c);
      35.   strip.show();
      36.   delay(wait);
      37.   }
      38.   }
      39.   
      40.   void rainbow(uint8_t wait) {
      41.   uint16_t i, j;
      42.   
      43.   for(j= 0; j<256; j++) {
      44.   for(i= 0; i<strip.numPixels(); i++) {
      45.   strip.setPixelColor(i, Wheel((i+j) & 255));
      46.   }
      47.   strip.show();
      48.   delay(wait);
      49.   }
      50.   }
      51.   
      52.   // Slightly different, this makes the rainbow equally distributed throughout
      53.   void rainbowCycle(uint8_t wait) {
      54.   uint16_t i, j;
      55.   
      56.   for(j= 0; j<256*5; j++) { // 5 cycles of all colors on wheel
      57.   for(i= 0; i< strip.numPixels(); i++) {
      58.   strip.setPixelColor(i, Wheel(((i * 256 / strip.numPixels()) + j) & 255));
      59.   }
      60.   strip.show();
      61.   delay(wait);
      62.   }
      63.   }
      64.   
      65.   //Theatre-style crawling lights.
      66.   void theaterChase(uint32_t c, uint8_t wait) {
      67.   for ( int j=0; j<10; j++) { //do 10 cycles of chasing
      68.   for ( int q=0; q < 3; q++) {
      69.   for ( int i=0; i < strip.numPixels(); i=i+3) {
      70.   strip.setPixelColor(i+q, c); //turn every third pixel on
      71.   }
      72.   strip.show();
      73.   
      74.   delay(wait);
      75.   
      76.   for ( int i=0; i < strip.numPixels(); i=i+3) {
      77.   strip.setPixelColor(i+q, 0); //turn every third pixel off
      78.   }
      79.   }
      80.   }
      81.   }
      82.   
      83.   //Theatre-style crawling lights with rainbow effect
      84.   void theaterChaseRainbow(uint8_t wait) {
      85.   for ( int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
      86.   for ( int q=0; q < 3; q++) {
      87.   for ( int i=0; i < strip.numPixels(); i=i+3) {
      88.   strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
      89.   }
      90.   strip.show();
      91.   
      92.   delay(wait);
      93.   
      94.   for ( int i=0; i < strip.numPixels(); i=i+3) {
      95.   strip.setPixelColor(i+q, 0); //turn every third pixel off
      96.   }
      97.   }
      98.   }
      99.   }
      100.   
      101.   // Input a value 0 to 255 to get a color value.
      102.   // The colours are a transition r - g - b - back to r.
      103.   uint32_t Wheel(byte WheelPos) {
      104.   WheelPos = 255 - WheelPos;
      105.   if(WheelPos < 85) {
      106.   return strip.Color( 255 - WheelPos * 3, 0, WheelPos * 3);
      107.   }
      108.   if(WheelPos < 170) {
      109. WheelPos -= 85;
      110. return strip.Color( 0, WheelPos * 3, 255 - WheelPos * 3);
      111.   }
      112.   WheelPos -= 170;
      113.   return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
      114.   }
      115.   
      116.   void FlashControl(char value) {
      117.   if (value == 'R') {
      118.   theaterChase(strip.Color( 127, 0, 0), 10); // Red
      119.   } else if (value == 'G') {
      120.   theaterChase(strip.Color( 127, 127, 127), 10);// White
      121.   } else if (value == 'B') {
      122.   theaterChase(strip.Color( 0, 0, 127), 10); // Blue
      123. } else{
      124.   theaterChaseRainbow( 0);//七色轮闪光
      125. }
      126.   }
      127.   void SetColor(char value) {
      128.   if (value == 'R') {
      129.   colorWipe(strip.Color( 255, 0, 0), 0);
      130.   } else if (value == 'G') {
      131.   colorWipe(strip.Color( 0, 255, 0), 0);
      132.   } else if (value == 'B') {
      133.   colorWipe(strip.Color( 0, 0, 255), 0);
      134.   } else if (value == '0') {
      135.   colorWipe(strip.Color( 0, 0, 0), 0);
      136.   }
      137.   int processOneCommand()
      138.   {
      139.   int nLedNo = recvData[ 1] - 0x30;
      140.   char nCmdNo = recvData[ 2];
      141.   char cValue = recvData[ 3];
      142.   // 看是哪种命令,并调用相应的接口
      143.   if (nCmdNo == LED_COMMAND_SET_FLASH)
      144.   {
      145.   FlashFlag= 1;
      146.   FlashValue=cValue;
      147.   }
      148.   else if (nCmdNo == LED_COMMAND_SET_COLOR)
      149.   {
      150.   FlashFlag= 0;
      151.   SetColor(cValue);
      152.   } else if(nCmdNo == LED_COMMAND_STOP_FLASH){
      153.   FlashFlag= 0;
      154.   SetColor( '0');
      155.    
      156.   }
      157.   void loop() {
      158.   // put your main code here, to run repeatedly:
      159.  
      160.   while (Serial.available() > 0)
      161.   {
      162. temp = Serial.read(); //获取串口接收到的数据一个一个收
      163.   if (temp == '$')
      164.   {
      165.   nRecvDataLen = 0;
      166.   }
      167.   if (nRecvDataLen < 6)
      168.   {
      169.   recvData[nRecvDataLen++] = temp;
      170.   }
      171.   if (recvData[ 0] == '$' && recvData[5] == '@')
      172.   {
      173.   processOneCommand();
      174.   memset(recvData, 0, sizeof(recvData));
      175.   nRecvDataLen = 0; // 已有的数据缓冲区清空
      176.   }
      177.   }
      178.   if(FlashFlag){
      179.   FlashControl(FlashValue);
      180.   }
      181.   
      182.   }
    1. 功能: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个字节
        结束标志:@


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM