I use my android app to control servo motor on arduino 101 through BLE.
First the arduino 101 accept the BLE connection from android phone
I write a function "servopulse" (we can check the codes below, it's very simple) to driver the servo motor to some angles.
I use pin 12 as my IO pin.
When the arduino 101 receive message, invoking function "instruct" (we can check the codes below), the function "instruct" use "servopulse" to write the servo motor, but the writing does not work, I make sure the function "servopulse" was used, because I can see the message from the serial port.
/*
* Copyright (c) 2016 Intel Corporation. All rights reserved.
* See the bottom of this file for the license terms.
*/
#include <CurieBLE.h>
#include <CurieTimerOne.h>
const int ledPin = 13; // set ledPin to use on-board LED
const int slp = 2;
const int pwm = 3;
const int dir = 4;
const int servopin = 12;
int pos = 0;
char result[4];
BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// create switch characteristic and allow remote device to read and write
BLECharacteristic switchChar("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite, 4);
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // use the LED on pin 13 as an output
pinMode(servopin,OUTPUT);
pinMode(slp,OUTPUT);
pinMode(pwm,OUTPUT);
pinMode(dir,OUTPUT);
digitalWrite(slp,LOW);
digitalWrite(pwm,LOW);
digitalWrite(dir,LOW);
digitalWrite(servopin,LOW);
// begin initialization
BLE.begin();
// set the local name peripheral advertises
BLE.setLocalName("LEDCB");
// set the UUID for the service this peripheral advertises
BLE.setAdvertisedService(ledService);
// add the characteristic to the service
ledService.addCharacteristic(switchChar);
// add service
BLE.addService(ledService);
// assign event handlers for connected, disconnected to peripheral
BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);
// assign event handlers for characteristic
switchChar.setEventHandler(BLEWritten, switchCharacteristicWritten);
// set an initial value for the characteristic
const unsigned char control[4] = {'0','0','0','0'};
switchChar.setValue(control,4);
// start advertising
BLE.advertise();
servopulse(90);
}
void loop() {
// poll for BLE events
BLE.poll();
}
void blePeripheralConnectHandler(BLEDevice central) {
// central connected event handler
digitalWrite(ledPin,HIGH);
};
void blePeripheralDisconnectHandler(BLEDevice central) {
// central disconnected event handler
}
void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
// central wrote new value to characteristic, update LED
strncpy(result, (char *)switchChar.value(),4);
instruct(result);
}
void instruct(char *instruction)
{
if(instruction[0]=='f' && instruction[1]=='0')
{
digitalWrite(slp,HIGH);
digitalWrite(dir,HIGH);
CurieTimerOne.pwmStart(pwm, 20.0, 10000);
}
else if(instruction[0]=='f' && instruction[1]=='1')
{
digitalWrite(slp,HIGH);
digitalWrite(dir,HIGH);
CurieTimerOne.pwmStart(pwm, 20.0, 10000);
}
else if(instruction[0]=='l' && instruction[1]=='0')
{
servopulse(60);
Serial.println("left: 60");
}
else if(instruction[0]=='l' && instruction[1]=='1')
{
servopulse(60);
Serial.println("left: 60");
}
else if(instruction[0]=='b' && instruction[1]=='0')
{
digitalWrite(slp,HIGH);
digitalWrite(dir,LOW);
CurieTimerOne.pwmStart(pwm, 20.0, 10000);
}
else if (instruction[0]=='b' && instruction[1]=='1')
{
digitalWrite(slp,HIGH);
digitalWrite(dir,LOW);
CurieTimerOne.pwmStart(pwm, 20.0, 10000);
}
else if(instruction[0]=='r' && instruction[1]=='0')
{
servopulse(120);
Serial.println("left: 120");
}
else if(instruction[0]=='r' && instruction[1]=='1')
{
servopulse(120);
Serial.println("left: 120");
}
else if(instruction[0]=='s' && instruction[1]=='0')
{
digitalWrite(slp,LOW);
digitalWrite(pwm,LOW);
digitalWrite(dir,LOW);
}
else if(instruction[0]=='s' && instruction[1]=='1')
{
digitalWrite(slp,LOW);
digitalWrite(pwm,LOW);
digitalWrite(dir,LOW);
}
}
void servopulse(int angle)
{
Serial.println("enter servopulse");
int pulsewidth=(angle*11)+500;
digitalWrite(servopin,HIGH);
delayMicroseconds(pulsewidth);
digitalWrite(servopin,LOW);
delayMicroseconds(20000-pulsewidth);
}
/*
Copyright (c) 2016 Intel Corporation. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-
1301 USA
*/