蓝牙模块加速度计 [英] bluetooth module accelerometer
问题描述
我有一个上学的项目:拳击手的俘虏.这将是我必须将Arduino nano 33 ble Sense与蓝牙连接.这个想法是通过一个应用程序(可能是MIT应用程序Inventor制作的)将加速度计的数据传输到智能手机.因此,向运动员展示击球的力量与作用力(加速度-力量-)的力量).但是我没有将arduino连接到我的smarthphone.确实,我遵循一个tuto来连接它.实际上,我成功地将arduino与nFr connect连接起来,但我无能为力.
I have a project to do for school: a captor for boxer. It will be i have to connect an Arduino nano 33 ble Sense with the Bluetooth. The idea is to transfer data of accelerometer to a smartphone with an application (maybe made with MIT app Inventor). And so show to an athlete the power of his hit with the application (accelration --) force ---) power). But i didn't manage to connect my arduino to my smarthphone. Indeed, i follow a tuto to connect it. In fact, I succeed in connecting the arduino with nFr connect but I can't do anything.
有问题的代码(用于蓝牙nFr连接)在这里:(这只是一个示例,因为我的最终目标是使用加速度计来完成此操作)
the code (for bluetooth nFr connect) in question is here : (its just an example because my final goal is to do this but with the accelerometer)
#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, y, 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;
}
}
感谢您的帮助(对不起我的英语)
thanks for helping (sorry for my english)
推荐答案
已进行编辑以匹配所讨论的已编辑代码
要传输加速度计数据,您可以执行与电池监视器示例:
To transfer your accelerometer data you could do something similar to the BatteryMonitor example:
使用有关特征的通知,使智能手机无需不断手动阅读即可接收更改.连接中央控制器后,不断读取并更新加速度计数据.
Use notifications on your characteristic to allow your smartphone to receive changes without constantly reading manually. Readout and update accelerometer data constantly while central is connected.
#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, y, 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验证功能.如果一切正常,您将继续开发自己的应用.
You can validate the functionality using nRF Connect. If everything works how you like it proceed to develop you own app.
这篇关于蓝牙模块加速度计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!