Servo Not Staying at Position

I’ve done quite a bit of googling and haven’t quite been able to find my answer.

I’m using an Adafruit Feather 32u4 to control a Hitec servo that is powered by a powerboost 1000. I am trying to use the Adafruit Bluefruit app with the controller to control the servo - press one button and the servo moves to one position, press another button and it moves to another. I can only get one button at a time to work correctly (whichever button I place 2nd in the code). The ‘non-working’ button will move the servo to the correct position when the button is pressed, but immediately moves it back to the center (90 degree) position.

If I change the order in which the button commands are in the code, it will change which button works correctly. I’m fairly stumped at this point.

  This is an example for our nRF51822 based Bluefruit LE modules

  Pick one up today in the adafruit shop!

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  MIT license, check LICENSE for more information
  All text above, and the splash screen below must be included in
  any redistribution
#include <Servo.h>
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#if not defined (_VARIANT_ARDUINO_DUE_X_) && not defined (_VARIANT_ARDUINO_ZERO_)
#include <SoftwareSerial.h>

#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"

#include "BluefruitConfig.h"


      FACTORYRESET_ENABLE       Perform a factory reset when running this sketch
                                Enabling this will put your Bluefruit LE module
                              in a 'known good' state and clear any config
                              data set in previous sketches or projects, so
                                running this at least once is a good idea.
                                When deploying your project, however, you will
                              want to disable factory reset by setting this
                              value to 0.  If you are making changes to your
                                Bluefruit LE device via AT commands, and those
                              changes aren't persisting across resets, this
                              is the reason why.  Factory reset will erase
                              the non-volatile memory where config data is
                              stored, setting it back to factory default
                                Some sketches that require you to bond to a
                              central device (HID mouse, keyboard, etc.)
                              won't work at all with this feature enabled
                              since the factory reset will clear all of the
                              bonding data stored on the chip, meaning the
                              central device won't be able to reconnect.
    MINIMUM_FIRMWARE_VERSION  Minimum firmware version to have some new features
    MODE_LED_BEHAVIOUR        LED activity, valid options are
                              "DISABLE" or "MODE" or "BLEUART" or
                              "HWUART"  or "SPI"  or "MANUAL"
#define FACTORYRESET_ENABLE         0
#define MODE_LED_BEHAVIOUR          "MODE"

// Create the bluefruit object, either software serial...uncomment these lines
  SoftwareSerial bluefruitSS = SoftwareSerial(BLUEFRUIT_SWUART_TXD_PIN, BLUEFRUIT_SWUART_RXD_PIN);

  Adafruit_BluefruitLE_UART ble(bluefruitSS, BLUEFRUIT_UART_MODE_PIN,

/* ...or hardware serial, which does not need the RTS/CTS pins. Uncomment this line */

/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */

/* SPI, using SCK/MOSI/MISO user-defined SPI pins and then user selected CS/IRQ/RST */
//                             BLUEFRUIT_SPI_MOSI, BLUEFRUIT_SPI_CS,
//                             BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);

// A small helper
void error(const __FlashStringHelper*err) {
  while (1);

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);

// the packet buffer
extern uint8_t packetbuffer[];

    @brief  Sets up the HW an the BLE module (this function is called
            automatically on startup)

int pos = 0;
int servoPin = 11;
Servo myservo;

void setup(void)
  pinMode(13, OUTPUT);
  pinMode(6, OUTPUT);

  Serial.println(F("Controller Testing"));

  /* Initialise the module */
  //Serial.print(F("Initialising the Bluefruit LE module: "));

  if ( !ble.begin(VERBOSE_MODE) )
    error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));

  /* Disable command echo from Bluefruit */

  ble.verbose(false);  // debug info is a little annoying after this point!

  /* Wait for connection */
  while (! ble.isConnected()) {


  // Set Bluefruit to DATA mode
  Serial.println( F("Switching to DATA mode!") );



    @brief  Constantly poll for new command or response data

void loop()
  /* Wait for new data to arrive */
  uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
  if (len == 0) return;

  /* Got a packet! */
  // printHex(packetbuffer, len);

  // Buttons
  if (packetbuffer[1] == 'B') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';

    // Button 1 Turns on a Piezo Buzzer
    if (buttnum == 1) {
      digitalWrite(6, HIGH);
      Serial.println("Buzzer is on");
    else  {
      (buttnum != 6);
      digitalWrite(6, LOW);

    //Button 2 turns on the LED on pin 13
    if (buttnum == 2) {
      Serial.println("LED is On!");
      digitalWrite(13, HIGH);
      digitalWrite(13, LOW);

    //Button 7 tells the servo to go to 180 degrees
    if (buttnum == 7) {
      Serial.println("Going to 180 degrees");
      for (pos = 90; pos <= 180; pos += 1) {

    //Button 8 tells the servo to go to 0 degrees
    if (buttnum == 8) {
      Serial.println("Going to 0 degrees");
      for (pos = 90; pos >= 0; pos -= 1 ) {


Bonus Problem: each action happens once when the button is pressed and once when the button is released, and I can’t figure out how to make only happen when the button is pressed. If anyone has any insight into that, that would also be greatly appreciated.

Have a look at the syntax of if/else and maybe try a few more {}.


Have a look at the syntax of if/else and maybe try a few more {}.


Awesome, thanks!