uno用两个按键分别控制led灯亮度增加和减少
时间: 2024-06-03 17:08:01 浏览: 100
儿童节庆祝项目:Arduino控制LED显示屏和音乐播放
以下是Uno的代码:
int ledPin = 9; //定义LED引脚
int brightness = 0; //初始亮度为0
int buttonUp = 2; //定义增加亮度按钮
int buttonDown = 3; //定义减少亮度按钮
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonUp, INPUT_PULLUP);
pinMode(buttonDown, INPUT_PULLUP);
}
void loop() {
//检测增加亮度按钮是否按下
if (digitalRead(buttonUp) == LOW) {
brightness++;
if (brightness > 255) {
brightness = 255;
}
}
//检测减少亮度按钮是否按下
if (digitalRead(buttonDown) == LOW) {
brightness--;
if (brightness < 0) {
brightness = 0;
}
}
analogWrite(ledPin, brightness); //设置LED亮度
delay(10);
}
阅读全文