Created
August 7, 2016 16:09
-
-
Save daose/d1a568370c8b193261d6f4e4b6644e8e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BMI160.h> | |
#include <CurieIMU.h> | |
#include "CurieIMU.h" | |
#include "CurieBLE.h" | |
#include <Math.h> | |
BLEPeripheral blePeripheral; | |
BLEService cursorService("19B10010-E8F2-537E-4F6C-D104768A1214"); | |
BLECharCharacteristic yCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1216", BLERead | BLENotify); | |
BLECharCharacteristic zCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1215", BLERead | BLENotify); | |
const int ledPin = 13; | |
void setup() { | |
Serial.begin(9600); // initialize Serial communication | |
while (!Serial); // wait for the serial port to open | |
pinMode(ledPin, OUTPUT); | |
blePeripheral.setLocalName("water"); | |
blePeripheral.setAdvertisedServiceUuid(cursorService.uuid()); | |
blePeripheral.addAttribute(cursorService); | |
blePeripheral.addAttribute(yCharacteristic); | |
blePeripheral.addAttribute(zCharacteristic); | |
yCharacteristic.setValue(0); | |
zCharacteristic.setValue(0); | |
blePeripheral.begin(); | |
Serial.println("Bluetooth device active, waiting for connections...."); | |
// initialize device | |
Serial.println("Initializing IMU device..."); | |
CurieIMU.begin(); | |
// Set the accelerometer range to 250 degrees/second | |
CurieIMU.setGyroRange(250); | |
} | |
void loop() { | |
BLECentral central = blePeripheral.central(); | |
if(central.connected()){ | |
digitalWrite(ledPin, HIGH); | |
int gxRaw, gyRaw, gzRaw; // raw gyro values | |
int finalX, finalY, finalZ; | |
float gx, gy, gz; | |
CurieIMU.readGyro(gxRaw, gyRaw, gzRaw); | |
gy = convertRawGyro(gyRaw); | |
gz = convertRawGyro(gzRaw); | |
yCharacteristic.setValue(gy); | |
zCharacteristic.setValue(gz); | |
} else { | |
digitalWrite(ledPin, LOW); | |
} | |
Serial.println(); | |
} | |
float convertRawGyro(int gRaw) { | |
float g = (gRaw * 250.0) / 32768.0; | |
return g; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment