Bluetooth remote control

The Trackduino controller has a built-in Bluetooth module for programming and wireless data exchange. A common use is to control the controller remotely from a smartphone or tablet. The Robotrack project provides:

  • The Robotrack PDU app for Android smartphones and tablets.
  • A Robotrack software library that reads data from a mobile device and updates global variables.

Programming

The program reads data from the library through global variables:

  • bluetooth_X, bluetooth_Y, bluetooth_Z, bluetooth_W, bluetooth_aX, bluetooth_aY, bluetooth_aZ — integer joystick and accelerometer axis readings from the PDU, ranging from −100 to 100.
  • bluetooth_F1–bluetooth_F6 — Boolean states of buttons F1–F6: 1 means pressed and 0 means released.

In text mode, declare the library variables with the extern specifier. Call BTinit(); in setup; the variables are then updated automatically in the background. A program using Bluetooth PDU has this structure:

extern int bluetooth_X, bluetooth_Y, bluetooth_Z, bluetooth_W, bluetooth_aX, bluetooth_aY, bluetooth_aZ;
extern bool bluetooth_F1, bluetooth_F2, bluetooth_F3, bluetooth_F4, bluetooth_F5, bluetooth_F6;

void setup()
{
  BTinit();
  // other commands
}

void loop()
{
  // main program code
}

In the visual environment, code for these variables is generated automatically when blocks from the Bluetooth Remote group are used.

Joystick variables

The bluetooth_X, bluetooth_Y, bluetooth_Z, and bluetooth_W variables contain analog joystick axis readings from −100 to 100.

Accelerometer variables

The bluetooth_aX, bluetooth_aY, and bluetooth_aZ variables contain gravity-vector projections on the accelerometer axes, ranging from −100 to 100.

Button variables

The bluetooth_F1–bluetooth_F6 variables contain the current state of buttons F1–F6 (1 or 0).

Example

In this example, the left joystick’s vertical Y axis controls the left motor speed, and the right joystick’s W axis controls the right motor. Toggle button F4 turns an LED (“headlight”) on or off, and pressing F1 turns the buzzer on for one second.

Generated code:

extern int bluetooth_X, bluetooth_Y, bluetooth_Z, bluetooth_W, bluetooth_aX, bluetooth_aY, bluetooth_aZ;
extern bool bluetooth_F1, bluetooth_F2, bluetooth_F3, bluetooth_F4, bluetooth_F5, bluetooth_F6;

void setup()
{
  BTinit();
  pinMode(OUT1, OUTPUT);
}

void loop()
{
  startMotor(1, bluetooth_Y);
  startMotor(2, bluetooth_W);
  led(OUT1, bluetooth_F4);
  if (bluetooth_F1)
  {
    tone(OUT2, 440, 1000);
  }
}