序
最近看到大家玩的小夜灯,觉得很有意思,所以拿Arduino玩一下。准备实现的效果是可以通过手机蓝牙控制小夜灯的显示状态。
准备
材料清单:
- Arduino主板*1
- 光敏电阻*1
- 分压电阻*1
- LED灯*1
- 限流电阻*3
- 面包板*1
- 杜邦线若干
- 外壳*1
- 电池盒*1
组装好之后如下:
开始制作
电气连接
首先是一通连线,感觉没什么要注意的地方,这个要拿什么画连线图好看一点我也不是很清楚,流水账记录一下。LED灯的红蓝绿分别通过限流电阻后接到Arduino的3,5,6引脚,光敏电阻接到A0后和分压电阻串联,其他就常规该怎么连就怎么连就好了。
代码编写
#define rLedPin 6 //RGB-LED引脚R
#define gLedPin 5 //RGB-LED引脚G
#define bLedPin 3 //RGB-LED引脚B
#define lightSensorPin A0 //光敏电阻分压电路信号输出
char bluetooth_flag='0';
int ledR = 0; //R Led 亮度
int ledG = 0; //G Led 亮度
int ledB = 0; //B Led 亮度
int lightReading; //光敏电阻分压电路信号输出
bool onOffState; //小夜灯开关状态
unsigned long previousLightMillis;
int lightCheckInterval = 1000;
int colorIndex;
int colorChangeDelay = 1;
void setup() {
//设置引脚为相应工作模式
pinMode(rLedPin, OUTPUT);
pinMode(gLedPin, OUTPUT);
pinMode(bLedPin, OUTPUT);
Serial.begin(9600);
lightReading = analogRead(lightSensorPin);
Serial.println("");
Serial.print("lightReading = "); Serial.println(lightReading);
Serial.println("===========================");
Serial.println("");
}
void loop() {
if(Serial.available()>0)
{
bluetooth_flag = Serial.read();//读取蓝牙模块发送到串口的数据
}
if(bluetooth_flag=='0'){
analogWrite (rLedPin, 0);
analogWrite (gLedPin, 0);
analogWrite (bLedPin, 0);
}
if(bluetooth_flag=='1'){
unsigned long currentMillis = millis();
lightCheck(currentMillis);
if(lightReading>=280){
if (onOffState == 0) fadeOn();
onOffState = 1;
if (colorIndex <= 1535) {
colorIndex++;
} else if ( colorIndex > 1535) {
colorIndex = 0;
}
ledShowColor(colorIndex);
} else {
if (onOffState == 1) fadeOff(); //保持小夜灯熄灭
onOffState = 0;
}
}
if(bluetooth_flag=='2'){ //red
analogWrite (rLedPin, 255);
analogWrite (gLedPin, 0);
analogWrite (bLedPin, 0);
}
if(bluetooth_flag=='3'){ //green
analogWrite (rLedPin, 0);
analogWrite (gLedPin, 255);
analogWrite (bLedPin, 0);
}
if(bluetooth_flag=='4'){ //blue
analogWrite (rLedPin, 0);
analogWrite (gLedPin, 0);
analogWrite (bLedPin, 255);
}
}
void fadeOn(){
int i;
while(i < 255){
i++;
ledR++;
ledG++;
ledB++;
analogWrite (rLedPin, ledR);
analogWrite (gLedPin, ledG);
analogWrite (bLedPin, ledB);
}
}
void fadeOff(){
Serial.println("Fade OFF");
while(ledR > 0){
ledR--;
analogWrite (rLedPin, ledR);
}
while(ledB > 0){
ledB--;
analogWrite (bLedPin, ledB);
}
while(ledG > 0){
ledG--;
analogWrite (gLedPin, ledG);
}
colorIndex = 0;
}
void ledShowColor(int ledColorIndex){
if (ledColorIndex >= 0 && ledColorIndex <= 255){
ledR = 255 - ledColorIndex;
analogWrite (rLedPin, ledR);
} else if(ledColorIndex >= 256 && ledColorIndex <= 511){
ledR = ledColorIndex -256;
analogWrite (rLedPin, ledR);
} else if(ledColorIndex >= 512 && ledColorIndex <= 767){
ledG = 767 - ledColorIndex;
analogWrite (gLedPin, ledG);
} else if(ledColorIndex >= 768 && ledColorIndex <= 1023){
ledG = ledColorIndex - 768;
analogWrite (gLedPin, ledG);
} else if(ledColorIndex >= 1024 && ledColorIndex <= 1279){
ledB = 1279 - ledColorIndex;
analogWrite (bLedPin, ledB);
} else if(ledColorIndex >= 1280 && ledColorIndex <= 1535){
ledB = ledColorIndex - 1280;
analogWrite (bLedPin, ledB);
}
delay(colorChangeDelay);
}
void lightCheck(unsigned long thisLightMillis) {
if ((unsigned long)(thisLightMillis - previousLightMillis >= lightCheckInterval)) {
lightReading = analogRead(lightSensorPin);
previousLightMillis = thisLightMillis;
}
}
组装外壳
外壳主要包含两部分,一部分是灯罩,另一部分是类似机箱用来放置主板和连接线。
蓝牙调试
手机上安装蓝牙串口助手后,可以通过手机发送信息来可控制LED灯的显示状态。
结束
最终实现了手机远程控制开关,显示状态切换功能。好玩,不用下床关灯了。
PS: 如有疑问需要交流可加V:robotmcomao