arduino 101 can't control the servo motor


i use android app control servo motor on arduino 101 through ble.

first arduino 101 accept ble connection android phone

i write function "servopulse" (we can check codes below, it's simple) driver servo motor angles.
i use pin 12 io pin.

when arduino 101 receive message, invoking function "instruct" (we can check codes below), function "instruct" use "servopulse" write servo motor, writing not work, make sure function "servopulse" used, because can see message serial port.

code: [select]
/*
 * copyright (c) 2016 intel corporation.  rights reserved.
 * see bottom of file license terms.
 */

#include <curieble.h>
#include <curietimerone.h>

const int ledpin = 13; // set ledpin 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 , allow remote device read , write
blecharacteristic switchchar("19b10001-e8f2-537e-4f6c-d104768a1214", bleread | blewrite, 4);

void setup() {
  serial.begin(9600);
 
  pinmode(ledpin, output); // use led on pin 13 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 local name peripheral advertises
  ble.setlocalname("ledcb");
  // set uuid service peripheral advertises
  ble.setadvertisedservice(ledservice);

  // add characteristic service
  ledservice.addcharacteristic(switchchar);

  // add service
  ble.addservice(ledservice);

  // assign event handlers connected, disconnected peripheral
  ble.seteventhandler(bleconnected, bleperipheralconnecthandler);
  ble.seteventhandler(bledisconnected, bleperipheraldisconnecthandler);

  // assign event handlers characteristic
  switchchar.seteventhandler(blewritten, switchcharacteristicwritten);
  // set initial value characteristic
  const unsigned char control[4] = {'0','0','0','0'};
  switchchar.setvalue(control,4);

  // start advertising
  ble.advertise();

  servopulse(90);
}

void loop() {
  // poll 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 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. rights reserved.

  library free software; can redistribute and/or
  modify under terms of gnu lesser general public
  license published free software foundation; either
  version 2.1 of license, or (at option) later version.

  library distributed in hope useful,
  without warranty; without implied warranty of
  merchantability or fitness particular purpose. see gnu
  lesser general public license more details.

  should have received copy of gnu lesser general public
  license along library; if not, write free software
  foundation, inc., 51 franklin street, fifth floor, boston, ma 02110-
  1301 usa
*/

hi,

you writing own pulse width modulation??
read https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/.

luc


Arduino Forum > Products > Arduino 101 (Moderators: cmaglie, facchinm) > arduino 101 can't control the servo motor


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???