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

code

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

code

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

#include <WiFi.

h>
#include <FS.h>
#include <SD_MMC.h>
#include <TFT_eSPI.h>
#include <esp_camera.h>

// Cấu hình chân cho ESP32-CAM


#define PWDN_GPIO_NUM -1
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 0
#define SIOD_GPIO_NUM 26
#define SIOC_GPIO_NUM 27

#define Y9_GPIO_NUM 35
#define Y8_GPIO_NUM 34
#define Y7_GPIO_NUM 39
#define Y6_GPIO_NUM 36
#define Y5_GPIO_NUM 21
#define Y4_GPIO_NUM 19
#define Y3_GPIO_NUM 18
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 25
#define HREF_GPIO_NUM 23
#define PCLK_GPIO_NUM 22

#define HB100_PIN 32 // Chân nối tín hiệu từ HB100

TFT_eSPI tft = TFT_eSPI(); // Khởi tạo màn hình TFT

volatile unsigned long lastPulseTime = 0;


volatile unsigned long pulseInterval = 0;
float speed = 0.0; // Tốc độ xe tính bằng km/h
const float SPEED_LIMIT = 50.0; // Ngưỡng tốc độ (km/h)

void IRAM_ATTR handleHB100Interrupt() {


unsigned long currentTime = micros();
if (lastPulseTime != 0) {
pulseInterval = currentTime - lastPulseTime;
speed = 2.7 * (1000000.0 / pulseInterval); // Công thức tính tốc độ từ HB100
}
lastPulseTime = currentTime;
}

void setupCamera() {
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = 20000000;
config.pixel_format = PIXFORMAT_JPEG;

config.frame_size = FRAMESIZE_UXGA;
config.jpeg_quality = 10;
config.fb_count = 1;

if (!esp_camera_init(&config)) {
Serial.println("Camera init failed");
return;
}
}

void savePhotoToSD() {
camera_fb_t *fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Camera capture failed");
return;
}

File file = SD_MMC.open("/photo.jpg", FILE_WRITE);


if (!file) {
Serial.println("Failed to open file in write mode");
esp_camera_fb_return(fb);
return;
}
file.write(fb->buf, fb->len);
file.close();
esp_camera_fb_return(fb);

Serial.println("Photo saved to SD card");


}
void setup() {
Serial.begin(115200);
pinMode(HB100_PIN, INPUT);
attachInterrupt(digitalPinToInterrupt(HB100_PIN), handleHB100Interrupt, RISING);

// Khởi tạo SD card


if (!SD_MMC.begin()) {
Serial.println("SD Card Mount Failed");
return;
}

// Khởi tạo camera


setupCamera();

// Khởi tạo màn hình TFT


tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE);/-strong/-heart:>:o:-((:-h tft.setTextSize(2);

tft.println("System Ready...");
}

void loop() {
tft.fillScreen(TFT_BLACK);
tft.setCursor(0, 0);

tft.print("Speed: ");
tft.print(speed);
tft.println(" km/h");

if (speed > SPEED_LIMIT) {


tft.setTextColor(TFT_RED);
tft.println("Over Speed!");
savePhotoToSD();
} else {
tft.setTextColor(TFT_GREEN);
tft.println("Safe Speed.");
}

delay(500);
}

You might also like