// Fnu Shoaib Ahmed, 652420658, fahme8
// Lab 4 - Photoresistor (LDR – Light Dependent Resistor)
// Description - In this lab we are trying to adjust a photo sensor where it tells us how bright is it in the area.
// Assumptions - I expected the potentiometer to work as increase and decrease the display brightness, and the wires to work correctly.
// References - I used the prelab links to setup my photoresistor on breadboard and the setup of potentiometer.
#include <LiquidCrystal.h>
// Initialize the LCD library with the pins (RS, E, D4, D5, D6, D7)
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int photoresistorPin = A0; // Pin where the photoresistor is connected
long previousMillis = 0;
void setup() {
lcd.begin(16, 2); // Set up the LCD with 16 columns and 2 rows
pinMode(photoresistorPin, INPUT); // Set the photoresistor pin as input
Serial.begin(9600); // Begin serial communication for debugging
}
void loop() {
int lightValue = analogRead(photoresistorPin); // Read the value from the photoresistor
// Map the lightValue to predefined text
String lightLevel;
if (lightValue < 50) {
lightLevel = "dark "; // Adding spaces to clear old characters
} else if (lightValue < 100) {
lightLevel = "partially dark";
} else if (lightValue < 200) {
lightLevel = "medium ";
} else if (lightValue < 400) {
lightLevel = "fully lit ";
} else {
lightLevel = "brightly lit ";
}
// Clear the LCD before writing new content
lcd.clear();
// Display the light level on the LCD
lcd.setCursor(0, 0); // Start at the first line
lcd.print("Light: ");
lcd.print(lightLevel);
// Display the time since reset on the second line
long currentMillis = millis(); // Get the current time
lcd.setCursor(0, 1); // Move to the second line
lcd.print("Time: ");
lcd.print(currentMillis);
delay(100); // Small delay to prevent flooding the LCD
}