写ARDUINO适用于 uno r3开发板的程序,通过光控控制黄红绿LED灯,并在液晶显示屏显示实时时间,并让舵机转动
时间: 2024-03-11 21:47:15 浏览: 133
好的,以下是一个简单的 ARDUINO 程序,可以通过光敏电阻控制三个 LED 灯的亮灭,同时在液晶显示屏上显示实时时间,最后让一个舵机旋转:
```c++
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
#define RED_LED 9
#define YELLOW_LED 10
#define GREEN_LED 11
#define LIGHT_SENSOR A0
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo myservo;
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
myservo.attach(3);
lcd.init();
lcd.backlight();
}
void loop() {
int light_value = analogRead(LIGHT_SENSOR);
int hour = hour();
int minute = minute();
int second = second();
lcd.setCursor(0, 0);
lcd.print("Time: ");
lcd.print(hour);
lcd.print(":");
lcd.print(minute);
lcd.print(":");
lcd.print(second);
if (light_value < 300) {
digitalWrite(RED_LED, HIGH);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, LOW);
myservo.write(90);
} else if (light_value >= 300 && light_value < 700) {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, HIGH);
digitalWrite(GREEN_LED, LOW);
myservo.write(0);
} else {
digitalWrite(RED_LED, LOW);
digitalWrite(YELLOW_LED, LOW);
digitalWrite(GREEN_LED, HIGH);
myservo.write(180);
}
delay(1000);
}
```
注意,这段代码默认使用 I2C 接口的液晶显示屏和舵机。如果使用其他型号或者接口的液晶显示屏或舵机,请相应地修改代码。另外,光敏电阻的阈值需要根据实际情况调整。
阅读全文