Advertisement
zoro-10

Light Sensitive Sensor

Apr 1st, 2024 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. https://imgur.com/a/s1T2HtL
  2.  
  3. int lightSensorValue = 0; // Variable to store the light sensor reading
  4. void setup()
  5. {
  6.   pinMode(A0, INPUT); // Set A0 pin as an input for the light sensor
  7.   pinMode(9, OUTPUT); // Set pin 9 as an output to control the LED
  8.   Serial.begin(9600); // Initialize serial communication at 9600 baud
  9. }
  10. void loop()
  11. {
  12.   lightSensorValue = analogRead(A0);                         // Read the light sensor value
  13.   Serial.println(lightSensorValue);                          // Print the sensor value to the Serial Monitor
  14.   int ledBrightness = map(lightSensorValue, 0, 1023, 0, 255); // Map the sensor value to LED brightness
  15.   analogWrite(9, ledBrightness);                             // Control LED brightness based on the sensor reading
  16.   delay(100);                                                // Wait for 100 milliseconds before the next loop iteration
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement