|
本帖最后由 topdog 于 2022-5-15 22:42 编辑
state还是用布尔变量吧!
按下灯长亮,按钮松开灭一秒。不按按钮灯是一直间隔一秒闪烁的。
[pre]const int LedPin = 13;
const int interruptPin = 2;
volatile bool state = false;
void setup()
{
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), isr, LOW);
}
void loop()
{
blink();
isr();
}
void blink()
{
digitalWrite(LedPin, HIGH);
delay(1000); // wait for a second
digitalWrite(LedPin, LOW);
delay(1000);
}
void isr()
{
digitalWrite(LedPin, HIGH);
state = digitalRead(interruptPin);
if (state == HIGH)
{
digitalWrite(LedPin, LOW);
delay(1000);
}
}[/pre]
|
|