1#include <LiquidCrystal.h>
2
3LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
4const int LDR_pin = A0;
5const int set_pot_pin = A1;
6const int led_light_pin = 9;
7
8
9int sensorValue = 0;
10int sensorMin = 1023;
11int sensorMax = 0;
12int threshold = 0;
13int light_intensity = 55, led_intensity = 20;
14void setup()
15{
16 lcd.begin(16, 4);
17 lcd.clear();
18 lcd.print("calibrating.....");
19 analogWrite(led_light_pin, 0);
20 digitalWrite(13, HIGH);
21 while (millis() < 10000)
22 {
23 sensorValue = analogRead(LDR_pin);
24 if (sensorValue > sensorMax) sensorMax = sensorValue;
25 if (sensorValue < sensorMin) sensorMin = sensorValue;
26 }
27 lcd.clear();
28 lcd.print("calibration over");
29 lcd.setCursor(0, 1);
30 lcd.print("max light:");
31 lcd.print(sensorMax);
32 lcd.setCursor(0, 2);
33 lcd.print("min light:");
34 lcd.print(sensorMin);
35 delay(2000);
36 lcd.clear();
37 lcd.print("light intensity");
38 lcd.setCursor(0, 2);
39 lcd.print("threshold:");
40 lcd.setCursor(0, 3);
41 lcd.print("LED light:0%");
42}
43
44void loop()
45{
46 sensorValue = analogRead(LDR_pin);
47 sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 100);
48 sensorValue = constrain(sensorValue,0,100);
49 lcd.setCursor(0, 1);
50 lcd.print(sensorValue);
51 lcd.print('%');
52 threshold = analogRead(set_pot_pin);
53 threshold = map(threshold, 0, 1023, 0, 100);
54 lcd.setCursor(11, 2);
55 lcd.print(threshold);
56 lcd.print('%');
57 if (sensorValue < threshold)
58 {
59 analogWrite(led_light_pin, light_intensity);
60 lcd.setCursor(11, 3);
61 lcd.print(led_intensity);
62 lcd.print('%');
63 if(light_intensity<255)
64 {
65 light_intensity += 25;
66 led_intensity += 10;
67 }
68 }
69 delay(2000);
70}
71
Anonymous user
5 years ago
wow am impressed