T

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include <LiquidCrystal.

h>

// Khai báo chân kết nối


const int gasSensorPin = A0; // Chân analog kết nối với cảm biến khí gas
const int ledPin = 13; // Chân digital kết nối với đèn LED
const int speakerPin = 9; // Chân digital kết nối với loa

// Khởi tạo đối tượng LiquidCrystal


const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

// Khai báo biến và hằng số


const int threshold = 500;
unsigned long previousMillis = 0; // Thời điểm trước đó
const unsigned long interval = 2000; // Khoảng thời gian giữa các lần cập nhật
(ms)

void setup() {
// Khởi động Serial Plotter
Serial.begin(9600);
while (!Serial) {
; // Đợi kết nối Serial Plotter
}
//Thiết lập lcd
lcd.begin(16, 2);
// Thiết lập chân kết nối
pinMode(ledPin, OUTPUT);
pinMode(speakerPin, OUTPUT);
delay(1000);
}

void loop() {
unsigned long currentMillis = millis();

// Đọc giá trị từ cảm biến khí gas


int gasValue = analogRead(gasSensorPin);

// Cập nhật Serial Plotter


if (currentMillis - previousMillis >= interval) {
// Gửi giá trị khí gas đến Serial Plotter
Serial.print(gasValue);
Serial.println();

previousMillis = currentMillis;
}
if (gasValue > threshold) {
lcd.setCursor(2, 0);
lcd.print("WARNING!!!");
lcd.setCursor(0, 1);
lcd.print("Chay ngay di !!!");
digitalWrite(ledPin, HIGH); // Bật đèn LED
tone(speakerPin, 1000); // Phát âm thanh từ loa
} else {
digitalWrite(ledPin, LOW); // Tắt đèn LED
noTone(speakerPin); // Tắt âm thanh
}
lcd.clear();
}

You might also like