This code turn ON LED depends on the value of LDR.
const int LED = 13;
const int LDR = A0;
int value;
The first two lines declare two constant integer variables LED and LDR and assign them the values 13 and A0, respectively.
The third line declares an integer variable named "value" without assigning it a value.
void setup()
{
Serial.begin (9600);
pinMode(LED, OUTPUT);
pinMode(LDR, INPUT);
}
The setup() function is a built-in function in the Arduino programming language that runs once at the start of the program.
In this code, the Serial.begin(9600) line initializes the serial communication between the Arduino board and the computer at a baud rate of 9600 bits per second.
The pinMode(LED, OUTPUT) line sets the LED pin (pin 13) as an output pin.
The pinMode(LDR, INPUT) line sets the LDR pin (analog pin 0) as an input pin.
void loop() {
Serial.println (value);
value = analogRead (LDR);
delay (500);
int input = analogRead(LDR);
if (input > 780)
{
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}
}
The "loop" function is another built-in function that runs repeatedly as long as the Arduino board has power.
In this code, the "Serial.println(value)" line prints the value of the "value" variable to the serial monitor.
The "value = analogRead(LDR)" line reads the value from the LDR and assigns it to the "value" variable.
The "delay(500)" line creates a delay of 500 milliseconds (half a second) between each loop iteration.
The "input = analogRead(LDR)" line reads the value from the LDR and assigns it to the "input" variable.
The "if (input > 780)" line checks if the value of "input" is greater than 780.
If the condition is true, the "digitalWrite(LED, HIGH)" line turn ON the LED (pin 13).
If the condition is false, the "digitalWrite(LED, LOW)" line turns OFF the LED.