// ======================================================
// IOT MASTER TEMPLATE (CÓ GIẢI THÍCH)
// ======================================================
#include <Wire.h>
#include <DHT.h>
// ===== CẤU HÌNH =====
#define LED 2
#define BUTTON 3
#define PIR 4
#define BUZZER 5
#define DHTPIN 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
// ======================================================
// KỸ THUẬT 1: NON-BLOCKING (millis)
// TÁC DỤNG:
// - Thay delay()
// - Cho phép chạy nhiều tác vụ cùng lúc (LED + đọc nút + sensor)
// ======================================================
unsigned long previousMillis = 0;
int interval = 1000;
bool ledState = false;
void blinkNonBlocking(){
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
ledState = !ledState;
digitalWrite(LED, ledState);
}
}
// ======================================================
// KỸ THUẬT 2: DEBOUNCE BUTTON
// TÁC DỤNG:
// - Loại bỏ nhiễu khi nhấn nút (rung tín hiệu)
// - Tránh việc bấm 1 lần mà nhận nhiều lần
// ======================================================
int lastButtonState = HIGH;
int buttonState = HIGH;
unsigned long lastDebounceTime = 0;
int debounceDelay = 50;
bool readButton(){
int reading = digitalRead(BUTTON);
if(reading != lastButtonState){
lastDebounceTime = millis();
}
if((millis() - lastDebounceTime) > debounceDelay){
if(reading != buttonState){
buttonState = reading;
if(buttonState == LOW){
lastButtonState = reading;
return true; // chỉ trigger 1 lần
}
}
}
lastButtonState = reading;
return false;
}
// ======================================================
// KỸ THUẬT 3: PIR RETRIGGER
// TÁC DỤNG:
// - Giữ trạng thái ON thêm một thời gian sau khi phát hiện
// - Dùng trong hệ thống báo động
// ======================================================
unsigned long lastMotionTime = 0;
int timeout = 5000;
void handlePIR(){
if(digitalRead(PIR) == HIGH){
digitalWrite(LED, HIGH);
lastMotionTime = millis();
}
else{
if(millis() - lastMotionTime > timeout){
digitalWrite(LED, LOW);
}
}
}
// ======================================================
// KỸ THUẬT 4: PWM (analogWrite)
// TÁC DỤNG:
// - Điều chỉnh độ sáng LED
// - Điều khiển tốc độ motor
// ======================================================
void pwmExample(){
for(int i=0;i<=255;i+=5){
analogWrite(LED, i);
delay(10); // demo nên dùng delay
}
}
// ======================================================
// KỸ THUẬT 5: SERIAL CONTROL
// TÁC DỤNG:
// - Điều khiển thiết bị từ máy tính
// - Debug dữ liệu sensor
// ======================================================
void serialControl(){
if(Serial.available()){
char c = Serial.read();
if(c == 'O'){
digitalWrite(LED, HIGH);
}
else if(c == 'F'){
digitalWrite(LED, LOW);
}
}
}
// ======================================================
// KỸ THUẬT 6: ĐỌC DHT SENSOR
// TÁC DỤNG:
// - Lấy dữ liệu nhiệt độ và độ ẩm
// ======================================================
void readDHT(){
float t = dht.readTemperature();
float h = dht.readHumidity();
Serial.print("Temp: "); Serial.println(t);
Serial.print("Hum: "); Serial.println(h);
}
// ======================================================
// KỸ THUẬT 7: STATE MACHINE (LOGIC TRẠNG THÁI)
// TÁC DỤNG:
// - Dùng cho đèn giao thông, robot
// - Giúp code rõ ràng, dễ mở rộng
// ======================================================
int state = 0;
unsigned long stateTime = 0;
void trafficLight(){
switch(state){
case 0: // XANH
digitalWrite(LED, HIGH);
if(millis() - stateTime > 3000){
state = 1;
stateTime = millis();
}
break;
case 1: // VÀNG
if(millis() - stateTime > 2000){
state = 2;
stateTime = millis();
}
break;
case 2: // ĐỎ
if(millis() - stateTime > 5000){
state = 0;
stateTime = millis();
}
break;
}
}
// ======================================================
// SETUP
// ======================================================
void setup(){
pinMode(LED, OUTPUT);
pinMode(BUTTON, INPUT_PULLUP);
pinMode(PIR, INPUT);
pinMode(BUZZER, OUTPUT);
Serial.begin(115200);
dht.begin();
stateTime = millis();
}
// ======================================================
// LOOP
// ======================================================
void loop(){
blinkNonBlocking(); // chạy song song
handlePIR(); // PIR
if(readButton()){
Serial.println("Button pressed");
}
serialControl(); // điều khiển từ PC
static unsigned long dhtTime = 0;
if(millis() - dhtTime > 2000){
dhtTime = millis();
readDHT();
}
trafficLight(); // state machine
}