C4 — Digital inputs

Level 3 — The Backwards Light

Complete the challenge

int inputPin = 7;
int ledPin = 10;

void setup() {
    pinMode(ledPin, OUTPUT);         // set up pin "ledPin" as an output.
    pinMode(inputPin, INPUT_PULLUP); // set up pin "inputPin" as an input.
}

void loop() {
    if (digitalRead(inputPin) == HIGH) {  // check if the button is pressed
        digitalWrite(ledPin, HIGH);    // turn the LED on
    } else {
        digitalWrite(ledPin, LOW);     // turn the LED off
    }
}

You have 2 options. Either change something in the line that that says

    if (digitalRead(inputPin) == HIGH) {  // check if the button is pressed

or change something in the lines that say

        digitalWrite(ledPin, HIGH);    // turn the LED on
    } else {
        digitalWrite(ledPin, LOW);     // turn the LED off

Make your code do the opposite of what it does now - change LOW to HIGH and change HIGH to LOW.

You have 2 options. Either change the if line to this

    if (digitalRead(inputPin) == LOW) {  // check if the button is not pressed

or change the digitalWrite lines to

        digitalWrite(ledPin, LOW);    // turn the LED off
    } else {
        digitalWrite(ledPin, HIGH);     // turn the LED on

Claim your achievement