const int LDR_PIN = A0;
const int RED_PIN = 3;
const int GREEN_PIN = 5;
const int BLUE_PIN = 6;
void setup()
{
Serial.begin(9600);
pinMode(LDR_PIN, INPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop()
{
int ldrValue = analogRead(LDR_PIN); // 获取光线传感器的值
Serial.println(ldrValue);
if (ldrValue < 500) // 当环境较暗时
{
analogWrite(RED_PIN, 50); // 调节亮度
analogWrite(GREEN_PIN, 50);
analogWrite(BLUE_PIN, 50);
}
else // 当环境较亮时
{
analogWrite(RED_PIN, 255); // 调节亮度
analogWrite(GREEN_PIN, 255);
analogWrite(BLUE_PIN, 255);
}
delay(1000); // 每秒更新一次
}