This system turns on 4 LEDs when the ambient brightness is low, and turns off them when ambient brightness is high.
CodeIn the beginning of the code, we define the variables...
int ledPin1 = A1;
int ledPin2 = A2;
int ledPin3 = A3;
int ledPin4 = A4;
int value;
int light;
Then we define the number of baud ( here 9600 ).
void setup() {
Serial.begin(9600);
}
In the void loop, we associate "value" to the reading of pin A0...
void loop() {
value = analogRead(A0);
And we copy the analog value on the serial monitor.
Serial.println("Analog Value : ");
Serial.println(value);
We compare the analog value coming from the photoresistor and the current that can be released for the LED. "Light" is our value that defines the current intensity issued for the LED.
light = map(value, 0, 250, 255, 0);
Finally, we issued current for LEDs with "light" intensity.
analogWrite(ledPin1, light);
analogWrite(ledPin2, light);
analogWrite(ledPin3, light);
analogWrite(ledPin4, light);
Comments