这是一个简单的 Arduino UNO 项目,可以使用按钮或光传感器打开和关闭 LED 灯。 按钮工作正常,可以毫无问题地打开和关闭 LED 灯。光传感器也工作了,我调试了一下,它可以检测到光。我的问题是,即使光照水平 (>400) 超过阈值 (400),它仍然不会打开或关闭 LED,就像没有效果一样。 (带光500-700)。 这是我的 Arduino 图表。 (绿线为光传感器)Arduino 图
这是我的代码。
int ledPin = 13; // LED connected to digital pin 13
int lightSensorPin = A0; // Photoresistor connected to analog pin A0
int btnPin = 2; // Button connected to digital pin 2
int threshold = 400; // Light level threshold (adjust based on your sensor and environment)
int state = LOW; // Current state of the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
pinMode(btnPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int lightLevel = analogRead(lightSensorPin); // Read the light sensor value
int buttonState = digitalRead(btnPin); // Read the button state
// Debugging: Print the light sensor value and button state
Serial.print("Light Level: ");
Serial.print(lightLevel);
Serial.print(" | Button State: ");
Serial.println(buttonState);
// Toggle LED state when button is pressed
if (buttonState == LOW) { // Button pressed (active LOW)
delay(50); // Debounce delay
if (digitalRead(btnPin) == LOW) { // Confirm button is still pressed
state = !state; // Toggle the state
digitalWrite(ledPin, state); // Update the LED
while (digitalRead(btnPin) == LOW) {
// Wait for button release to avoid multiple toggles
}
}
}
else {
// Control LED based on light sensor only if the button is not pressed
if (lightLevel < threshold) {
digitalWrite(ledPin, LOW); // Turn on the LED if light level is below the threshold
} else {
digitalWrite(ledPin, HIGH); // Turn off the LED if light level is above the threshold
}
}
delay(100); // Small delay to smooth the sensor readings
}
我已经在 Arduino Uno 上运行了您的代码,并按您所连接的按钮,并用电位计代替了 LDR。当我转动电位计使模拟读数超过 400 值时,LED 也会相应地切换。所以你的代码可以工作。
为了更好地了解正在发生的情况,您可以编辑代码,以便获得更接近您使用该值的位置的串行端口的模拟读数。所以试试这个:
int ledPin = 13; // LED connected to digital pin 13
int lightSensorPin = A0; // Photoresistor connected to analog pin A0
int btnPin = 2; // Button connected to digital pin 2
int threshold = 400; // Light level threshold (adjust based on your sensor and environment)
int state = LOW; // Current state of the LED
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
pinMode(btnPin, INPUT_PULLUP); // Set the button pin as input with an internal pull-up resistor
Serial.begin(9600); // Initialize serial communication for debugging
}
void loop() {
int lightLevel = analogRead(lightSensorPin); // Read the light sensor value
int buttonState = digitalRead(btnPin); // Read the button state
/*
// Debugging: Print the light sensor value and button state
Serial.print("Light Level: ");
Serial.print(lightLevel);
Serial.print(" | Button State: ");
Serial.println(buttonState);
*/
// Toggle LED state when button is pressed
if (buttonState == LOW) { // Button pressed (active LOW)
delay(50); // Debounce delay
if (digitalRead(btnPin) == LOW) { // Confirm button is still pressed
state = !state; // Toggle the state
digitalWrite(ledPin, state); // Update the LED
while (digitalRead(btnPin) == LOW) {
// Wait for button release to avoid multiple toggles
}
}
}
else {
lightLevel = analogRead(lightSensorPin);
Serial.print("Light Level: ");
Serial.println(lightLevel); //note I am using println here
// Control LED based on light sensor only if the button is not pressed
if (lightLevel < threshold) {
digitalWrite(ledPin, LOW); // Turn on the LED if light level is below the threshold
} else {
digitalWrite(ledPin, HIGH); // Turn off the LED if light level is above the threshold
}
}
delay(100); // Small delay to smooth the sensor readings
}
我还注释掉了发送到串行端口的其他代码,以澄清发生了什么。
当您更改灯光级别时,您应该看到值转变为 400 级别,如通过串行监视器报告的那样。此时您应该会看到 LED 状态发生变化。
如果您看到串行读数,则 LED 不可能无法更改状态。如果您没有看到串行读数,则一定是 (buttonState == LOW) 条件阻止代码到达 LDR 部分。