1
6
7
8const int ldrPin = A0;
9const int ledPin1 = 12;
10const int ledPin2 = 11;
11const int ledPin3 = 10;
12const int ledPin4 = 9;
13
14int ldrValue = 0;
15int ldrlevel1=600;
16int ldrlevel2=700;
17int ldrlevel3=750;
18int ldrlevel4=800;
19
20void setup() {
21 Serial.begin(9600);
22 pinMode(ledPin1, OUTPUT);
23 pinMode(ledPin2, OUTPUT);
24 pinMode(ledPin3, OUTPUT);
25 pinMode(ledPin4, OUTPUT);
26}
27
28void loop() {
29 ldrValue = analogRead(ldrPin);
30 Serial.println(ldrValue);
31 if (ldrValue < ldrlevel1) {
32 digitalWrite(ledPin4, HIGH);
33 }
34 else if (ldrValue < ldrlevel2) {
35 digitalWrite(ledPin4, LOW);
36 digitalWrite(ledPin3, HIGH);
37 }
38 else if (ldrValue < ldrlevel3) {
39 digitalWrite(ledPin3, LOW);
40 digitalWrite(ledPin2, HIGH);
41 }
42 else if (ldrValue < ldrlevel4) {
43 digitalWrite(ledPin2, LOW);
44 digitalWrite(ledPin1, HIGH);
45 }
46 else {
47 digitalWrite(ledPin1, LOW);
48 }
49 }
50
Anonymous user
5 years ago
Great tutorial! I can only offer one improvement, usually when using an acronym such as "LDR" the first instance of it should be preceded by the full word, followed in capital letters in parentheses the acronym to help those that don't know what it stands for. Example: Arduino Tutorial - Controlling LEDS with Light Dependant Resistor (LDR) Sensor