一.想要实现功能说明:
这里模拟的是最低级的落水警报求救手环。MPU6050用于打印x,y,z的距离,间隔时间为两秒一次,并指定一定范围的x,y,z距离限制,只要在这个限制中出现10次则提示出人已经落水,请迅速救援的信息,当检测到x,y,z距离已经脱离了落水范畴,则提示人已经救出,重新开始进行间隔时间为两秒一次的x,y,z的距离打印。0.96 OLED模块进行手环界面设计。
二.代码实现:
#include <Wire.h>
#include <MPU6050.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
MPU6050 mpu;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Threshold values for each axis (adjust as needed)
const int16_t thresholdX = 1000; // Example threshold for x-axis
const int16_t thresholdY = 1000; // Example threshold for y-axis
const int16_t thresholdZ = 1000; // Example threshold for z-axis
boolean rescued = false; // Flag to indicate if the person has been rescued
void setup() {
Serial.begin(9600);
Wire.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor((SCREEN_WIDTH - 12 * 6) / 2, 0);
display.println("Water Alarm and Rescue Bracelet");
display.setCursor((SCREEN_WIDTH - 11 * 6) / 2, 20);
display.println("Normal Status...");
display.display();
// Initialize MPU6050
mpu.initialize();
mpu.setDLPFMode(0); // Disable DLPF for faster response
}
void loop() {
int16_t ax, ay, az;
mpu.getAcceleration(&ax, &ay, &az);
// Print sensor data if the person is not rescued
if (!rescued) {
Serial.print("AccelX:");
Serial.print(ax);
Serial.print(", AccelY:");
Serial.print(ay);
Serial.print(", AccelZ:");
Serial.println(az);
}
// Check if the sensor readings indicate a person has fallen into water
if (ax < thresholdX && ay < thresholdY && az < thresholdZ) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Help Alarm");
display.display();
// Print rescue information and update rescued flag
Serial.println("Urgent: Person has fallen into water! Rescue needed!");
Serial.print("Location: ");
// Print location information here
rescued = true; // Set rescued flag to true
} else {
// If the person is rescued, print rescue message
if (rescued) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Rescue Completed");
display.display();
Serial.println("Person has been rescued!");
// Print location information here
rescued = false; // Reset rescued flag
}
}
// Delay for 2 seconds between normal printing
delay(2000);
// Clear OLED display and show normal status
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(1);
display.setCursor((SCREEN_WIDTH - 12 * 6) / 2, 0);
display.println("Water Alarm and Rescue Bracelet");
display.setCursor((SCREEN_WIDTH - 11 * 6) / 2, 20);
display.println("Normal Status...");
display.display();
}
三.硬件连接:
四.
硬件实现-CSDN直播无https://live.csdn.net/v/374889
五.
串口监视器实现-CSDN直播无https://live.csdn.net/v/374890
视频有很多地方不清晰,请见谅。