ArduinoArduino UnoExact chip

Button and LED

Hold the button and the LED lights; the serial port says so.

Button and LED in Argynix Lab

The code

sketch.inoC++
// Hold the button: the LED lights. The button joins pin 2 to GND, so with
// INPUT_PULLUP the pin reads LOW while it is pressed.

const int BUTTON = 2;
const int LED = 13;

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  static bool was = false;
  bool pressed = digitalRead(BUTTON) == LOW;
  digitalWrite(LED, pressed ? HIGH : LOW);
  if (pressed != was) {
    Serial.println(pressed ? "Pressed" : "Released");
    was = pressed;
  }
  delay(5);
}