//负熵生之光
#include <SoftwareSerial.h>
SoftwareSerial BTserial(7, 8); // 建立SoftwareSerial对象,RX引脚8, TX引脚7
int r , g , b ;
const int W = 3 ; int A = 0; // 开关计数返回值
const int R = 9 ; const int C = 4; // 红外感应模块
const int G = 6 ; const int KG=12; // 分级开关
const int B = 5 ; const int E = 6; // RGBW选项
const int M = A0; const int K = A1; //光敏模块 声音模块
int f = 0 ; const int NE= 2; //W 亮度值 RGBW驱动使能
const int fan=10; const int rm= A2; //散热风扇 热敏电阻
int X[3]={ 9,6,5 }; //RGB引脚数组
unsigned long te ; //开始时间
void setup()
{ BTserial.begin(9600);
Serial.begin(9600);
pinMode(NE,OUTPUT);
pinMode(W, OUTPUT); pinMode(R, OUTPUT);
pinMode(G, OUTPUT); pinMode(B, OUTPUT);
pinMode(M, INPUT ); pinMode(K, INPUT );
pinMode(KG, INPUT_PULLUP); pinMode(C, INPUT );
r=0; g=0; b=0;
}
void loop(){
sanre(); //散热风扇
switch (ScanKey()) {
case 0 : ganyin(); break;
case 1 : col(0,0,0,f) ; shijian ( 2 ); break;
case 2 : col(0,10,10,190); shijian ( 2 ); break;
case 3 : col(0,0,0, 220); shijian ( 2 ); break;
case 4 : col(0,0,0, 255); shijian ( 1 ); break;
case 5 : col(0,200,200,250); shijian ( 1 ); break;
case 6 : RGBW(); shijian ( 2 ); break;
default: col(0,0,0,0); digitalWrite(NE, LOW);
r=0; g=0; b=0; A = 0; f=0; break;
}
}
//蓝牙控制
void BT() {
if( BTserial.available()>0 ) { //如果软件串口有HC-06发来的数据
if( BTserial.read()==':' ) {
r = BTserial.parseInt();
g = BTserial.parseInt();
b = BTserial.parseInt();
if ( BTserial.read()==';' ) {
col(r,g,b,0);
}
}
}
}
//温度控制
void sanre(){
if(analogRead(rm)<420){
if(analogRead(rm)<405){
if(analogRead(rm)<385){
digitalWrite(NE, LOW);}
digitalWrite(fan, HIGH);}
analogWrite(fan, 160);
}else{
analogWrite(fan , 0);
}
}
//时间控制
void shijian (int t){
unsigned long tme = millis();
if( (tme - te )>(t*3600*1000)){
A=0;
}
}
//按键控制
int ScanKey()
{ //按键扫描函数 接上拉电阻 按键导通INPUT低电平到单片机
if (digitalRead(KG) == LOW || analogRead(M)>600 ) {
delay(50);
if (digitalRead(KG) == LOW || analogRead(K)>255 ) {
A++;
while (digitalRead(KG) == LOW || analogRead(M)>666 );
}
}
return A;
}
//红外感应控制
void ganyin(){
te= millis();
if(analogRead(M)>700&&digitalRead(C)==HIGH){
col(0,0,0,f); //RGBW
}else{
BT();
}
}
//RGBW组件
void col(int r,int g,int b,int w){
digitalWrite(NE, HIGH);
if(f<160){delay(4); f++; }
analogWrite(R, r); analogWrite(G, g);
analogWrite(B, b); analogWrite(W, w);
}
//呼吸灯组件
void rgbw(int n) {
int c = 12;
for (int a = 200; a >= 0; a -= 1) {
analogWrite(n, a);
unsigned long oldtime = millis();
while (millis() - oldtime < c){
if (ScanKey() != E){
return;
}
}
oldtime = millis();
}
for (int a = 0; a <= 200; a +=1) {
analogWrite(n, a);
unsigned long oldtime = millis();
while (millis() - oldtime < c){
if (ScanKey() != E){
return;
}
}
oldtime = millis();
}
}
//呼吸灯组件
int RGBW()
{
digitalWrite(NE, HIGH);
for (size_t i = 0; i < 3; i++)
{
if (i == 0) {
rgbw(X[0]);
if (ScanKey() != E){
col(0,0,0,0); //熄灭
return 0;
}
}
else if (i == 1){
rgbw(X[1]);
if (ScanKey() != E){
col(0,0,0,0); //熄灭
return 0;
}
}
else{
rgbw(X[2]);
if (ScanKey() != E){
col(0,0,0,0); //熄灭
return 0;
}
}
}
}