C5 — Analogue inputs

Level 3 — Turn LEDs on and off with the potentiometer

Complete the challenge

int sensorPin = A5;            // the input pin for the potentiometer on the ThinkerShield
int sensorValue = 0;           // variable to store the value coming from the sensor

void setup() {
    pinMode(8, OUTPUT);        // declare pin 8 as an OUTPUT
    pinMode(9, OUTPUT);        // declare pin 9 as an OUTPUT
    pinMode(10, OUTPUT);       // declare pin 10 as an OUTPUT
    pinMode(11, OUTPUT);       // declare pin 11 as an OUTPUT
    pinMode(12, OUTPUT);       // declare pin 12 as an OUTPUT
}

void loop() {
    sensorValue = analogRead(sensorPin);     // read the value from the sensor
}

How would you turn all of the LEDs off? digitalWrite(8, LOW); turns off LED 8.

Pseudocode “If the value from the potentiometer is between 0 and 204, turn LED 8 on.” needs three lines of actual code:

    if (sensorValue >= 0 && sensorValue <= 204) {  // if the value from the potentiometer is between 0 and 204.
        digitalWrite(8, HIGH);                      // turn LED 8 on
    }

Put the code inside the loop() function.

Claim your achievement