8007浏览
查看: 8007|回复: 14

[进阶] Bluno控制灯带

[复制链接]
最近到手一套蓝牙炫彩LED灯带套件,听起来就酷的不行。。。正直双蛋期间,可是我木有圣诞树做装饰,暂且只能玩玩灯带,还望各位看官忍受一下
1、套件清单
  • BLUNO控制器        1个
  • 模拟声音传感器     1个
  • 音频分析模块       1个
  • 3米WS2812灯带      1条
  • 传感器扩展板V7     1个
  • 电源转接头         1个
  • 5V@3.6A电源适配器  1个
Bluno控制灯带图3Bluno控制灯带图2Bluno控制灯带图1Bluno控制灯带图6

2、连接


Bluno和扩展板连接
Bluno控制灯带图9

电源转接头连接
Bluno控制灯带图8Bluno控制灯带图4
传感器连接
Bluno控制灯带图10

整体连接起来
Bluno控制灯带图7

3、编程控制(用的官网代码示例)


first sample,不用蓝牙模块,也不需要传感器,效果如下图
Bluno控制灯带图5

代码
  1. /*
  2. This code can be a good test code for your strip
  3. function: leds.setPixelColor(i,y) can be used to let number i of your led turn on with color of y
  4. and you can draw your idea easily with this function but never forget function: led.show()
  5. */
  6. #include <Adafruit_NeoPixel.h>
  7. #define PIN 6     //The signal pin connected with Arduino   
  8. #define LED_COUNT 180 // the amount of the leds of your strip
  9. // Create an instance of the Adafruit_NeoPixel class called "leds".
  10. // That'll be what we refer to from here on...
  11. Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);
  12. void setup()
  13. {
  14.   leds.begin();  // Call this to start up the LED strip.
  15.   clearLEDs();   // This function, defined below, turns all LEDs off...
  16.   leds.show();   // ...but the LEDs don't actually update until you call this.
  17. }
  18. void loop()
  19. {
  20.     for (int i=0; i<LED_COUNT; i++)
  21.   {
  22.     rainbow(i);
  23.     delay(50);  // Delay between rainbow slides
  24.   }
  25. }
  26.    
  27. // Sets all LEDs to off, but DOES NOT update the display;
  28. // call leds.show() to actually turn them off after this.
  29. void clearLEDs()
  30. {
  31.   for (int i=0; i<LED_COUNT; i++)
  32.   {
  33.     leds.setPixelColor(i, 0);
  34.   }
  35. }
  36. // Prints a rainbow on the ENTIRE LED strip.
  37. //  The rainbow begins at a specified position.
  38. // ROY G BIV!
  39. void rainbow(byte startPosition)
  40. {
  41.   // Need to scale our rainbow. We want a variety of colors, even if there
  42.   // are just 10 or so pixels.
  43.   int rainbowScale = 192 / LED_COUNT;
  44.    
  45.   // Next we setup each pixel with the right color
  46.   for (int i=0; i<LED_COUNT; i++)
  47.   {
  48.     // There are 192 total colors we can get out of the rainbowOrder function.
  49.     // It'll return a color between red->orange->green->...->violet for 0-191.
  50.     leds.setPixelColor(i, rainbowOrder((rainbowScale * (i + startPosition)) % 192));
  51.   }
  52.   // Finally, actually turn the LEDs on:
  53.   leds.show();
  54. }
  55. // Input a value 0 to 191 to get a color value.
  56. // The colors are a transition red->yellow->green->aqua->blue->fuchsia->red...
  57. //  Adapted from Wheel function in the Adafruit_NeoPixel library example sketch
  58. uint32_t rainbowOrder(byte position)
  59. {
  60.   // 6 total zones of color change:
  61.   if (position < 31)  // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
  62.   {
  63.     return leds.Color(0xFF, position * 8, 0);
  64.   }
  65.   else if (position < 63)  // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
  66.   {
  67.     position -= 31;
  68.     return leds.Color(0xFF - position * 8, 0xFF, 0);
  69.   }
  70.   else if (position < 95)  // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
  71.   {
  72.     position -= 63;
  73.     return leds.Color(0, 0xFF, position * 8);
  74.   }
  75.   else if (position < 127)  // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
  76.   {
  77.     position -= 95;
  78.     return leds.Color(0, 0xFF - position * 8, 0xFF);
  79.   }
  80.   else if (position < 159)  // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
  81.   {
  82.     position -= 127;
  83.     return leds.Color(position * 8, 0, 0xFF);
  84.   }
  85.   else  //160 <position< 191   Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
  86.   {
  87.     position -= 159;
  88.     return leds.Color(0xFF, 0x00, 0xFF - position * 8);
  89.   }
  90. }
