Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Sachin Dwivedi
Created March 24, 2017

Healthy Arduno Habit

DIY realtime footsteps Counter using arduino to show number of steps taken per day on your smart phone

38
Healthy Arduno Habit

Story

Read more

Code

Arduino Step Counter

C/C++
this code will be uploaded into Arduino 101 , it continuously count number of steps and send it over BLE to android app
#include "CurieIMU.h"

/* To get an interrupt notification for every step detected,
    set stepEventsEnabeled to true. Otherwise, the main loop will
    poll for the current step count.

   By design, the step counter does not immediately update on every step detected.
   Please refer to Section 2.7 of the BMI160 IMU SensorData Sheet
   for more information on this feature.
*/
/////////////////////
#include <CurieBLE.h>
BLEPeripheral blePeripheral;//BLE Peripheral Device (Arduino Device)
BLEService demo111("19b10010-e8f2-537e-4f6c-d104768a1214"); // BLE demo111 Service

// BLE demo111 buttons Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedCharCharacteristic buttons("19b10011-e8f2-537e-4f6c-d104768a1214", BLERead | BLEWrite);
// BLE sensor rate Characteristic"
BLEUnsignedIntCharacteristic sensors("19b10012-e8f2-537e-4f6c-d104768a1214", BLERead | BLENotify);


long previousMillis = 0;  // last time the sensor was checked, in ms



/////////////////////
const int ledPin = 13;

boolean stepEventsEnabeled = true;   // whether you're polling or using events
long lastStepCount = 0;              // step count on previous polling check
boolean blinkState = false;          // state of the LED

void setup() {
  Serial.begin(9600);
  
  // set advertised local name and service UUID:
  blePeripheral.setLocalName("HealthyArduino");
  blePeripheral.setAdvertisedServiceUuid(demo111.uuid());
  // add service and characteristic:
  blePeripheral.addAttribute(demo111);//service 
  blePeripheral.addAttribute(buttons);// add the buttons characteristic
  blePeripheral.addAttribute(sensors); // add the sensor characteristic
  // set the initial value for the characeristic:
  buttons.setValue(1);
  // begin advertising BLE Light service:
  blePeripheral.begin();
  Serial.println("Demo111 service begin!");
  pinMode(13, OUTPUT);
  // intialize the sensor:
  CurieIMU.begin();
  // turn on step detection mode:
  CurieIMU.setStepDetectionMode(CURIE_IMU_STEP_MODE_NORMAL);
  // enable step counting:
  CurieIMU.setStepCountEnabled(true);

  if (stepEventsEnabeled) {
    // attach the eventCallback function as the
    // step event handler:
    CurieIMU.attachInterrupt(eventCallback);
    CurieIMU.interrupts(CURIE_IMU_STEP);  // turn on step detection

    Serial.println("IMU initialisation complete, waiting for events...");
  }
}

void loop() {
  /* Instead of using step detection event notifications,
     we can check the step count periodically */
  if (!stepEventsEnabeled) {
    updateStepCount();
  }
  digitalWrite(13, blinkState);
  blinkState = !blinkState;
  delay(1000);
  /////////////////
  // listen for BLE peripherals to connect:
  BLECentral central = blePeripheral.central();
  digitalWrite(green, LOW);
  digitalWrite(red, LOW);
  // if a central is connected to peripheral:
  if (central) {
    Serial.print("Connected to central: ");
    // print the central's MAC address:
    Serial.println(central.address());

    // while the central is still connected to peripheral:
    while (central.connected()) {
      // if the remote device wrote to the characteristic,
      // use the value to control the leds:
      

      long currentMillis = millis();
      // if 10s have passed, check the sensor:
      if (currentMillis - previousMillis >= 10000) {
        previousMillis = currentMillis;
        updateSensor();
      }
      //finish
    }
    // when the central disconnects, print it out:
    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());
  }
  else
  {
    /////////

  }
  ////////////////
}

static void updateStepCount() {
  // get the step count:
  int stepCount = CurieIMU.getStepCount();

  // 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;
  }
}

static void eventCallback(void) {
  if (CurieIMU.stepsDetected())
    updateStepCount();
}

void updateSensor() {
  
  sensors.setValue(CurieIMU.getStepCount());//send steps value
  Serial.println(CurieIMU.getStepCount());//if you want check steps i serial monitor
}

HealthyArduino.apk

Java
apk file to install app on your smartphone
No preview (download only).

Credits

Sachin Dwivedi
4 projects • 6 followers
Student of Final Year of B.Tech. in Computer Science

Comments