Book Image

Arduino Android Blueprints

Book Image

Arduino Android Blueprints

Overview of this book

Table of Contents (17 chapters)
Arduino Android Blueprints
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Writing the Arduino sketch


We will now write the sketch to control the servo motor via BLE. This is the complete sketch for this part:

#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include <aREST.h>
#include <Servo.h>

// Lightweight mode
#define LIGHTWEIGHT 1

// Pins
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2     // This should be pin 2 or 3
#define ADAFRUITBLE_RST 9
 
// Create servo object
Servo myservo;

// Create aREST instance
aREST rest = aREST();

// Servo position
int pos = 0;   

// BLE instance
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

void setup()
{
   // Start Serial
  Serial.begin(115200);
  
  // Attaches the servo on pin 7 to the servo object
  myservo.attach(7);  
  
  // Start BLE
  BTLEserial.begin();
  
  // Give name and ID to device
  rest.set_id("001");
  rest.set_name("servo_control");
  
  // Expose function to API
  rest.function("servo",servoControl);
}
 
 
void loop()
{...