#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
// LCD settings
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust I2C address if necessary
// DHT22 settings
#define DHTPIN1 4 // Pin for the first DHT22 sensor
#define DHTPIN2 5 // Pin for the second DHT22 sensor
#define DHTTYPE DHT22
DHT dht1(DHTPIN1, DHTTYPE);
DHT dht2(DHTPIN2, DHTTYPE);
// TCRT5000, buzzer, and button pins
const int tcrt5000Pin = 2; // Digital pin for TCRT5000
const int resetButtonPin = 3; // Digital pin for reset button
const int pageSwitchButtonPin = 6; // Digital pin for page switch button
const int buzzerPin = 7; // Digital pin for the buzzer
int counter = 0; // Count detected events
bool previousState = HIGH; // Previous state of the TCRT5000 sensor
bool currentPage = false; // False = TCRT5000 page, True = DHT22 page
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Initialize DHT22 sensors
dht1.begin();
dht2.begin();
// Display initial TCRT5000 page
showTCRT5000Page();
// Initialize the pins
pinMode(tcrt5000Pin, INPUT_PULLUP);
pinMode(resetButtonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(pageSwitchButtonPin, INPUT_PULLUP); // Use internal pull-up resistor
pinMode(buzzerPin, OUTPUT); // Buzzer pin as output
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off initially
}
void loop() {
// Read the current state of the TCRT5000 sensor
bool currentState = digitalRead(tcrt5000Pin);
// Detect a transition from HIGH to LOW (light to dark)
if (previousState == HIGH && currentState == LOW) {
counter++; // Increment the counter
soundBuzzer(); // Sound the buzzer for 0.3 seconds at 1,000 Hz
if (!currentPage) {
updateTCRT5000Page();
}
}
// Check if the reset button is pressed
if (digitalRead(resetButtonPin) == LOW) {
counter = 0; // Reset the counter
if (!currentPage) {
updateTCRT5000Page();
}
delay(200); // Debounce delay
}
// Check if the page switch button is pressed
if (digitalRead(pageSwitchButtonPin) == LOW) {
currentPage = !currentPage; // Toggle the page
if (currentPage) {
showDHT22Page();
} else {
showTCRT5000Page();
}
delay(200); // Debounce delay
}
// Update the DHT22 page if it's active
if (currentPage) {
updateDHT22Page();
}
// Update the previous state of the TCRT5000 sensor
previousState = currentState;
// Small delay to debounce inputs
delay(50);
}
void showTCRT5000Page() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("TCRT5000 Counter:");
lcd.setCursor(0, 1);
lcd.print("Count: ");
lcd.setCursor(7, 1);
lcd.print(counter);
}
void updateTCRT5000Page() {
lcd.setCursor(7, 1);
lcd.print(" "); // Clear old value
lcd.setCursor(7, 1);
lcd.print(counter);
}
void showDHT22Page() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("S1: ");
lcd.setCursor(0, 1);
lcd.print("S2: ");
lcd.setCursor(0, 2);
lcd.print("Avg Temp: ");
lcd.setCursor(0, 3);
lcd.print("Avg Hum: ");
}
void updateDHT22Page() {
// Read temperature and humidity from both DHT22 sensors
float temp1 = dht1.readTemperature(true); // True for Fahrenheit
float hum1 = dht1.readHumidity();
float temp2 = dht2.readTemperature(true);
float hum2 = dht2.readHumidity();
// Calculate averages
float avgTemp = (temp1 + temp2) / 2.0;
float avgHum = (hum1 + hum2) / 2.0;
// Update LCD without flickering
lcd.setCursor(0, 0);
lcd.print("S1: ");
lcd.print(temp1, 1);
lcd.print("F ");
lcd.print(hum1, 1);
lcd.print("% "); // Overwrite leftover characters
lcd.setCursor(0, 1);
lcd.print("S2: ");
lcd.print(temp2, 1);
lcd.print("F ");
lcd.print(hum2, 1);
lcd.print("% "); // Overwrite leftover characters
lcd.setCursor(0, 2);
lcd.print("Avg Temp: ");
lcd.print(avgTemp, 1);
lcd.print("F ");
lcd.setCursor(0, 3);
lcd.print("Avg Hum: ");
lcd.print(avgHum, 1);
lcd.print("% ");
delay(1000); // Delay for real-time updates
}
void soundBuzzer() {
tone(buzzerPin, 1000); // Generate a 1,000 Hz tone on the buzzer pin
delay(300); // Play the tone for 0.3 seconds
noTone(buzzerPin); // Stop the tone
}