step-counter-workshop.ino
/*
STEP 1
*/
const int ledPin = 13; // set ledPin to use on-board LED
BLEPeripheral blePeripheral; // create peripheral instance
boolean stepEventsEnabeled = true; // whether you're polling or using events
long lastStepCount = 0; // step count on previous polling check
boolean blinkState = false;
BLEService bleService("19B10001-E8F2-537E-4F6C-D104768A1214"); // create service
// create ble characteristic and allow remote device to read and write
BLECharCharacteristic bleChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);
void setupBlePeripheral() {
// set the local name peripheral advertises
/*
STEP 2
*/
blePeripheral.setDeviceName();
blePeripheral.setLocalName("#cmmp2016");
// set the UUID for the service this peripheral advertises
blePeripheral.setAdvertisedServiceUuid(bleService.uuid());
// add service and characteristic
blePeripheral.addAttribute(bleService);
blePeripheral.addAttribute(bleChar);
// assign event handlers for connected, disconnected to peripheral
blePeripheral.setEventHandler(BLEConnected, blePeripheralConnectHandler);
blePeripheral.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
bleChar.setEventHandler(BLEWritten, bleCharacteristicWritten);
// set an initial value for the characteristic
bleChar.setValue(0);
// advertise the service
blePeripheral.begin();
Serial.println(("Bluetooth device active, waiting for connections..."));
}
static void eventCallback(void) {
if (CurieImu.getIntStepStatus())
updateStepCount();
}
void setUpImuSensor() {
// intialize the sensor:
CurieImu.initialize();
// turn on step detection mode:
/*
STEP 3
*/
// enable step counting:
CurieImu.setStepCountEnabled(true);
// attach the eventCallback function as the
// step event handler:
CurieImu.attachInterrupt(eventCallback);
CurieImu.setIntStepEnabled(true); // turn on step detection
CurieImu.setIntEnabled(true); // enable interrupts
Serial.println("IMU initialisation complete, waiting for events...");
}
void setup() {
Serial.begin(9600);
delay(500);
Serial.println("OK");
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
setUpImuSensor();
setupBlePeripheral();
}
static void updateStepCount() {
// get the step count:
/*
STEP - 4
*/
// if the step count has changed, print it:
if (stepCount != lastStepCount) {
Serial.print("Step count: ");
Serial.println(stepCount);
// save the current count for comparison next check:
lastStepCount = stepCount;
}
}
void process() {
static uint32_t prev = millis();
/*
* STEP - 5
*/
if (millis() - prev > 1000) {
prev = millis();
bleChar.setValue();
}
}
void loop() {
// poll peripheral
blePeripheral.poll();
process();
}
void blePeripheralConnectHandler(BLECentral& central) {
// central connected event handler
Serial.print("Connected event, central: ");
Serial.println(central.address());
}
void blePeripheralDisconnectHandler(BLECentral& central) {
// central disconnected event handler
Serial.print("Disconnected event, central: ");
Serial.println(central.address());
}
void bleCharacteristicWritten(BLECentral& central, BLECharacteristic& characteristic) {
// central wrote new value to characteristic, update LED
}