Arduino push Button LED_BUILTIN

Arduino oefening: druk op een knopje en laat het ingebouwde LED branden.

Doel

:

Arduino startersoefening

Nodig

:

  • 1x Arduino
  • 1x Breadboard
  • 1x Button
  • 1x weerstand 200Ω

 

/* Turns on and off the built-in-LED (light emitting diode)
when pressing a pushbutton attached to pin 8. */

/// constants won't change. They're used here to set pin numbers:
const int buttonPin = 8; // the number of the pushbutton pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  // turn LED on:
  digitalWrite(LED_BUILTIN, HIGH);
  } else {
  // turn LED off:
  digitalWrite(LED_BUILTIN, LOW);
  }
}