https://v.youku.com/v_show/id_XNDI3NDY2NjA0MA==.html?spm=a2h3j.8428770.3416059.1
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 |
#include //全彩LED库文件
#include
#define PIN1 A3 //设置全彩LED引脚为A3 可自己调整
#define PIN2 A4 //设置全彩LED引脚为A4 可自己调整
#define PIN3 A5 //设置全彩LED引脚为A5 可自己调整
#define PIN4 2 //设置全彩LED引脚为13 可自己调整
#include
// 数码管 配置引脚
#define CLK 11 //!参数 clk -数字引脚连接到模块的时钟引脚数
#define DIO 12//!参数 dio -数字引脚连接到模块的DIO引脚数
TM1637 TM(CLK, DIO);//!初始化一个TM1637对象,设置时钟和数据引脚。
#define MAX_LED 9
Adafruit_NeoPixel strip1 = Adafruit_NeoPixel( MAX_LED, PIN1, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
Adafruit_NeoPixel strip2 = Adafruit_NeoPixel( MAX_LED, PIN2, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
Adafruit_NeoPixel strip3 = Adafruit_NeoPixel( MAX_LED, PIN3, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
Adafruit_NeoPixel strip4 = Adafruit_NeoPixel( MAX_LED, PIN4, NEO_RGB + NEO_KHZ800 );//Adafruit_NeoPixel实例化,通过构造函数初始化
Metro metro = Metro(6000); //把 blink1Metro 实例化 Metro 对象 ,并设置间隔时间
int min_time;
long second_time=0;
int red=255,green=255,blue=255;
static long time_1=0;
static long time_2=0;
//uint32_t color_led = strip1.Color(green,red,blue); //GRB
//arduino引脚排列 VCC TX RX GND
void setup()
{
Serial.begin(9600);//初始化波特率为9600
strip1.begin(); //初始化Adafruit_NeoPixel;
strip2.begin();
strip3.begin();
strip4.begin();
LED(0);
TM.show(false);//设置数码管显示 为真显示,否则不显示
}
String comdata = "";
int mode_LED=0;
bool timer=false;
void loop()
{
while (Serial.available() > 0){
comdata += char(Serial.read()); //每次读一个char字符,并相加
delay(2);
}
if(timer) timing();//定时
if (comdata.length() > 0){
Serial.println(comdata); //打印接收到的字符
switch(comdata.charAt(0)){
case 'a':
String str=comdata.substring(1,comdata.length());
min_time=str.toInt();
Serial.println(min_time);
time_2=millis();
timer=true;
break;
}
/***********设置灯光颜色 rgb****************/
if(comdata.charAt(0)=='g'){
String str=comdata.substring(1,comdata.length());
green=str.toInt();
Serial.println(green);
}
else if(comdata.charAt(0)=='r'){
String str=comdata.substring(1,comdata.length());
red=str.toInt();
Serial.println(red);
}
else if(comdata.charAt(0)=='b'){
String str=comdata.substring(1,comdata.length());
blue=str.toInt();
Serial.println(blue);
}
/***********设置灯光模式****************/
if(comdata.equals("one")){
mode_LED=1;
}
else if(comdata.equals("two")){
mode_LED=2;
}
else if(comdata.equals("three")){
mode_LED=3;
}
else if(comdata.equals("four")){
mode_LED=4;
}
else if(comdata.equals("off")){
mode_LED=5;
}
comdata = "";
}
mode(mode_LED);
}
void timing(){
//
if(millis()-time_2>60000){
min_time=min_time-1;
time_2=millis();
}
if(min_time==0){
LED(0);
mode_LED=0;
delay(500);
TM.show(false);//设置数码管显示 为真显示,否则不显示
timer=false;
}else{
TM.DNum(00,min_time,true);//显示双数字,左边两位显示num1最后两位;左边两位显示num2最后两位;piont 是否显示中间的两点
TM.show(true);//设置数码管显示 为真显示,否则不显示
}
}
void mode(int number){//灯光模式
switch(number){
case 1:
LED_color(green,red,blue);
LED(255);
break;
case 2:
LED_color(green,red,blue);
two();
break;
case 3:
LED_color(green,red,blue);
three();
break;
case 4:
four();
break;
case 5:
LED(0);
break;
}
}
int x=0;
void two(){
if(x==5) x=0;
if(millis()-time_1>1000){
x++;
time_1=millis();
}
switch(x){
case 1:
strip1.setBrightness(255);
strip1.show();
strip2.setBrightness(0);
strip2.show();
strip3.setBrightness(0);
strip3.show();
strip4.setBrightness(0);
strip4.show();
break;
case 2:
strip1.setBrightness(0);
strip1.show();
strip2.setBrightness(255);
strip2.show();
strip3.setBrightness(0);
strip3.show();
strip4.setBrightness(0);
strip4.show();
break;
case 3:
strip1.setBrightness(0);
strip1.show();
strip2.setBrightness(0);
strip2.show();
strip3.setBrightness(255);
strip3.show();
strip4.setBrightness(0);
strip4.show();
break;
case 4:
strip1.setBrightness(0);
strip1.show();
strip2.setBrightness(0);
strip2.show();
strip3.setBrightness(0);
strip3.show();
strip4.setBrightness(255);
strip4.show();
break;
}
}
int i=255;//呼吸灯 亮度
bool flag;
void three(){
if(i==255) flag=true;
if(i==0) flag=false;
if(millis()-time_1>10){
if(flag) i--;
else i++;
time_1=millis();
}
LED(i);
}
int j=0;
void four(){
if(j==7) j=0;
if(millis()-time_1>2000){
j++;
time_1=millis();
}
switch(j){
case 1:
LED_color(0,255,0);
break;
case 2:
LED_color(255,255,0);
break;
case 3:
LED_color(0,0,255);
break;
case 4:
LED_color(255,0,0);
break;
case 5:
LED_color(255,0,255);
break;
case 6:
LED_color(0,255,255);
break;
case 7:
LED_color(255,255,255);
break;
}
LED(255);
}
void LED_color(int green,int red,int blue){//设置灯光颜色
strip1.setPixelColor(0,strip1.Color(green,red,blue));
strip1.setPixelColor(1,strip1.Color(green,red,blue));
strip2.setPixelColor(0,strip2.Color(green,red,blue));
strip2.setPixelColor(1,strip2.Color(green,red,blue));
strip3.setPixelColor(0,strip3.Color(green,red,blue));
strip3.setPixelColor(1,strip3.Color(green,red,blue));
strip4.setPixelColor(0,strip4.Color(green,red,blue));
strip4.setPixelColor(1,strip4.Color(green,red,blue));
}
void LED(int light){//设置灯光亮度
strip1.setBrightness(light);
strip1.show();
strip2.setBrightness(light);
strip2.show();
strip3.setBrightness(light);
strip3.show();
// strip4.setPixelColor(0, strip4.Color(green,red,blue);
// strip4.setPixelColor(1,strip4.Color(green,red,blue));
strip4.setBrightness(light);
strip4.show();
}
下载链接:
QQ交流群:731722723 验证消息:Arduino