# Category: how-to

## Programming the body interaction 1 (BI) part 2

The BI has built in the accelerometer Bosch BMA020. The BMA020 is a 3-axis accelerometer and reads acceleration data in X, Y and Z direction. The accelerometer is used to control the operation of the BI. It captures changes in movement eg. slowing, speed up, change of direction. Steady movement can not be captured. Nevertheless it is possible to calculate the orientation of the BI eg. upright or downright.

To read out the data we use the JeeLib – a great library we will use to control other devices and read out sensors (read here how to install). The library must be included, add:

`#include <JeeLib.h>`

In JeeLib the accelerometer is called GravityPlug. It is not connected to a pin directly (as the vibration motor). Instead it is connected to the I2C bus, but at this point ignore the details. Just add:

```PortI2C myBus (1);
GravityPlug sensor (myBus);```

Now we can read out the accelerometer using the name “sensor”.

Now we want to read out the sensor. This is done by adding “.getAxes()” to “sensor”. But before we declare a new variable called p, where we the acceleration in X-, Y- and Z-axis is stored:

`const int* p = sensor.getAxes();`

The acceleration in x-direction is stored in p[0], y-direction in p[1] and z-direction in p[2].

p[0], p[1] and p[2] are integers. When there is no acceleration (eg. the BI is not moved) the value would be approx 0. If you move it horizontally to the right it will return positive values. If you move it horizontal to the left it will return negative values. (Could be vice versa, it depends on the orientation of the accelerometer).

We introduce 3 new variables x, y and z for storing the values:

`int x,y,z;`

As we are not interested in the direction of the acceleration we convert negative values to positives. This is known as the absolute value and is computed with the function abs(), see: https://www.arduino.cc/en/Reference/Abs

```x = abs(p[0]);
y = abs(p[1]);
z = abs(p[2]);```

We also introduce a further variable called “threshold”. Only values above a given threshold will change the behaviour of the vibration motor. This is useful to ignore the gravity which influences at least of the axises.

`int threshold = 250;`

Now we put everything together. The script should start the vibration motor if there is acceleration either in x, y or z direction above a threshold. Otherwise the motor is off.

```#include JeeLib.h;
PortI2C myBus (1);
GravityPlug sensor (myBus);

int motor = 3;
int threshold = 100;
int x, y, z;

void setup() {
pinMode(motor, OUTPUT);
}

void loop() {
const int* p = sensor.getAxes();
x = abs(p[0]);
y = abs(p[1]);
z = abs(p[2]);
analogWrite(motor, 0); //motor off
if (x &amp;gt; threshold || y &amp;gt; threshold || z &amp;gt; threshold) {
analogWrite(motor, 255); //motor on
}
delay(2000);  // wait for 2 second
}
```

Let’s take a further look at the loop. Every time when the instruction in the loop are executed, the sensor will be read out. The absolute values are stored in x, y and z by using the abs() function. Then the motor is set off.

The following statement

`analogWrite(motor, 255);`

will turn the motor on if the following condition is true:

`(x > threshold || y > threshold || z > threshold)`

The condition is true when either x > threshold or y > threshold or z > threshold. You can read “||” as logical or. Read more about the if function and boolean operators (or, and, not).

Finally a delay functions stops further processing for 2 seconds. Then the loop will be executed again, starting to read out the accelerometer values.

In depth example of reading the BMA020:

http://playground.arduino.cc/Main/SoftwareI2CLibrary

Jeelib GravityPlug  – how to read out the BMA020 with the JeeLib library.

http://jeelabs.org/2010/03/22/gravity-plug/

More about Acceleration and Gyros and how to calculate the orientation:

http://www.instructables.com/id/Accelerometer-Gyro-Tutorial/

Copy the source code (script) into an Arduino window. Then click compile . If everything is ok you get the message done compiling.

Then upload the code to the body interaction 1. If everything is ok you get the message done uploading. You can ignore the warnings. But if you don’t get the done uploading message something went wrong. in this case read the following post:

https://bodyinteraction.com/2016/01/08/get-started-with-arduino-1-6-7-and-windows-10/

Go to the next tutorial (part 3): ramps

## Controlling the vibration motor

The vibration motor is an analog device. You can control the vibration on a scale between 0 and 255. If you set the vibration to 0 the motor is off, if you set the vibration to 255 the motor will be at full speed.

Good vibrations Tokyo by Kevin Dooley, CC BY 2.0

The motor is connected to a pin of the controller (“ATtiny84”), the heart of the BI. Every pin has a number and the motor is always connected to pin 3.

Now we can start with the first script (or program). The script will set the motor to full speed for one second. Then the motor will be off for 1 second. And this will be repeated infinite.

Here is the complete script:

```int motor=3;
void setup() {
pinMode(motor, OUTPUT);
}

void loop() {
analogWrite(motor, 255); //motor on
delay(1000);  // wait for 1 second
analogWrite(motor, 0); //motor off
delay(1000);
}
```

Now the script is explained line by line:

`int motor=3;`

First we declare a variable called “motor” and assign the value 3. The variable is of type int (integer) which is used to store a number. Now we could use “motor” instead of “3” whenever we want to control the vibration motor – this will help us to understand and debug our script.

```void setup() {
…
}```

This is function which is part of every Arduino script. It is called setup and well be executed at first and only once.

`pinMode(motor, OUTPUT);`

Each pin can be in INPUT or OUTPUT mode. In input mode sensor data can read, in output mode a motor or a LED can be controlled. We set the motor pin to OUTPUT.

```void loop() {
…
}```

In the function loop we put all the instructions which should be carried out. When all instructions are done the script doesn’t stop but starts again. Therefore the loop will be repeated infinite.

`analogWrite(motor, 255);`

The motor is set to full speed (255).

`delay(1000);`

The delay function stops all processing for 1000 milliseconds. 1000 millisecond are 1 second.

`analogWrite(motor, 0);`

Then motor is set off (0). In the second part of the tutorial uploading of the script to the body interaction 1 is explained.

More:

https://www.arduino.cc/en/Tutorial/Foundations