复制代码

second sample,Bluno蓝牙控制,嘎嘎

这个是可以通过手机app来控制的,当手机与Bluno蓝牙配对连接后就可以控制了,app的下载网址https://www.dfrobot.com.cn/goods-823.html
app界面如下(箭头所指处事蓝牙连接)
Bluno控制灯带图11

代码(注意提前导入库)
  1. #include <Adafruit_NeoPixel.h>
  2. #include "WS2812_Definitions.h"
  3. #include <avr/pgmspace.h>
  4. #include <Wire.h>
  5. #include "Arduino.h"
  6. #include "PlainProtocol.h"
  7. #include <Metro.h>
  8. #include <AudioAnalyzer.h>
  9. #define PIN 6             //Arduino Pin connect to the LED Strip
  10. #define LED_COUNT 220      //set the Amount of LED to 180 and this number depend on how many you really have
  11. PlainProtocol BLUNOPlainProtocol(Serial,115200);//set Serial baud rate to 115200   
  12. Adafruit_NeoPixel leds = Adafruit_NeoPixel(LED_COUNT, PIN, NEO_GRB + NEO_KHZ800);// NEO_GRB means the type of your LED Strip
  13. Metro ledMetro = Metro(18);  // Metro for data receive in a regular time
  14. Analyzer Audio = Analyzer(4,5,0); // Strobe->4 RST->5 Analog->0
  15. int humid=0;
  16. int temper=0;          //  humid and temper means nothing in this program,just for the software in your phone
  17. int State01=2,State02=0;         //  the value will change when your press "BUZZER" in your phone   
  18. int Red=10,Green=0,Blue=10,Number_Position_RGB=100;//the value of RGB and the position of the LEDs
  19. int FreqVal[7];//the spectrum value
  20. int color[]={0xDC143C,0xFFA500,0xFFFF00, 0x32CD32,0x0000FF,0x2F4F4F,0x4B0082,0xDA70D6};
  21. int Num_Channel=0,Buff_Channel=0;
  22. int Num_Color[7],Buff_Num_Color[7];
  23. boolean Dis_En=false;
  24. int Num_First_Color=0,Buf_Max=0; //
  25. int Num_Shark02_High=0,Number_Shark02_LOW=0;
  26. void setup()
  27. {
  28.   Audio.Init();//Init module
  29.   leds.begin();  // Call this to start up the LED strip.
  30.   clearLEDs();   // This function, defined below, turns all LEDs off...
  31.   leds.show();   // ...but the LEDs don't actually update until you call this.
  32.   Serial.begin(115200);
  33.   TCCR1B &= ~((1<<CS12)|(1<<CS11)|(1<<CS10));        //Clock select: SYSCLK divde 8;
  34.   TCCR1B |= (1<<CS11);
  35.   TCCR2B &= ~((1<<CS12)|(1<<CS11)|(1<<CS10));        //Clock select: SYSCLK divde 8;
  36.   TCCR2B |= (1<<CS11);
  37. }
  38. void loop()
  39. {
  40.   temper=State01;
  41.   humid=State02;
  42.   if (BLUNOPlainProtocol.available())
  43.   {
  44.     if(BLUNOPlainProtocol.receivedCommand=="TEMP")          BLUNOPlainProtocol.write("TEMP", temper);  
  45.     else if(BLUNOPlainProtocol.receivedCommand=="HUMID")    BLUNOPlainProtocol.write("HUMID", humid);
  46.     if (BLUNOPlainProtocol.receivedCommand=="BUZZER")          //get command of "BUZZER"
  47.     {
  48.       if(BLUNOPlainProtocol.receivedContent[0]==1)  State01=1;
  49.       else                                          State01=2;
  50.     }
  51.     else if(BLUNOPlainProtocol.receivedCommand=="RGBLED")  //get command of  "RGB"
  52.     {
  53.       State01=3;
  54.       Red  = BLUNOPlainProtocol.receivedContent[0];
  55.       Green= BLUNOPlainProtocol.receivedContent[1];
  56.       Blue = BLUNOPlainProtocol.receivedContent[2];
  57.     }
  58.   }
  59.   if (ledMetro.check() == 1)//time for metro
  60.   {
  61.     if(State01==1)
  62.     {
  63.       clearLEDs();  // Turn off all LEDs
  64.       leds.show();
  65.     }
  66.     else if(State01==2)     
  67.     {   
  68.         Rock_With_Song();//leds.show();           
  69.     }
  70.     else if(State01==3)
  71.     {
  72.       for (int i=0;i<LED_COUNT;i++)
  73.       {
  74.         if(i%7==0)
  75.           leds.setPixelColor(i,Red, Green, 0);//change the color
  76.         else if(i%3==0)
  77.           leds.setPixelColor(i,0, Green, Blue);//change the color
  78.         else if(i%2==0)
  79.           leds.setPixelColor(i,Red, Green, Blue);//change the color
  80.         else     
  81.           leds.setPixelColor(i,Red,0, Blue);//change the color
  82.       }
  83.       leds.show();
  84.     }
  85.   }
  86. }
  87. void clearLEDs()
  88. {
  89.   for (int i=0; i<LED_COUNT; i++)  leds.setPixelColor(i, 0);
  90. }
  91. void rainbow(byte startPosition)
  92. {
  93.   int rainbowScale =  192/LED_COUNT;
  94.   leds.setPixelColor( startPosition, rainbowOrder((rainbowScale * ( startPosition + startPosition)) % 192));
  95.   leds.show();
  96. }
  97. uint32_t rainbowOrder(byte position)
  98. {
  99.   if (position < 31)  // Red -> Yellow (Red = FF, blue = 0, green goes 00-FF)
  100.   {
  101.     return leds.Color(0xFF, position * 8, 0);
  102.   }
  103.   else if (position < 63)  // Yellow -> Green (Green = FF, blue = 0, red goes FF->00)
  104.   {
  105.     position -= 31;
  106.     return leds.Color(0xFF - position * 8, 0xFF, 0);
  107.   }
  108.   else if (position < 95)  // Green->Aqua (Green = FF, red = 0, blue goes 00->FF)
  109.   {
  110.     position -= 63;
  111.     return leds.Color(0, 0xFF, position * 8);
  112.   }
  113.   else if (position < 127)  // Aqua->Blue (Blue = FF, red = 0, green goes FF->00)
  114.   {
  115.     position -= 95;
  116.     return leds.Color(0, 0xFF - position * 8, 0xFF);
  117.   }
  118.   else if (position < 159)  // Blue->Fuchsia (Blue = FF, green = 0, red goes 00->FF)
  119.   {
  120.     position -= 127;
  121.     return leds.Color(position * 8, 0, 0xFF);
  122.   }
  123.   else  //160 <position< 191   Fuchsia->Red (Red = FF, green = 0, blue goes FF->00)
  124.   {
  125.     position -= 159;
  126.     return leds.Color(0xFF, 0x00, 0xFF - position * 8);
  127.   }
  128. }
  129. void Rock_With_Song()
  130. {
  131.   Buff_Channel=Num_Channel;// Buff_Channnel can store the number of the channel which has the max spectrum value
  132.   Audio.ReadFreq(FreqVal);// get the spectrum value
  133.   for (int i=0;i<6;i++)
  134.   {
  135.     if(FreqVal[Num_Channel]<FreqVal[i])  Num_Channel=i;// get the number of the channel which has the max spectrum value
  136.     Buff_Num_Color[i]=Num_Color[i];                      //store the value for the using below
  137.   }
  138.   if(FreqVal[Num_Channel]<400)      {  
  139.     Number_Shark02_LOW++;
  140.   } //count if a low voice started
  141.   if(Buf_Max!=Num_Channel && FreqVal[Num_Channel]>300)    // judge if the sound changed
  142.   {
  143.     Num_First_Color++;
  144.     Dis_En=true;                                          //enable the display
  145.     if(FreqVal[Num_Channel]>400) Number_Shark02_LOW=0;    //reset the count of low voice
  146.     if (Num_First_Color==7) Num_First_Color=0;                              //for recycle
  147.     for(int i=0;i<7;i++)                                  //recycle the colour
  148.     {
  149.       int x=random(0,6);         
  150.       if(i==0)   Num_Color[i]=Num_First_Color;                  //recycle the value        
  151.       else       Num_Color[i]=Buff_Num_Color[x];       //change the color randomly
  152.     }
  153.   }
  154.   Buf_Max=Num_Channel;                                   //store the max_value_channel for next use
  155.   if( (Buf_Max==5 || Buf_Max==4 ) && FreqVal[Buf_Max]>700)  //count when the  High vlaue of the sound started
  156.   {
  157.     Num_Shark02_High++;
  158.   }
  159.   else Num_Shark02_High=0;                                  //reset the count of the High_value_count
  160.   if(Num_Shark02_High>22)                                  //time of High value voice reached
  161.   {
  162.     for(int i=0;i<LED_COUNT/2;i++)                         //these are effects of color changing
  163.     {
  164.       leds.setPixelColor(i,rainbowOrder(i));               //rising from two origin points
  165.       leds.setPixelColor(i+LED_COUNT/2,rainbowOrder(i));
  166.       leds.show();
  167.       if(i>LED_COUNT/4) delay(1);
  168.     }
  169.     for(int i=0;i<LED_COUNT/2;i++)                        
  170.     {
  171.       leds.setPixelColor(LED_COUNT-i,rainbowOrder(i));
  172.       leds.setPixelColor(i+LED_COUNT/2,rainbowOrder(i));
  173.       leds.show();
  174.     }
  175.     for (int i=0;i<LED_COUNT;i++)
  176.     {
  177.       leds.setPixelColor(i,GOLD);
  178.     }
  179.     leds.show();
  180.     Audio.ReadFreq(FreqVal);                                 
  181.     if(FreqVal[4]>800)                                         //if High sound value continues, take another effect out!
  182.     {
  183.       for(int x=0;x<6;x++)
  184.       {
  185.         if(FreqVal[x]>1000)   
  186.         {
  187.           for(int y=0;y<LED_COUNT/2;y++)    {
  188.             leds.setPixelColor(LED_COUNT-y,RED);
  189.             leds.setPixelColor(LED_COUNT/2-y,RED);
  190.             leds.show();
  191.           }
  192.           x=7;
  193.         }
  194.       }
  195.     }
  196.     Num_Shark02_High=0;                                  //reset the count when effect playing finished
  197.   }
  198.   if(Number_Shark02_LOW>40)                             //when the time of low value sound reached
  199.   {
  200.     for(int i=0;i<LED_COUNT/2;i++)                       //close the light from two point
  201.     {
  202.       leds.setPixelColor(i,0);
  203.       leds.setPixelColor(LED_COUNT-i,0);
  204.       leds.show();
  205.     }
  206.     Number_Shark02_LOW=0;
  207.     Dis_En=false;                                    //disable the function:Display() because the light should be closed
  208.   }
  209.   if(Dis_En==true)
  210.     Display();
  211. }
  212. void Display()                           
  213. {
  214.   for (int i=0;i<LED_COUNT;i++)
  215.   {
  216.     if(i<LED_COUNT/7)
  217.     {
  218.       leds.setPixelColor(i,color[Num_Color[0]]);  
  219.     }
  220.     else if(i<(LED_COUNT/7)*2)   {  
  221.       leds.setPixelColor(i,color[Num_Color[1]]);  
  222.     }
  223.     else if(i<(LED_COUNT/7)*3)   {  
  224.       leds.setPixelColor(i,color[Num_Color[2]]);  
  225.     }
  226.     else if(i<(LED_COUNT/7)*4)   {  
  227.       leds.setPixelColor(i,color[Num_Color[3]]);  
  228.     }
  229.     else if(i<(LED_COUNT/7)*5)   {  
  230.       leds.setPixelColor(i,color[Num_Color[4]]);  
  231.     }
  232.     else if(i<(LED_COUNT/7)*6)   {  
  233.       leds.setPixelColor(i,color[Num_Color[5]]);  
  234.     }
  235.     else if(i<LED_COUNT)         {  
  236.       leds.setPixelColor(i,color[Num_Color[6]]);  
  237.     }
  238.   }
  239.   leds.show();
  240. }
