功能需求

  1. 四种灯光模式 :照明灯、呼吸灯、流水灯、七彩灯
  2. 可实现99分钟内定时功能
  3. 可调节灯光颜色比例,实现任意彩灯切换
  4. 远程控制

image.png
硬件
Arduino UNO主板
HC-06蓝牙模块
RGB全彩灯 4个
四位数码管
手机
3D打印相关材料(外壳)或kt板
软件环境
Arduino IDE
APP inventor

接线
Arduino RGB全彩灯
A3 S
A4 S
A5 S
D2 S
5V VCC
GND GND
Arduino 数码管
11 CLK
12 DIO
5V VCC
GND GND
Arduino 蓝牙
RX TX
5V VCC
GND GND

程序

  1. #include <Adafruit_NeoPixel.h>//全彩LED库文件
  2. #include <Metro.h>
  3. #define PIN1 A3 //设置全彩LED引脚为A3 可自己调整
  4. #define PIN2 A4 //设置全彩LED引脚为A4 可自己调整
  5. #define PIN3 A5 //设置全彩LED引脚为A5 可自己调整
  6. #define PIN4 2 //设置全彩LED引脚为13 可自己调整
  7. #include <TM1637.h>
  8. // 数码管 配置引脚
  9. #define CLK 11 //!参数 clk -数字引脚连接到模块的时钟引脚数
  10. #define DIO 12//!参数 dio -数字引脚连接到模块的DIO引脚数
  11. TM1637 TM(CLK, DIO);//!初始化一个TM1637对象,设置时钟和数据引脚。
  12. #define MAX_LED 9
  13. Adafruit_NeoPixel strip1 = Adafruit_NeoPixel( MAX_LED, PIN1, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
  14. Adafruit_NeoPixel strip2 = Adafruit_NeoPixel( MAX_LED, PIN2, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
  15. Adafruit_NeoPixel strip3 = Adafruit_NeoPixel( MAX_LED, PIN3, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
  16. Adafruit_NeoPixel strip4 = Adafruit_NeoPixel( MAX_LED, PIN4, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
  17. Metro metro = Metro(6000); //把 blink1Metro 实例化 Metro 对象 ,并设置间隔时间
  18. int min_time;
  19. long second_time=0;
  20. int red=255,green=255,blue=255;
  21. static long time_1=0;
  22. static long time_2=0;
  23. //uint32_t color_led = strip1.Color(green,red,blue); //GRB
  24. //arduino引脚排列 VCC TX RX GND
  25. void setup()
  26. {
  27. Serial.begin(9600);//初始化波特率为9600
  28. strip1.begin(); //初始化Adafruit_NeoPixel;
  29. strip2.begin();
  30. strip3.begin();
  31. strip4.begin();
  32. LED(0);
  33. TM.show(false);//设置数码管显示 为真显示,否则不显示
  34. }
  35. String comdata = "";
  36. int mode_LED=0;
  37. bool timer=false;
  38. void loop()
  39. {
  40. while (Serial.available() > 0){
  41. comdata += char(Serial.read()); //每次读一个char字符,并相加
  42. delay(2);
  43. }
  44. if(timer) timing();//定时
  45. if (comdata.length() > 0){
  46. Serial.println(comdata); //打印接收到的字符
  47. switch(comdata.charAt(0)){
  48. case 'a':
  49. String str=comdata.substring(1,comdata.length());
  50. min_time=str.toInt();
  51. Serial.println(min_time);
  52. time_2=millis();
  53. timer=true;
  54. break;
  55. }
  56. /***********设置灯光颜色 rgb****************/
  57. if(comdata.charAt(0)=='g'){
  58. String str=comdata.substring(1,comdata.length());
  59. green=str.toInt();
  60. Serial.println(green);
  61. }
  62. else if(comdata.charAt(0)=='r'){
  63. String str=comdata.substring(1,comdata.length());
  64. red=str.toInt();
  65. Serial.println(red);
  66. }
  67. else if(comdata.charAt(0)=='b'){
  68. String str=comdata.substring(1,comdata.length());
  69. blue=str.toInt();
  70. Serial.println(blue);
  71. }
  72. /***********设置灯光模式****************/
  73. if(comdata.equals("one")){
  74. mode_LED=1;
  75. }
  76. else if(comdata.equals("two")){
  77. mode_LED=2;
  78. }
  79. else if(comdata.equals("three")){
  80. mode_LED=3;
  81. }
  82. else if(comdata.equals("four")){
  83. mode_LED=4;
  84. }
  85. else if(comdata.equals("off")){
  86. mode_LED=5;
  87. }
  88. comdata = "";
  89. }
  90. mode(mode_LED);
  91. }
  92. void timing(){
  93. //
  94. if(millis()-time_2>60000){
  95. min_time=min_time-1;
  96. time_2=millis();
  97. }
  98. if(min_time==0){
  99. LED(0);
  100. mode_LED=0;
  101. delay(500);
  102. TM.show(false);//设置数码管显示 为真显示,否则不显示
  103. timer=false;
  104. }else{
  105. TM.DNum(00,min_time,true);//显示双数字,左边两位显示num1最后两位;左边两位显示num2最后两位;piont 是否显示中间的两点
  106. TM.show(true);//设置数码管显示 为真显示,否则不显示
  107. }
  108. }
  109. void mode(int number){//灯光模式
  110. switch(number){
  111. case 1:
  112. LED_color(green,red,blue);
  113. LED(255);
  114. break;
  115. case 2:
  116. LED_color(green,red,blue);
  117. two();
  118. break;
  119. case 3:
  120. LED_color(green,red,blue);
  121. three();
  122. break;
  123. case 4:
  124. four();
  125. break;
  126. case 5:
  127. LED(0);
  128. break;
  129. }
  130. }
  131. int x=0;
  132. void two(){
  133. if(x==5) x=0;
  134. if(millis()-time_1>1000){
  135. x++;
  136. time_1=millis();
  137. }
  138. switch(x){
  139. case 1:
  140. strip1.setBrightness(255);
  141. strip1.show();
  142. strip2.setBrightness(0);
  143. strip2.show();
  144. strip3.setBrightness(0);
  145. strip3.show();
  146. strip4.setBrightness(0);
  147. strip4.show();
  148. break;
  149. case 2:
  150. strip1.setBrightness(0);
  151. strip1.show();
  152. strip2.setBrightness(255);
  153. strip2.show();
  154. strip3.setBrightness(0);
  155. strip3.show();
  156. strip4.setBrightness(0);
  157. strip4.show();
  158. break;
  159. case 3:
  160. strip1.setBrightness(0);
  161. strip1.show();
  162. strip2.setBrightness(0);
  163. strip2.show();
  164. strip3.setBrightness(255);
  165. strip3.show();
  166. strip4.setBrightness(0);
  167. strip4.show();
  168. break;
  169. case 4:
  170. strip1.setBrightness(0);
  171. strip1.show();
  172. strip2.setBrightness(0);
  173. strip2.show();
  174. strip3.setBrightness(0);
  175. strip3.show();
  176. strip4.setBrightness(255);
  177. strip4.show();
  178. break;
  179. }
  180. }
  181. int i=255;//呼吸灯 亮度
  182. bool flag;
  183. void three(){
  184. if(i==255) flag=true;
  185. if(i==0) flag=false;
  186. if(millis()-time_1>10){
  187. if(flag) i--;
  188. else i++;
  189. time_1=millis();
  190. }
  191. LED(i);
  192. }
  193. int j=0;
  194. void four(){
  195. if(j==7) j=0;
  196. if(millis()-time_1>2000){
  197. j++;
  198. time_1=millis();
  199. }
  200. switch(j){
  201. case 1:
  202. LED_color(0,255,0);
  203. break;
  204. case 2:
  205. LED_color(255,255,0);
  206. break;
  207. case 3:
  208. LED_color(0,0,255);
  209. break;
  210. case 4:
  211. LED_color(255,0,0);
  212. break;
  213. case 5:
  214. LED_color(255,0,255);
  215. break;
  216. case 6:
  217. LED_color(0,255,255);
  218. break;
  219. case 7:
  220. LED_color(255,255,255);
  221. break;
  222. }
  223. LED(255);
  224. }
  225. void LED_color(int green,int red,int blue){//设置灯光颜色
  226. strip1.setPixelColor(0,strip1.Color(green,red,blue));
  227. strip1.setPixelColor(1,strip1.Color(green,red,blue));
  228. strip2.setPixelColor(0,strip2.Color(green,red,blue));
  229. strip2.setPixelColor(1,strip2.Color(green,red,blue));
  230. strip3.setPixelColor(0,strip3.Color(green,red,blue));
  231. strip3.setPixelColor(1,strip3.Color(green,red,blue));
  232. strip4.setPixelColor(0,strip4.Color(green,red,blue));
  233. strip4.setPixelColor(1,strip4.Color(green,red,blue));
  234. }
  235. void LED(int light){//设置灯光亮度
  236. strip1.setBrightness(light);
  237. strip1.show();
  238. strip2.setBrightness(light);
  239. strip2.show();
  240. strip3.setBrightness(light);
  241. strip3.show();
  242. // strip4.setPixelColor(0, strip4.Color(green,red,blue);
  243. // strip4.setPixelColor(1,strip4.Color(green,red,blue));
  244. strip4.setBrightness(light);
  245. strip4.show();
  246. }