Andrea De Gaetano
Published © Apache-2.0

Handstand Master

Help to train and improve your handstand!

EasyFull instructions provided3 hours486
Handstand Master

Things used in this project

Hardware components

Arduino 101 & Genuino 101
Arduino 101 & Genuino 101
×1

Software apps and online services

Google chrome

Story

Read more

Code

Code for Genuino 101

Arduino
This is the code running on the device.
It must be flashed on the arduino/genuino device
#include <CurieBLE.h>
#include "CurieIMU.h"

int lastOrientation = - 1; // previous orientation (for comparison)

BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)
BLEService handstandService("19B10000-E8F2-537E-4F6C-D104768A1214"); // B

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEIntCharacteristic handstandChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLENotify);


void setup() {
  Serial.begin(9600); // initialize Serial communication
  //while (!Serial);    // wait for the serial port to open

  // initialize device
  Serial.println("Initializing IMU device...");
  CurieIMU.begin();

  blePeripheral.setLocalName("Handstan");
  blePeripheral.setAdvertisedServiceUuid(handstandService.uuid());

  // add service and characteristic:
  blePeripheral.addAttribute(handstandService);
  blePeripheral.addAttribute(handstandChar);

  blePeripheral.begin();

  // Set the accelerometer range to 2G
  CurieIMU.setAccelerometerRange(2);
}

void loop() {
int orientation = -1;   // the board's orientation
  String orientationString; // string for printing description of orientation
  /*
    The orientations of the board:
    0: flat, processor facing up
    1: flat, processor facing down
    2: landscape, analog pins down
    3: landscape, analog pins up
    4: portrait, USB connector up
    5: portrait, USB connector down
  */
  // read accelerometer:
  int x = CurieIMU.readAccelerometer(X_AXIS);
  int y = CurieIMU.readAccelerometer(Y_AXIS);
  int z = CurieIMU.readAccelerometer(Z_AXIS);

  // calculate the absolute values, to determine the largest
  int absX = abs(x);
  int absY = abs(y);
  int absZ = abs(z);

  if ( (absZ > absX) && (absZ > absY)) {
    // base orientation on Z
    if (z > 0) {
      orientationString = "up";
      orientation = 0;  
    } else {
      orientationString = "down";
      orientation = 1;
    }
  } else if ( (absY > absX) && (absY > absZ)) {
    // base orientation on Y
    if (y > 0) {
      orientationString = "digital pins up";
      orientation = 2;
    } else {
      orientationString = "analog pins up";
      orientation = 3;
    }
  } else {
    // base orientation on X
    if (x < 0) {
      orientationString = "connector up";
      orientation = 4;
    } else {
      orientationString = "connector down";
      orientation = 5;
    }
  }

  // if the orientation has changed, print out a description:
  if (orientation != lastOrientation) {
    Serial.println(orientationString);
    lastOrientation = orientation;
    notifyUpdate(lastOrientation);
  }
}

/*updating bluetooth listeners*/
void notifyUpdate(int newOrientation) {
  handstandChar.setValue(newOrientation);
}

Html5 Code

Arduino
Unzip the archive and launch the main.html file
No preview (download only).

Credits

Andrea De Gaetano

Andrea De Gaetano

8 projects • 14 followers
Interested in iot, mobile and security Love hackathons This is my blog http://pestohacks.blogspot.com
Contact

Comments