微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

蓝牙模块加速度计

如何解决蓝牙模块加速度计

我有一个项目要为学校做:拳击手的俘虏。这将是 我必须将 Arduino nano 33 ble Sense 与蓝牙连接。 这个想法是将加速度计的数据传输到带有应用程序的智能手机(可能使用麻省理工学院的应用程序 Inventor 制作)。 因此,向运动员展示他的击球力量(加速度--)力量--)力量)。 但我没有设法将我的 arduino 连接到我的智能手机。事实上,我按照教程来连接它。事实上,我成功地将 arduino 与 nFr connect 连接起来,但我什么也做不了。

有问题的代码(用于蓝牙 nFr 连接)在这里:(这只是一个例子,因为我的最终目标是使用加速度计来做到这一点)

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>

BLEService dataService("180C"); // User defined service

BLEStringCharacteristic dataCharacteristic("2A56",// standard 16-bit characteristic UUID
    BLERead | BLENotify,13); // remote clients will be able to read and subscribe to notifications

int oldValue = 0;  // last value
long prevIoUsMillis = 0;  // last time the value was checked,in ms

void setup()
{
    Serial.begin(9600); // initialize serial communication
    while (!Serial)
        ;

    pinMode(LED_BUILTIN,OUTPUT); // initialize the built-in LED pin

    if (!BLE.begin()) { // initialize BLE
        Serial.println("starting BLE Failed!");
        while (1)
            ;
    }

    BLE.setLocalName("Sac de frappe"); // Set name for connection
    BLE.setAdvertisedService(dataService); // Advertise service
    dataService.addCharacteristic(dataCharacteristic); // Add characteristic to service
    BLE.addService(dataService); // Add service
    dataCharacteristic.setValue(String(oldValue)); // Set data string

    BLE.advertise(); // Start advertising
    Serial.print("Peripheral device MAC: ");
    Serial.println(BLE.address());
    Serial.println("Waiting for connections...");

  Serial.begin(9600);
  while (!Serial);
  Serial.println("Started");

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }

  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println(" Hz");
  Serial.println();
  Serial.println("acceleration in G's");
  Serial.println("X\tY\tZ");
}

void loop()
{
    BLEDevice central = BLE.central(); // Wait for a BLE central to connect

    // if a central is connected to the peripheral:
    if (central) {
        Serial.print("Connected to central MAC: ");
        // print the central's BT address:
        Serial.println(central.address());
        // turn on the LED to indicate the connection:
        digitalWrite(LED_BUILTIN,HIGH);

        // update value every 200ms
        // while the central is connected:
        while (central.connected()) {
            long currentMillis = millis();
            // if 200ms have passed,update value:
            if (currentMillis - prevIoUsMillis >= 200) {
                prevIoUsMillis = currentMillis;
                updateValue();
            }
        }
    }
    // when the central disconnects,turn off the LED:
    digitalWrite(LED_BUILTIN,LOW);
    Serial.print("disconnected from central MAC: ");
    Serial.println(central.address());
     float x,y,z;

  if (IMU.accelerationAvailable()) {
    IMU.readacceleration(x,z);

    Serial.print(x);
    Serial.print('\t');
    Serial.print(y);
    Serial.print('\t');
    Serial.println(z);
  }

}

void updateValue() {
    int value = analogRead(A0); // Read your accelerometer data here

    if (value != oldValue) {
        Serial.print("Accelerometer Data is Now: "); // print it
        Serial.println(value);
        dataCharacteristic.writeValue(String(value));  // update value
        // save the level for next comparison
        oldValue = value;
    }
}    
   

感谢您的帮助(对不起我的英语)

解决方法

已编辑以匹配有问题的已编辑代码

要传输加速度计数据,您可以执行类似于 BatteryMonitor example 的操作:

在您的特征上使用通知,让您的智能手机无需经常手动阅读即可接收更改。在中央连接时不断读取和更新加速度计数据。

#include <ArduinoBLE.h>
#include <Arduino_LSM9DS1.h>

BLEService dataService("180C"); // User defined service

BLEStringCharacteristic dataCharacteristic("2A56",// standard 16-bit characteristic UUID
    BLERead | BLENotify,50); // remote clients will be able to read and subscribe to notifications

float oldX = 0.0;  // last value
float oldY = 0.0;
float oldZ = 0.0;
long previousMillis = 0;  // last time the value was checked,in ms

void setup()
{
    Serial.begin(9600); // initialize serial communication
    while (!Serial)
        ;

    pinMode(LED_BUILTIN,OUTPUT); // initialize the built-in LED pin

    if (!BLE.begin()) { // initialize BLE
        Serial.println("starting BLE failed!");
        while (1)
            ;
    }

    BLE.setLocalName("Sac de frappe"); // Set name for connection
    BLE.setAdvertisedService(dataService); // Advertise service
    dataService.addCharacteristic(dataCharacteristic); // Add characteristic to service
    BLE.addService(dataService); // Add service
    dataCharacteristic.setValue(""); // Set initial value

    BLE.advertise(); // Start advertising
    Serial.print("Peripheral device MAC: ");
    Serial.println(BLE.address());
    Serial.println("Waiting for connections...");

    Serial.begin(9600);
    while (!Serial);
    Serial.println("Started");

    if (!IMU.begin()) {
        Serial.println("Failed to initialize IMU!");
        while (1);
    }

    Serial.print("Accelerometer sample rate = ");
    Serial.print(IMU.accelerationSampleRate());
    Serial.println(" Hz");
    Serial.println();
    Serial.println("Acceleration in G's");
    Serial.println("X\tY\tZ");
}

void loop()
{
    BLEDevice central = BLE.central(); // Wait for a BLE central to connect

    // if a central is connected to the peripheral:
    if (central) {
        Serial.print("Connected to central MAC: ");
        // print the central's BT address:
        Serial.println(central.address());
        // turn on the LED to indicate the connection:
        digitalWrite(LED_BUILTIN,HIGH);

        // update value every 200ms
        // while the central is connected:
        while (central.connected()) {
            long currentMillis = millis();
            // if 200ms have passed,update value:
            if (currentMillis - previousMillis >= 200) {
                previousMillis = currentMillis;
                updateValue();
            }
        }
    }
    // when the central disconnects,turn off the LED:
    digitalWrite(LED_BUILTIN,LOW);
    Serial.print("Disconnected from central MAC: ");
    Serial.println(central.address());
}

void updateValue() {
    float x,y,z;

    if (!IMU.accelerationAvailable()) return; // Return if not ready
    IMU.readAcceleration(x,z); // Read new data

    if (x != oldX || y != oldY || z != oldZ) {
        // print it
        Serial.print(x);
        Serial.print('\t');
        Serial.print(y);
        Serial.print('\t');
        Serial.println(z);
        dataCharacteristic.writeValue("X: " + String(x) + " Y: " + String(y) + " Z: " + String(z));  // update value
        // save the value for next comparison
        oldX = x;
        oldY = y;
        oldZ = z;
    }
}

您可以使用 nRF Connect 验证功能。如果一切正常,您可以继续开发您自己的应用程序。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?