Let’s enjoy Arduino! LED With Button 2

By in Arduino, Programming on 2015年11月15日

Source Code is below.

int val = 0;
int state = 0;
void setup() {
  // put your setup code here, to run once:
  pinMode(7, OUTPUT);
  pinMode(2, INPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
  val = digitalRead(2);

  if (val == HIGH) {
    state = 1 - state;
  }

  if (state == 1) {
    digitalWrite(7, HIGH);
  } else {
    digitalWrite(7, LOW);
  }
}

 

Comments are closed.