255是灯的最大亮度;
1.模拟输出analogWrite(pin,参数) //通过参数的大小,控制引脚(比如说亮度)
boolean pushButton1;
boolean pushButton2;
int LED=9;
int brightness=128 //LED亮度参数
void setup(){
pinMode(2,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
pinMode(LED,OUTPUT);
Serial.begin(115200);
}
void loop(){
pushButton1=digitalRead(D2);
pushButton2=digitalRead(D3);
if(!pushButton1 && brightness>0) //当按下开关1,并且brightness>0
{
brightness--;
}
if(!pushButton2 && brightness<255) //当按下开关2,并且brightness<255
{
brightness++;
}
analogWrite(LED,brightness); //通过birghtness的大小来模拟控制LED亮度
Serial.println(brightness);
delay(10);
}
标签:LED,brightness,模拟输出,亮度,analog,pushButton2,pushButton1,输入 From: https://www.cnblogs.com/fengzhihean/p/16833765.html