我必须制作一个程序,其中每次单击开关时开关都会导致LED改变亮度。下面的代码会使LED根据需要改变亮度,但如果按住开关则不会停止改变。我需要它只在每次单击开关时更改一次。如何在点击时更改一次,而不是循环显示其他亮度?
const int ledPin = 10;
int brightness = 0;
boolean up = 1;
int steps = 0;
int inpin = 2;
int reading;
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode (inpin, INPUT);
}
void loop() {
do{
reading = digitalRead(inpin);
}while (reading != HIGH);
analogWrite(ledPin, brightness);
delay(1000);
if (up)
{
brightness = brightness + 63;
}
else {
brightness = brightness - 63;
}
steps = steps + 1;
if (steps == 4)
{
up = !up;
steps = 0;
}
}
答案 0 :(得分:0)
亮度改变后,等待开关被释放。