int ledPin=4; //led接板子上的4端口
int buttonPin=2; //按键2端口,接外部中断0
int buttonState=0; //按键状态的标志位,按一次为1,再按一次变为0
void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT); // 初始化ledPin引脚为输出引脚
pinMode(buttonPin,INPUT); // 初始化buttonPin引脚为输入引脚
attachInterrupt(0, interrupt, FALLING); //触发中断0,调用blink函数,下降沿触发
}
void loop() {
// put your main code here, to run repeatedly:
if(buttonState==1){
digitalWrite(ledPin,HIGH);
}else{
digitalWrite(ledPin,LOW);
}
}
void interrupt(){
buttonState=!buttonState;
}