guopengfa
发布于 2021-12-05 / 577 阅读 / 0 评论 / 0 点赞

Arduino点击亮灯点击灭灯

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(13, HIGH);
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);
  pinMode(4, INPUT);
  digitalWrite(4, HIGH);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(4) == LOW) {
    led_control();
  }
}

void led_control() {
  Serial.print("the Pin 4 is: ");
  Serial.print(digitalRead(4));
  Serial.print("\n");
  if (digitalRead(2) == HIGH) {
    digitalWrite(2, LOW);
  } else {
    digitalWrite(2, HIGH);
  }
  delay(1000);
}

评论