Into this section it is explained with the functioning of the LED RGB with a LDR in ESP-8266-MOD.
The following video is the simulation made in Tinkercad of the LED RGB with a LDR.
1 /* Created on 2022 2 * Last modification: 2nd May 2022 3 * by Villares 4 */ 5 6 const int RedLED = D6; // Red leg connected to D6 pin 7 const int GreenLED = D5; // Green leg connected to D5 pin 8 const int BlueLED = D3; // Blue leg connected to D3 pin 9 // RGB LED uses 3 outputs to control 3 basic colours 10 const int pinLDR = A0; // LDR connected to A0 pin 11 12 void setup() { // the setup routine runs once when you press reset
13 pinMode(RedLED, OUTPUT); // Configures RedLED pin to behave as an output
14 pinMode(GreenLED, OUTPUT); // Configures GreenLED pin to behave as an output
15 pinMode(BlueLED, OUTPUT); // Configures BlueLED pin to behave as an output
16 analogReference(EXTERNAL); // the voltage applied to the AREF pin (0 to 5V only) is used as the reference
17 }
18
19 void loop() { // the loop routine runs over and over again forever
20 int LDRvalue = analogRead (pinLDR); // the loop routine runs over and over again forever
21 if(LDRvalue >= 1023) { // The conditional if-else structure controls colour corresponding to light intensity
22 digitalWrite(RedLED, 128); // between the brackets of this function there are: the Arduino pin number and the value which corresponds to Colour Intensity
23 digitalWrite(GreenLED, 0);
24 digitalWrite(BlueLED, 0);
25 }
25 else if((LDRvalue >= 959) & (LDRvalue < 1023)) {
26 digitalWrite(RedLED, 255);
27 digitalWrite(GreenLED, 0);
28 digitalWrite(BlueLED, 0);
29 }
30 else if((LDRvalue >= 895) & (LDRvalue < 959)) {
31 digitalWrite(RedLED, 255);
32 digitalWrite(GreenLED, 128);
33 digitalWrite(BlueLED, 0);
34 }
35 else if((LDRvalue >= 767) & (LDRvalue < 831)) {
36 digitalWrite(RedLED, 255);
37 digitalWrite(GreenLED, 255);
38 digitalWrite(BlueLED, 0);
39 }
40 else if((LDRvalue >= 703) & (LDRvalue < 767)) {
41 digitalWrite(RedLED, 128);
42 digitalWrite(GreenLED, 255);
43 digitalWrite(BlueLED, 255);
44 }
45 else if((LDRvalue >= 639) & (LDRvalue < 703)) {
46 digitalWrite(RedLED, 128);
47 digitalWrite(GreenLED, 128);
48 digitalWrite(BlueLED, 255);
49 }
50 else if((LDRvalue >= 575) & (LDRvalue < 639)) {
51 digitalWrite(RedLED, 0);
52 digitalWrite(GreenLED, 128);
53 digitalWrite(BlueLED, 255);
54 }
55 else if((LDRvalue >= 511) & (LDRvalue < 575)) {
56 digitalWrite(RedLED, 0);
57 digitalWrite(GreenLED, 0);
58 digitalWrite(BlueLED, 255);
59 }
60 else if((LDRvalue >= 447) & (LDRvalue < 511)) {
61 digitalWrite(RedLED, 0);
62 digitalWrite(GreenLED, 0);
63 digitalWrite(BlueLED, 128);
64 }
65 else if((LDRvalue >= 383) & (LDRvalue < 447)) {
66 digitalWrite(RedLED, 0);
67 digitalWrite(GreenLED, 128);
68 digitalWrite(BlueLED, 0);
69 }
70 else if((LDRvalue >= 319) & (LDRvalue < 383)) {
71 digitalWrite(RedLED, 0);
72 digitalWrite(GreenLED, 255);
73 digitalWrite(BlueLED, 0);
74 }
75 else if((LDRvalue >= 255) & (LDRvalue < 319)) {
76 digitalWrite(RedLED, 128);
77 digitalWrite(GreenLED, 255);
78 digitalWrite(BlueLED, 0);
79 }
80 else if((LDRvalue >= 191) & (LDRvalue < 255)) {
81 digitalWrite(RedLED, 0);
82 digitalWrite(GreenLED, 255);
83 digitalWrite(BlueLED, 128);
84 }
85 else if((LDRvalue >= 127) & (LDRvalue < 191)) {
86 digitalWrite(RedLED, 128);
87 digitalWrite(GreenLED, 255);
88 digitalWrite(BlueLED, 128);
89 }
90 else if((LDRvalue >= 63) & (LDRvalue < 127)) {
91 digitalWrite(RedLED, 128);
92 digitalWrite(GreenLED, 128);
93 digitalWrite(BlueLED, 128);
94 }
95 else if((LDRvalue >=0) & (LDRvalue < 63)) {
96 digitalWrite(RedLED, 55);
97 digitalWrite(GreenLED, 55);
98 digitalWrite(BlueLED, 55);
99 }
100 else {
101 digitalWrite(RedLED, 0);
102 digitalWrite(GreenLED, 0);
103 digitalWrite(BlueLED, 0);
104 }
105 }
106
107 void color(int red, int green,int blue) {
108 analogWrite(RedLED, 255-red);
109 analogWrite(GreenLED, 255-green);
110 analogWrite(BlueLED, 255-blue);
111 }
112