#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define ledbuzz 7
#define Sensor_smoke A0
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
#define MY_BLUE_COLOR SSD1306_WHITE // Change to SSD1306_BLACK for dark text on blue background
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
pinMode(ledbuzz, OUTPUT);
pinMode(Sensor_smoke, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println(F("DLD Project: Natural Gas Detector"));
display.display();
delay(3000);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
display.println(F("Made by Munawar Abbas "));
display.println(F("and Muhammad Saeed "));
display.display();
delay(3000);
}
void loop() {
digitalWrite(ledbuzz, LOW);
display.clearDisplay();
display.setTextSize(3);
display.setCursor(0, 0);
display.setTextColor(SSD1306_WHITE);
drawHappyFace();
display.display();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
float temp = analogRead(Sensor_smoke);
float temp2 = temp * (5.0 / 1023.0);
// Serial output of gas sensor reading
Serial.print("Gas Value: ");
Serial.println(temp); // Serial print the raw sensor value
if (temp > 150) {
// Blink the LED and beep the buzzer rapidly
while (temp > 150) { // Run loop until temp is above 70
for (int i=0;i<10;i++){
digitalWrite(ledbuzz, HIGH);
delay(100);
digitalWrite(ledbuzz, LOW);
delay(50);
}
digitalWrite(ledbuzz, HIGH);
display.clearDisplay();
display.setTextColor(MY_BLUE_COLOR);
display.println(F("Gas leak detected!"));
display.setCursor(0, 10);
display.print(F("Gas value: "));
display.print(temp);
display.display();
// Wait for a short time before checking temperature again
delay(100);
// Update temperature value
temp = analogRead(Sensor_smoke);
temp2 = temp * (5.0 / 1023.0);
}
// Clear the display and turn off the LED and buzzer once the temperature is below 70
digitalWrite(ledbuzz, LOW);
display.clearDisplay();
} else {
display.setTextColor(MY_BLUE_COLOR); // Using the defined blue color
display.println(F("No gas leak detected"));
drawHappyFace();
display.display();
}
}
void drawHappyFace() {
display.setCursor(32, 10);
display.print(F(":)"));
display.setCursor(32, 30);
display.print(F(" "));
delay(1000);
}