C5 — Analogue inputs

Level 2 — Controlling the LED with an analogue input
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(12, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600); // set the speed of the serial data connection
}
void loop() {
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.print("The sensor value is: "); // send the words over the serial data connection
Serial.println(sensorValue); // send the sensor value over the serial data connection
delay(100); // wait for 0.1 seconds so that we only send the sensor value back 10 times a second.
}
void setup() {
pinMode(13, OUTPUT); // set up pin 13 as an output.
}
void loop() {
digitalWrite(13, HIGH); // turn the LED on
delay(1000); // wait for 1 second
digitalWrite(13, LOW); // turn the LED off
delay(1000); // wait for 1 second
}
The number in the brackets in the delay()
function tells the Arduino how long to delay. But it doesn’t have to be a number – you can put a variable there too.
Change each delay(1000);
to be delay(sensorValue);
.