Let there be light!
Now we have everything we need to know to start building our project, let's start by writing the code:
int sensorPin = A0; // select the input pin for the ldr unsigned int sensorValue = 0; // variable to store the value coming from the ldr void setup() { pinMode(13, OUTPUT); //Start Serial port Serial.begin(9600); // start serial for output - for testing } void loop() { // read the value from the ldr: sensorValue = analogRead(sensorPin); if(sensorValue<400) digitalWrite(13, HIGH); // set the LED on else digitalWrite(13, LOW); // set the LED off }
We start by storing the pin numbers in variables, so that it doesn't become confusing, and then initialize our LED pins for output.
The code consists of a if
statement that checks the value of sensorValue
. If the values stored in it is less than 400
, meaning that...