#include <LiquidCrystal.h>
// pin definitions
const int LCD_RS = 12, LCD_EN = 11, LCD_D4 = 5, LCD_D5 = 4, LCD_D6 = 3, LCD_D7 = 2;
const int PHOTO_PIN = A0, LDR_PIN = A1;
const int BUTTON_PIN = 7;
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
int displayMode = 0;
const int TOTAL_MODES = 3;
void setup() {
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
int photoValue = readPhotoResistor();
int ldrValue = readLDR();
updateLCD(photoValue, ldrValue);
sendSerialData(photoValue, ldrValue);
checkButtonPress();
delay(100);
}
int readPhotoResistor() {
int rawValue = analogRead(PHOTO_PIN);
return map(rawValue, 0, 1023, 0, 100);
}
int readLDR() {
int rawValue = analogRead(LDR_PIN);
return map(rawValue, 0, 1023, 0, 100);
}
void updateLCD(int photoValue, int ldrValue) {
lcd.clear();
switch (displayMode) {
case 0:
lcd.print("Photo: ");
lcd.print(photoValue);
lcd.print("%");
break;
case 1:
lcd.print("LDR: ");
lcd.print(ldrValue);
lcd.print("%");
break;
case 2:
lcd.print("Photo: ");
lcd.print(photoValue);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("LDR: ");
lcd.print(ldrValue);
lcd.print("%");
break;
}
}
void sendSerialData(int photoValue, int ldrValue) {
Serial.print("Photo: ");
Serial.print(photoValue);
Serial.print(",LDR:");
Serial.println(ldrValue);
}
void checkButtonPress() {
static bool lastButtonState = HIGH;
bool buttonState = digitalRead(BUTTON_PIN);
if (buttonState == LOW && lastButtonState == HIGH) {
displayMode = (displayMode+1) % TOTAL_MODES;
delay(50);
}
lastButtonState = buttonState;
}