初学Arduino——按钮控制(二)

执行动作:

采用两个按钮,一个按钮按下后,led亮;另外一个按钮按下后,led灭

ps:两个按钮按下的瞬间,都是由低电压到高电压

int led = 3;
int button1 = 5;
int button2 = 8;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(button1) == HIGH) {
    digitalWrite(led, HIGH);
  }
  else if (digitalRead(button2) == HIGH) {
    digitalWrite(led, LOW);
  }
  else {}
}

 

你可能感兴趣的:(Arduino)