//Design Concept 设计概念:
//There are two lamps on site, Lamp A and Lamp B. 现场有 A 台灯和B台灯,
//Both Lamp A and Lamp B include the following components: an RGB light, a motion sensor, and a distance sensor;
//A台灯、B台灯其中任一台灯都包括的组件:一个 RGB light, 一个 motion sensor,一个 distance sensor;
//Basic functions included in both Lamp A and Lamp B: can turn on / turn off (operated by a button); when turned on, the lamp emits yellow light; when turned off, the lamp is off; provides 2 levels of brightness adjustment (operated by a button).
//A台灯、B台灯其中任一台灯都包含的基本功能:可以 turn on / turn off(通过按钮操作开关);当 turn on 台灯,亮黄灯;当 turn off,台灯关闭;提供2档明暗调节功能(通过按钮操作明暗);
//Collaborative functions between the two lamps:
//When both lamps detect an object closer than 30cm in front of them && both lamps are turned on, the light from both lamps changes from yellow to pink.
//When one lamp is adjusted to a dimmer setting && the other lamp is turned on, the brightness of the latter automatically matches that of the former.
//两台台灯间的合作功能:
//1、当两个台灯同时感应各自前方距离小于30cm,且两个台灯都为 turn on 状态时,两个台灯灯光由黄光变为粉色灯光;
//2、当其中一个台灯被调节暗度,且另一个台灯为 turn on 状态时,后者的亮度自动与前者亮度保持一致
#include <NewPing.h>
// Define the pins of Lamp A 定义A台灯的引脚
int redPinA = 3;//RGB灯红灯
int greenPinA = 5;//RGB灯
int bluePinA = 6;//RGB灯
int motionSensorA = 2;// Motion Sensor
int triggerPinA = 7;// Distance Sensor Input
int echoPinA = 8;// Distance Sensor Outout
int buttonPinA = 12; // Lamp switch 台灯开关
int brightnessButtonA = 13; // Brightness adjustment switch 亮度调节开关
// Define the pins of Lamp B 定义B台灯的引脚
int redPinB = 9;
int greenPinB = 10;
int bluePinB = 11;
int motionSensorB = 4;
int triggerPinB = A2;
int echoPinB = A3;
int buttonPinB = A4;
int brightnessButtonB = A5;
// Define the distance sensor object 定义距离传感器对象
NewPing distanceSensorA(triggerPinA, echoPinA, 200); // Max distance = 200cm
NewPing distanceSensorB(triggerPinB, echoPinB, 200);
// Variables used to store the lamp's status and brightness level 变量用于存储灯的状态和亮度等级
bool lightStateA = false;
bool lightStateB = false;
int brightnessLevelA = 0; // 0 for dim, 1 for bright, 2 for brighter 0为暗,1为亮,2为更亮
int brightnessLevelB = 0;
// Button state variable 按钮状态变量
int lastButtonStateA = LOW;
int lastButtonStateB = LOW;
int lastBrightnessButtonStateA = LOW;
int lastBrightnessButtonStateB = LOW;
void setup() {
// Initialize serial communication 初始化串行通信
Serial.begin(9600);
// Set the LED pin as output 设置LED引脚为输出
pinMode(redPinA, OUTPUT);
pinMode(greenPinA, OUTPUT);
pinMode(bluePinA, OUTPUT);
pinMode(redPinB, OUTPUT);
pinMode(greenPinB, OUTPUT);
pinMode(bluePinB, OUTPUT);
// Set the sensor pin as input 设置传感器引脚为输入
pinMode(motionSensorA, INPUT);
pinMode(motionSensorB, INPUT);
// Set the button pin as input 设置按钮引脚为输入
pinMode(buttonPinA, INPUT_PULLUP);
pinMode(buttonPinB, INPUT_PULLUP);
pinMode(brightnessButtonA, INPUT_PULLUP);
pinMode(brightnessButtonB, INPUT_PULLUP);
}
void loop() {
// Read distance 读取距离
int distanceA = distanceSensorA.ping_cm();
int distanceB = distanceSensorB.ping_cm();
Serial.println(distanceA);
Serial.println(distanceB);
// Read button status 读取按钮状态
int buttonStateA = digitalRead(buttonPinA);
int buttonStateB = digitalRead(buttonPinB);
int brightnessStateA = digitalRead(brightnessButtonA);
int brightnessStateB = digitalRead(brightnessButtonB);
// Determine the switch and brightness adjustment for Lamp A 处理A台灯的开关和亮度调节
if (buttonStateA == LOW && lastButtonStateA == HIGH) {
lightStateA = !lightStateA; // Toggle the switch stete 切换开关状态
if (lightStateA) setLightColor(redPinA, greenPinA, bluePinA, 255, 255, 0); // Turn on, yellow light 开灯黄光
else setLightOff(redPinA, greenPinA, bluePinA); // Turn off 关灯
}
if (brightnessStateA == LOW && lastBrightnessButtonStateA == HIGH && lightStateA) {
adjustBrightness(redPinA, greenPinA, bluePinA, brightnessLevelA);
}
// Determine the switch and brightness adjustment for Lamp B 处理B台灯的开关和亮度调节
if (buttonStateB == LOW && lastButtonStateB == HIGH) {
lightStateB = !lightStateB; // Toggle the switch stete 切换开关状态
if (lightStateB) setLightColor(redPinB, greenPinB, bluePinB, 255, 255, 0); // Turn on 开灯黄光
else setLightOff(redPinB, greenPinB, bluePinB); // Turn off 关灯
}
if (brightnessStateB == LOW && lastBrightnessButtonStateB == HIGH && lightStateB) {
adjustBrightness(redPinB, greenPinB, bluePinB, brightnessLevelB);
}
// If both lamps are on && the distance is less than 30cm, then change to pink light 如果两个台灯都开着,并且距离小于30cm,则变为粉色灯光
if (lightStateA && lightStateB && distanceA < 30 && distanceB < 30) {
setLightColor(redPinA, greenPinA, bluePinA, 255, 0, 0); // Lamp A turn to Pink light A台灯粉色
setLightColor(redPinB, greenPinB, bluePinB, 255, 0, 0); // Lamp B turn to Pink light B台灯粉色
}
// Update the state of button 更新按钮状态
lastButtonStateA = buttonStateA;
lastButtonStateB = buttonStateB;
lastBrightnessButtonStateA = brightnessStateA;
lastBrightnessButtonStateB = brightnessStateB;
delay(100); // Slightly delay to reduce jitter 稍微延迟以减少抖动
}
// Function to set the light color 设置灯光颜色的函数
void setLightColor(int redPin, int greenPin, int bluePin, int redValue, int greenValue, int blueValue) {
analogWrite(redPin, redValue);
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
// Function to turn off the light 关闭灯光的函数
void setLightOff(int redPin, int greenPin, int bluePin) {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
// Function to adjust brightness 调整亮度的函数
void adjustBrightness(int redPin, int greenPin, int bluePin, int &brightnessLevel) {
brightnessLevel = (brightnessLevel + 1) % 3; // Cycle through 0, 1, 2 在0、1、2之间循环,每次按钮被按下时,亮度等级就按照0 -> 1 -> 2 -> 0的顺序变化
int brightness = (brightnessLevel == 0) ? 0 : (brightnessLevel == 1) ? 125 : 255;
// Yellow light as the base color 黄色灯光为基础色
setLightColor(redPin, greenPin, bluePin, brightness, brightness, 0);
}