#define LED 7
#define BUTTON 9
int bts=0;
int bbts=0;
int state=0;
void setup() {
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
}
void loop() {
bts = digitalRead(BUTTON);
if((bts==1)&&(bbts==0)){ //检测开关完整过程(一开一关)
state=!state; //设置一个开关变量
delay(50); //防止按键抖动
}
bbts = bts; //改变前一次状态
if(state==1){
digitalWrite(LED,HIGH);
}else{
digitalWrite(LED,LOW);
}
}