复制代码
另外,上面这段程序如果不连接蓝牙的话,则是声音传感器和音频分析模块来通过分析外界声音,使灯带有节奏的变化








hnyzcj  版主

发表于 2015-12-27 18:31:26

让我们忍受也可以,你跳个舞啥的,就行了。
回复

使用道具 举报

大连林海  初级技神

发表于 2015-12-27 19:26:43

hnyzcj 发表于 2015-12-27 18:31
让我们忍受也可以,你跳个舞啥的,就行了。

他把打印机当做圣诞树了  话说他的好玩东西现在比你还要做 那么多那么多
回复

使用道具 举报

hnyzcj  版主

发表于 2015-12-27 20:20:13

大连林海 发表于 2015-12-27 19:26
他把打印机当做圣诞树了  话说他的好玩东西现在比你还要做 那么多那么多 ...

haodeshoudao
回复

使用道具 举报

丄帝De咗臂  高级技匠
 楼主|

发表于 2015-12-27 21:01:40

大连林海 发表于 2015-12-27 19:26
他把打印机当做圣诞树了  话说他的好玩东西现在比你还要做 那么多那么多 ...

低调。。。
回复

使用道具 举报

大连林海  初级技神

发表于 2015-12-27 21:02:23


明天要玩个什么呢
回复

