Arduino学习笔记之:Arduino Uno + MPU6050 + 0.96 OLED

一.想要实现功能说明:

这里模拟的是最低级的落水警报求救手环。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

视频有很多地方不清晰,请见谅。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值