Control your Smartphone via Bluetooth

Description


In this example is shown how to control your smartphone using the bluetooth of the Arduino Primo.

Hardware


  • Arduino Primo
  • Joystick or a Tinkerkit Joystick
  • 3 Buttons or  3 Tinkerkit Button
  • 3 Resistor of 1KOhm
  • Breadboard
  • Wires
  • Smartphone with the Bluetooth enabled

Circuit


Make the following connections:

  • the Joystick to the pins A0 and A1;
  • the three buttons respectively at the pins 3, 4 and 12;
  • finally the board to PC.


Circuit

Code

Upload the below code.


Warning:

Use the Arduino IDE 1.8.x and check that you have already downloaded the corresponding platform (Arduino NRF52 Boards) from the Board Manager.

#include <BLEHIDPeripheral.h>
#include <BLEMouse.h>
#include <BLEMultimedia.h>
#include <BLESystemControl.h>

#define JOYSTICK_BUTTON_PIN USER1_BUTTON
#define JOYSTICK_X_AXIS_PIN A0
#define JOYSTICK_Y_AXIS_PIN A1
#define JOYSTICK_RANGE 24
#define VOLUME_UP_PIN 3
#define VOLUME_DOWN_PIN 4
#define BLOCK_SCREEN_PIN 12

// create peripheral instance, see pinouts above
BLEHIDPeripheral bleHIDPeripheral = BLEHIDPeripheral();
BLEMouse bleMouse;
BLEMultimedia bleMultimedia;
BLESystemControl bleSystemControl;

int mouseButtonState;
int vUpButtonState;
int vDownButtonState;
int bsButtonState;
int joystickXCenter;
int joystickYCenter;

void setup() {
Serial.begin(9600);

pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
mouseButtonState = digitalRead(JOYSTICK_BUTTON_PIN);
pinMode(VOLUME_UP_PIN, INPUT_PULLUP);
vUpButtonState = digitalRead(VOLUME_UP_PIN);
pinMode(VOLUME_DOWN_PIN, INPUT_PULLUP);
vDownButtonState = digitalRead(VOLUME_DOWN_PIN);
pinMode(BLOCK_SCREEN_PIN, INPUT_PULLUP);
bsButtonState = digitalRead(BLOCK_SCREEN_PIN);

Serial.println(F("BLE HID Peripheral - clearing bond data"));

// clear bond store data
bleHIDPeripheral.clearBondStoreData();

bleHIDPeripheral.setReportIdOffset(1);
bleHIDPeripheral.setLocalName("HID Demo");

bleHIDPeripheral.addHID(bleMouse);
bleHIDPeripheral.addHID(bleMultimedia);
bleHIDPeripheral.addHID(bleSystemControl);

bleHIDPeripheral.begin();

Serial.println(F("BLE HID Demo"));

joystickXCenter = readJoystickAxis(JOYSTICK_X_AXIS_PIN);
joystickYCenter = readJoystickAxis(JOYSTICK_Y_AXIS_PIN);
}

void loop() {
BLECentral central = bleHIDPeripheral.central();
int tempButtonState;
int tempButtonState2;
int tempButtonState3;
int tempButtonState4;
if (central) {
// central connected to peripheral
Serial.print(F("Connected to central: "));
Serial.println(central.address());

while (central.connected()) {
tempButtonState = digitalRead(JOYSTICK_BUTTON_PIN);
if (tempButtonState != mouseButtonState) {
mouseButtonState = tempButtonState;

if (mouseButtonState == LOW) {
Serial.println(F("Mouse press"));
bleMouse.press();
} else {
Serial.println(F("Mouse release"));
bleMouse.release();
}
}

tempButtonState2 = digitalRead(VOLUME_UP_PIN);
if (tempButtonState2 != vUpButtonState) {
vUpButtonState = tempButtonState2;

if (vUpButtonState == LOW) {
bleMultimedia.write(MMKEY_VOL_UP);
}
}

tempButtonState3 = digitalRead(VOLUME_DOWN_PIN);
if (tempButtonState3 != vDownButtonState) {
vDownButtonState = tempButtonState3;

if (vDownButtonState == LOW) {
bleMultimedia.write(MMKEY_VOL_DOWN);
}
}

tempButtonState4 = digitalRead(BLOCK_SCREEN_PIN);
if (tempButtonState4 != bsButtonState) {
bsButtonState = tempButtonState4;

if (bsButtonState == LOW) {
bleSystemControl.write(SYSCTRLKEY_POWER);
}
}


int x = readJoystickAxis(JOYSTICK_X_AXIS_PIN);
int y = readJoystickAxis(JOYSTICK_Y_AXIS_PIN);

if (x || y) {
bleMouse.move(x, y);
}
}

// central disconnected
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}

int readJoystickAxis(int pin) {
int rawValue = analogRead(pin);
delay(1);
int mappedValue = map(rawValue, 0, 1023, 0, JOYSTICK_RANGE);
int centeredValue = mappedValue - (JOYSTICK_RANGE / 2);

return (centeredValue * -1); // reverse direction
}


Output

Use a Smartphone with a Bluetooth enabled and search all available bluetooth devices.

It should appear a device called “HID demo”.


1_Search_device.jpg


Connect to the device.


2_Connec_device.jpg



Open the Serial Monitor, it will be shown the status of the board:

HID_example.jpg


Use the Joystick for moving the pointer and the USER1 button of board to click:


3_pointer.jpg


Use instead the buttons, connected to pins 3 and 4, to increase and decrease the volume of the Smartphone.


4_volume.jpg