/* * Task1 * * Uses a button with weak pull-up to light up an external LED * */ // Good practice to define pin numbers by logical meaning #define LED_PIN 2 #define BUTTON_IN 10 #define BUTTON_GND 12 void setup() { // Pin to control LED pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // Start with LED off // Pin to provide GND side of button pinMode(BUTTON_GND, OUTPUT); digitalWrite(BUTTON_GND, LOW); // Always LO (ground) // Pin to read button state, use internal pull-up to avoid external resistor // This also emulates TTL inputs that float HI. // Use INPUT for normal digital input (emulates CMOS input) pinMode(BUTTON_IN, INPUT_PULLUP); } void loop() { // Test the button state // As this is negative true (LO when pressed), invert the return value with ! bool buttonPressed = !digitalRead(BUTTON_IN); // Light up the LED if the button is pressed by writing the boolean (HI/LO) to the output pin digitalWrite(LED_PIN, buttonPressed); }