arduino按钮切换led状态:
int LED=13;
int BUTTON=3;
int val=0;
int old_val=0;
int state=0;
void setup() {
// put your setup code here, to run once:
pinMode(LED,OUTPUT);
pinMode(BUTTON,INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(BUTTON);
if (val == HIGH && old_val == LOW) { // 注释2
state = 1 - state;
}
old_val = val; // 注释1
if (state == 1) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}