ผมลองให้chat gpt เขียนให้ละได้ประมาณนี้
#include <Wire.h>
#include "MPU6050.h"
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
// โมดูล
MPU6050 mpu;
SoftwareSerial dfplayerSerial(10, 11); // RX, TX ของ Arduino
DFRobotDFPlayerMini dfplayer;
// เกณฑ์ก้มหน้า
const int16_t tiltThreshold = 8000; // ถ้าค่า Z ต่ำกว่า แปลว่าก้ม
// จับเวลาเพื่อป้องกันเล่นซ้ำ
unsigned long lastPlayTime = 0;
const unsigned long playCooldown = 5000; // 5 วินาทีก่อนเตือนใหม่
void setup() {
Wire.begin();
Serial.begin(9600);
dfplayerSerial.begin(9600);
// เริ่ม MPU6050
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed!");
while (1); // หยุดทำงาน
}
Serial.println("MPU6050 OK");
// เริ่ม DFPlayer
if (!dfplayer.begin(dfplayerSerial)) {
Serial.println("DFPlayer not found!");
while (1);
}
dfplayer.volume(25); // ระดับเสียง 0-30
Serial.println("DFPlayer OK");
}
void loop() {
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
Serial.print("az = ");
Serial.println(az);
unsigned long now = millis();
// ตรวจว่าก้มมาก และยังไม่ถึงเวลาเล่นใหม่
if (az < tiltThreshold && now - lastPlayTime > playCooldown) {
Serial.println("Head down detected → playing sound");
dfplayer.play(1); // เล่น 001.mp3
lastPlayTime = now;
}
delay(100); // หน่วงนิดเพื่อลดการอ่านถี่เกินไป
}
มันบอกไม่พบmpu 6050 ใครพอจะช่วยเเก้โค้ดให้ได้ไหม
วิธีการทำงานคือ ใช้เซนเซอร์mpu 6050 ตรวจจับว่าศีรษะก้มมากเกินไปสั่งdf player mini เล่นเสียงmp3
ใช้โปรเเกรมardunio ide
เขียนโค้ดโครงงานหมวกกันน็อคเตือนหลับในให้ผมหน่อย
ผมลองให้chat gpt เขียนให้ละได้ประมาณนี้
#include <Wire.h>
#include "MPU6050.h"
#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
// โมดูล
MPU6050 mpu;
SoftwareSerial dfplayerSerial(10, 11); // RX, TX ของ Arduino
DFRobotDFPlayerMini dfplayer;
// เกณฑ์ก้มหน้า
const int16_t tiltThreshold = 8000; // ถ้าค่า Z ต่ำกว่า แปลว่าก้ม
// จับเวลาเพื่อป้องกันเล่นซ้ำ
unsigned long lastPlayTime = 0;
const unsigned long playCooldown = 5000; // 5 วินาทีก่อนเตือนใหม่
void setup() {
Wire.begin();
Serial.begin(9600);
dfplayerSerial.begin(9600);
// เริ่ม MPU6050
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed!");
while (1); // หยุดทำงาน
}
Serial.println("MPU6050 OK");
// เริ่ม DFPlayer
if (!dfplayer.begin(dfplayerSerial)) {
Serial.println("DFPlayer not found!");
while (1);
}
dfplayer.volume(25); // ระดับเสียง 0-30
Serial.println("DFPlayer OK");
}
void loop() {
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
Serial.print("az = ");
Serial.println(az);
unsigned long now = millis();
// ตรวจว่าก้มมาก และยังไม่ถึงเวลาเล่นใหม่
if (az < tiltThreshold && now - lastPlayTime > playCooldown) {
Serial.println("Head down detected → playing sound");
dfplayer.play(1); // เล่น 001.mp3
lastPlayTime = now;
}
delay(100); // หน่วงนิดเพื่อลดการอ่านถี่เกินไป
}
มันบอกไม่พบmpu 6050 ใครพอจะช่วยเเก้โค้ดให้ได้ไหม
วิธีการทำงานคือ ใช้เซนเซอร์mpu 6050 ตรวจจับว่าศีรษะก้มมากเกินไปสั่งdf player mini เล่นเสียงmp3
ใช้โปรเเกรมardunio ide