Detecting gestures

In this next part, we will have a look at how to detect gestures with the accelerometer. For this project, we will not be using the gyro and compass. However, remember that even though this chapter will eventually end, it does not mean your projects have to. Once you are done, you can keep developing the project further and implementing the compass into the bike gloves, which may come in handy in the future.

First, let's take a look at how we can detect gestures. We will need three different interactions, the holding of the handlebar, indicating a turn, and stopping. We need to know that we are holding the handlebar, since this will act as our default state when the light should be off. In order to turn the white LEDs on, we need to know when the hand is lifted to indicate a turn, and we need to know when the hand is in a vertical position so we can turn on the red LEDs to indicate that we are stopping. Take a look at Chapter 2, Working with Sensors in order to connect the accelerometer. As mentioned earlier, the connections that need to be made from the accelerometer to the FLORA board is 3 V to 3.3 V, GND to GND, SDA to SDA, and SCL to SCL:

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM9DS0.h>
#include <Adafruit_Sensor.h>

// i2c
Adafruit_LSM9DS0 lsm = Adafruit_LSM9DS0();

void setupSensor()
{
  //Set range of the accelerometer
  lsm.setupAccel(lsm.LSM9DS0_ACCELRANGE_2G);
}

void setup()
{
  //wait until serial starts
  while (!Serial);

  Serial.begin(9600);

  // Try to initialize sensor and warn if it cant find it
  if (!lsm.begin())
  {
    Serial.println("Cant find the sensor, Check your connections!");
    while (1);
  }
  Serial.println("Found the sensor");
  Serial.println("");
  Serial.println("");
}

void loop()
{
  //read the sensor
  lsm.read();
  if(lsm.accelData.x<0 && lsm.accelData.y<0){
    Serial.println("Holding handlebar");
  }
  if(lsm.accelData.y>1000 && lsm.accelData.x<0){
    Serial.println("Stopping");
  }
  if(lsm.accelData.x<-1000 && lsm.accelData.y<3000){
    Serial.println("Turning");
  }
delay(500);
}

In the sketch, we are using a combination of the x and y axis to detect the gestures. Note that the values used in the preceding sketch were generated from the direction of my sensor, and the reference value that you need might be something completely different. It all depends in which direction you sensor is placed. The easiest way to figure out the gestures is to place the sensor flat on the table and read the x and y values. This will act as your default state, or the "holding of the handlebar" state. In my case, I got steady negative readings, so any changes to the positive, or bigger then 0 as presented in the code, would indicate that the hand is moving. Hold the sensor vertically in order to figure out what value readings will be suitable for indicating a stopping gesture. From the stopping position, turn the sensor about 45 degrees and you will find the turning position.

This sketch is just a rough estimate and shows how you can make your own simple gesture recognition in code. The values we base our gestures on also depend on whether we are calibrating the left or right glove. In the final version of the code for this project, we will need to modify the values. In order to fine-tune everything, we need to put all the components together.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset