0% found this document useful (0 votes)
14 views

code

Uploaded by

ducmanh9a219
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

code

Uploaded by

ducmanh9a219
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Code :

#include <Adafruit_Sensor.h>
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>

// --- Chân cảm biến và phần cứng ---


#define DHT_PIN 4
#define LED1_PIN 2
#define BUZZER_PIN 15
#define BTN1_PIN 12
#define BTN2_PIN 13
#define LED2_PIN 32
#define BUTTON_SEM_PIN 25
#define SDA_PIN 21
#define SCL_PIN 22
#define DHTTYPE DHT22 // Change this to DHT22

LiquidCrystal_I2C lcd(0x27, 16, 2);


DHT dht(DHT_PIN, DHTTYPE); // Initialize DHT22 sensor here

SemaphoreHandle_t SEM_handle;
TimerHandle_t TIMER_handle;

float temperature = 0.0;


float humidity = 0.0;
int mqValue = 0;
int SemCount = 0;
int SemLimit = 0;

// Task 2: LCD Task


void vTimerLCDCallback(TimerHandle_t xTimer) {
// Đọc cảm biến DHT22
temperature = dht.readTemperature(); // Reading from DHT22
humidity = dht.readHumidity(); // Reading from DHT22

Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print("C / Humidity: ");
Serial.print(humidity);
Serial.println("%");

// Hiển thị lên LCD


lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humid: ");
lcd.print(humidity);
lcd.print("%");
}

// Task 3: Buzzer Task


void taskBuzzer(void *pvParameters) {
while (1) {
// Jigel Bell âm thanh tần sốcao
tone(BUZZER_PIN, 1000); // Buzzer phát ra tầ n số1kHz
vTaskDelay(pdMS_TO_TICKS(200)); // Delay ngắ n đểphát 1 nố t
noTone(BUZZER_PIN); // Tắ t buzzer
vTaskDelay(pdMS_TO_TICKS(100)); // Thời gian nghỉ giữa các nốt
tone(BUZZER_PIN, 1200); // Nốt cao hơn 1 chút
vTaskDelay(pdMS_TO_TICKS(200)); // Delay ngắ n
noTone(BUZZER_PIN); // Tắ t buzzer
vTaskDelay(pdMS_TO_TICKS(100)); // Thời gian nghỉ giữa các nốt
}
}
// Task 4: Button-Led Task
void taskButtonControlLED(void *pvParameters) {
while (1) {
if (digitalRead(BTN1_PIN) == LOW) {
digitalWrite(LED1_PIN, HIGH); // Bật LED
}
if (digitalRead(BTN2_PIN) == LOW) {
digitalWrite(LED1_PIN, LOW); // Tắ t LED
}
vTaskDelay(pdMS_TO_TICKS(50)); // Kiể m tra nút bấm mỗi 50ms
}
}

// Task 5: Button Semaphore Task


void taskSemaphoreButton(void *pvParameters) {
while (1) {
if (digitalRead(BUTTON_SEM_PIN) == LOW && SemLimit < 4) {
xSemaphoreGive(SEM_handle);
SemCount++;
SemLimit++;
Serial.println("Semaphore cấp!");
vTaskDelay(pdMS_TO_TICKS(300)); // Anti-bounce
}
vTaskDelay(pdMS_TO_TICKS(50)); // Kiểm tra nút bấm
}
}

// Task 6: Led Blink Task


void taskBlinkLED(void *pvParameters) {
while (1) {
xSemaphoreTake(SEM_handle, portMAX_DELAY);
SemCount--;
if (SemCount == 0) {
SemLimit = 0;
}
for (int i = 0; i < 3; i++) { // Chớp 3 lần
digitalWrite(LED2_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(500));
digitalWrite(LED2_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
}

// --- Setup ---


void setup() {
Serial.begin(115200);
pinMode(BTN1_PIN, INPUT_PULLUP);
pinMode(BTN2_PIN, INPUT_PULLUP);
pinMode(BUTTON_SEM_PIN, INPUT_PULLUP);
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);

pinMode(BUZZER_PIN, OUTPUT);
// Khởi tạo LCD
Wire.begin(SDA_PIN, SCL_PIN);
lcd.begin(16, 2);
lcd.setBacklight(255);
lcd.setCursor(0, 0);
lcd.print("Initializing...");
dht.begin();
// Tạo Semaphore
SEM_handle = xSemaphoreCreateCounting(5, 0); // Tối đa 5 Semaphore
// Tạo Timer cho LCD
TIMER_handle = xTimerCreate("LCD Timer", pdMS_TO_TICKS(1000), pdTRUE,
0, vTimerLCDCallback);
xTimerStart(TIMER_handle, 0);
// Tạo các task
xTaskCreate(taskButtonControlLED, "Control LED", 1024, NULL, 2,
NULL);
xTaskCreate(taskSemaphoreButton, "Semaphore Button", 1024, NULL, 2,
NULL);
xTaskCreate(taskBlinkLED, "Blink LED", 1024, NULL, 1, NULL);
xTaskCreate(taskBuzzer, "Buzzer", 1024, NULL, 1, NULL);
}

// --- Loop ---


void loop() {
// Không dùng gì ở đây, FreeRTOS quản lý task
}

You might also like