Writing the Arduino sketch
Now that we have made sure that the robot is working properly, we can write the final sketch that will receive the commands via Bluetooth. As the sketch shares many similarities with the test sketch, we are only going to see what is added compared to the test sketch. We first need to include more libraries:
#include <SPI.h> #include "Adafruit_BLE_UART.h" #include <aREST.h>
We also define which pins the BLE module is connected to:
#define ADAFRUITBLE_REQ 10 #define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3 #define ADAFRUITBLE_RST 9
We have to create an instance of the BLE module:
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
In the setup()
function of the sketch, we initialize the BLE chip:
BTLEserial.begin();
In the loop()
function, we check the status of the BLE chip and store it in a variable:
BTLEserial.pollACI(); aci_evt_opcode_t status = BTLEserial.getState();
If...