#include <LiquidCrystal_I2C.
h>
#include <SoftwareSerial.h>
// Pins
const int mq3Pin = A0; // MQ3 sensor pin
const int mq135Pin = A1; // MQ135 sensor pin
const int redLEDPin = 2; // Red LED pin
const int greenLEDPin = 3; // Green LED pin
const int blueLEDPin = 4; // Blue LED pin (Bluetooth ready)
const int buzzerPin = 5; // Buzzer pin
// Initialize LCD with I2C address 0x27 and 16x2 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
SoftwareSerial bluetooth(6, 7); // RX, TX pins for HC05 Bluetooth
float mq3Threshold = 0;
float mq135Threshold = 0;
void setup() {
// Initialize Serial Monitor
Serial.begin(9600);
// Initialize Bluetooth
bluetooth.begin(9600);
digitalWrite(blueLEDPin, HIGH); // Start with blue LED ON to indicate Bluetooth is ready
// Initialize LCD (correct initialization)
lcd.begin(16, 2); // Set LCD to 16 columns and 2 rows
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("INFINITY SOULS");
lcd.clear();
delay(2000);
lcd.setCursor(0, 1);
lcd.print("Food Spoilage");
lcd.setCursor(0, 1);
lcd.print("Detector");
delay(3000);
lcd.clear();
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
lcd.setCursor(0, 0);
lcd.print("MQ135 VAL:");
lcd.print(mq135Threshold);
lcd.setCursor(0, 1);
lcd.print("MQ3 VAL:");
lcd.print(mq3Threshold);
delay(3000);
digitalWrite(buzzerPin, HIGH);
delay(500);
digitalWrite(buzzerPin, LOW);
lcd.clear();
// Set pin modes
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(blueLEDPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
// Start with blue LED ON to indicate Bluetooth is ready
// Initial delay of 5 seconds
delay(5000);
// Get threshold values for MQ3 and MQ135 sensors
mq3Threshold = getThreshold(mq3Pin);
mq135Threshold = getThreshold(mq135Pin);
// After threshold calculation, turn off blue LED
digitalWrite(blueLEDPin, LOW);
}
float getThreshold(int sensorPin) {
float minVal = 1023;
float maxVal = 0;
unsigned long startTime = millis();
// Gather sensor data for 5 seconds to calculate threshold
while (millis() - startTime < 5000) {
int sensorVal = analogRead(sensorPin);
if (sensorVal < minVal) {
minVal = sensorVal;
}
if (sensorVal > maxVal) {
maxVal = sensorVal;
}
delay(100); // small delay for stability
}
// Calculate threshold as (max + min) / 2
return (minVal + maxVal) / 2;
}
void loop() {
// Read sensor values
int mq3Value = analogRead(mq3Pin);
int mq135Value = analogRead(mq135Pin);
// Display values on LCD
lcd.setCursor(0, 0);
lcd.print("MQ135:");
lcd.print(mq135Value);
lcd.setCursor(0, 1);
lcd.print("MQ3:");
lcd.print(mq3Value);
// Send values to Serial Monitor and Bluetooth
Serial.print("MQ135: ");
Serial.print(mq135Value);
Serial.print(" | MQ3: ");
Serial.println(mq3Value);
bluetooth.print("MQ135: ");
bluetooth.print(mq135Value);
bluetooth.print(" | MQ3: ");
bluetooth.println(mq3Value);
// Check if either sensor crosses threshold
if (mq135Value > mq135Threshold + 5 && mq3Value > mq3Threshold + 5) {
// Food is spoiled
digitalWrite(redLEDPin, HIGH); // Turn on red LED
digitalWrite(greenLEDPin, LOW); // Turn off green LED
digitalWrite(buzzerPin, HIGH); // Turn on buzzer
delay(1000);
digitalWrite(buzzerPin, LOW);
digitalWrite(redLEDPin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("FOOD SPOILED!");
bluetooth.println("FOOD SPOILED!");
Serial.println("Rotten Food");
} else {
// Food is fresh
digitalWrite(redLEDPin, LOW); // Turn off red LED
digitalWrite(greenLEDPin, HIGH); // Turn on green LED
digitalWrite(buzzerPin, LOW);
digitalWrite(greenLEDPin, LOW);// Turn off buzzer
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("FOOD FRESH!");
bluetooth.println("FOOD FRESH!");
Serial.println("Fresh Food");
}
delay(1000); // Delay for stability
}