Nano 33 BLE Sense Rev2需要下载的程序
#include <ArduinoBLE.h>
#include "Arduino_BMI270_BMM150.h"
float x, y, z;
int degreesX = 0;
int degreesY = 0;
BLEService ledService("19B10010-E8F2-537E-4F6C-D104768A1214"); // create service
// create button characteristic and allow remote device to get notifications
BLEByteCharacteristic buttonCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
void setup() {
Serial.begin(9600);
while (!Serial);
if (!IMU.begin()) {
Serial.println("Failed to initialize IMU!");
while (1);
}
Serial.print("Accelerometer sample rate = ");
Serial.print(IMU.accelerationSampleRate());
Serial.println(" Hz");
// begin initialization
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
// set the local name peripheral advertises
BLE.setLocalName("ButtonLED");
// set the UUID for the service this peripheral advertises:
BLE.setAdvertisedService(ledService);
// add the characteristics to the service
ledService.addCharacteristic(buttonCharacteristic);
// add the service
BLE.addService(ledService);
buttonCharacteristic.writeValue(0);
// start advertising
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
// poll for BLE events
BLE.poll();
buttonCharacteristic.writeValue(0);
if (IMU.accelerationAvailable()) {
IMU.readAcceleration(x, y, z);
if(x > 0.1){
x = 100*x;
degreesX = map(x, 0, 97, 0, 90);
Serial.print("Tilting up ");
Serial.print(degreesX);
Serial.println(" degrees");
buttonCharacteristic.writeValue(degreesX);
}
if(x < -0.1){
x = 100*x;
degreesX = map(x, 0, -100, 0, 90);
Serial.print("Tilting down ");
Serial.print(degreesX);
Serial.println(" degrees");
buttonCharacteristic.writeValue(degreesX);
}
if(y > 0.1){
y = 100*y;
degreesY = map(y, 0, 97, 0, 90);
Serial.print("Tilting left ");
Serial.print(degreesY);
Serial.println(" degrees");
buttonCharacteristic.writeValue(degreesY);
}
if(y < -0.1){
y = 100*y;
degreesY = map(y, 0, -100, 0, 90);
Serial.print("Tilting right ");
Serial.print(degreesY);
Serial.println(" degrees");
buttonCharacteristic.writeValue(degreesY);
}
}
delay(500);
//buttonCharacteristic.writeValue(10000);
}
手机端需要下载软件 https://download.csdn.net/download/qq_45685327/88908283
操作如图,最后一张就是接受到的数据