使用道具 举报

丄帝De咗臂  高级技匠
 楼主|

发表于 2015-12-27 21:05:45


明天先干活,干完活玩玩那个六足机器人
回复

使用道具 举报

大连林海  初级技神

发表于 2015-12-27 21:15:22

丄帝De咗臂 发表于 2015-12-27 21:05
明天先干活,干完活玩玩那个六足机器人

那我明天玩馒头
回复

使用道具 举报

孙毅  初级技匠

发表于 2015-12-28 13:22:54

hnyzcj 发表于 2015-12-27 18:31
让我们忍受也可以,你跳个舞啥的,就行了。

陈老师口味还是没有变。。。。
回复

使用道具 举报

dsweiliang  初级技神

发表于 2015-12-29 09:13:24

一套下来好贵啊~
回复

使用道具 举报

丄帝De咗臂  高级技匠
 楼主|

发表于 2015-12-29 10:20:44


来一套吧,骚年
回复

使用道具 举报

dsweiliang  初级技神

发表于 2015-12-29 11:52:11


太贵了~
回复

使用道具 举报

Geemi  初级技匠

发表于 2015-12-30 17:57:42

这……………………代码好长
回复

使用道具 举报

丄帝De咗臂  高级技匠
 楼主|

发表于 2015-12-30 18:07:15

Geemi 发表于 2015-12-30 17:57
这……………………代码好长

对于我这凡夫俗子,能看懂就好
回复

使用道具 举报

凌风清羽  中级技匠

发表于 2016-1-10 20:46:10

为啥都有这么多DF的东西,穷孩子只能看看了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail