// Example code when button input uses pull up resistor
// Set the output pin for the LED to pin 13,
// and the input pin for the button to pin 8
int ledPin = 13;
int buttonPin = 8;
// The button can have to states, either it is pressed (LOW)
// or not pressed (HIGH)
int buttonState = 0;
// The setup function runs as soon as the Arduino gets
// connected to power
void setup() {
// Set the LED pin as output and the button pin as
// input with a pull-up resistor
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
// The loop is the main loop that runs/loops as fast as
// fast possible executing all code within it
void loop() {
// Read the button pin using digital read function
// and store this value in the variable buttonState
buttonState = digitalRead(buttonPin);
// Use a if statement to check if the button is pressed
// or not. If the button is pressed the button input pin
// goes low, if the buttonState variable is low, then turn
// on the LED pin by setting it high so it becomes bright.
// If the button is not pressed, then set the LED pin
// to low.
if (buttonState == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}