$20$Bluefruit nf8001 v1.1 keeps stalling/ packets arent being consistently read

We have the Adafruit bluetooth module connected to the Arduino Uno that is connected to the Pololu motor shield. The motor shield is connected to 4 bi-directional motors.
We have used the Adafruit app on Android connected on the control pad.

The current code we have, after 3 button presses, it freezes and nothing is able to communicate with the Arduino. We have to unplug the power source. Otherwise it is in runaway mode.

  • We need speed control for the motors, can you make the app with (1) direction button press makes the device speed slow and then another directional button press (same one) can speed up the motors

motor_shield_and_bluetooth_SecondATTEMPT.ino (5.31 KB)

OP’s code where folks can actually see it.

/*********************************************************************
  This is an example for our nRF8001 Bluetooth Low Energy Breakout

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/products/1697

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

  Written by Kevin Townsend/KTOWN  for Adafruit Industries.
  MIT license, check LICENSE for more information
  All text above, and the splash screen below must be included in any redistribution
*********************************************************************/

#include <SPI.h>

#include "Adafruit_BLE_UART.h"
#include "DualVNH5019MotorShield.h"

DualVNH5019MotorShield md;

// Connect CLK/MISO/MOSI to hardware SPI
// e.g. On UNO & compatible: CLK = 13, MISO = 12, MOSI = 11
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 3    // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 1

Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);

#define BLE_READPACKET_TIMEOUT         50   // Timeout in ms waiting to read a response


// A small helper
void error(const __FlashStringHelper*err) {
  Serial.println(err);
  while (1);
}
void stopIfFault()
{
  if (md.getM1Fault())
  { Serial.println("M1 fault");
    while (1);
  }
  if (md.getM2Fault())
  { Serial.println("M2 fault");
    while (1);
  }
}

// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE_UART *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[];


/**************************************************************************/
/*!
    Configure the Arduino and start advertising with the radio
*/
/**************************************************************************/
void setup(void)
{
  Serial.begin(115200);
  while (!Serial); // Leonardo/Micro should wait for serial init
  Serial.println(F("Adafruit Bluefruit/ Motor Shield Example"));
  Serial.println(F("-----------------------------------------"));
  Serial.println("Dual VNH5019 Motor Shield");

  /* Initialise the module */
  Serial.print(F("Initialising the Adafruit nRF8001 module: "));

  if ( !BTLEserial.begin())
  {
    error(F("Couldn't find nrf8001, check wiring?"));
  }
  Serial.println( F("OK!") );
  Serial.print("****CONNECT NOW DUMMY****");

  BTLEserial.setDeviceName("AND_JEF"); /* 7 characters max! */
  md.init();
}

/**************************************************************************/
/*!
    Constantly checks for new events on the nRF8001
*/
/**************************************************************************/
aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;

void loop()
{
  // Tell the nRF8001 to do whatever it should be working on.
  BTLEserial.pollACI();

  // Ask what is our current status
  aci_evt_opcode_t status = BTLEserial.getState();
  // If the status changed....
  if (status != laststatus) {
    // print it out!
    if (status == ACI_EVT_DEVICE_STARTED) {
      Serial.println(F("* Advertising started"));
    }
    if (status == ACI_EVT_CONNECTED) {
      Serial.println(F("* Connected!"));
    }
    if (status == ACI_EVT_DISCONNECTED) {
      Serial.println(F("* Disconnected or advertising timed out"));
    }
    // OK set the last status change to this one
    laststatus = status;
  }

  if (status != ACI_EVT_CONNECTED) {
    return;
  }

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


  // Buttons
  if (packetbuffer[2] == '5') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
  for (int i = 0; i <= 400; i++)
  {
    md.setM1Speed(i);
    md.setM2Speed(i);
  }
      Serial.println(" Forward pressed");
 
     } else {
      md.setM1Speed(0);
      md.setM2Speed(0);
      Serial.println(" Forward released");
    }
  }

  BTLEserial.pollACI();

  
  if (packetbuffer[2] == '6') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
      md.setM1Speed(-400);
      md.setM2Speed(-400);
      Serial.println(" Reverse pressed");

   } else {
      md.setM1Speed(0);
      md.setM2Speed(0);
      Serial.println(" Reverse released");

    }
  }

  BTLEserial.pollACI();

  if (packetbuffer[2] == '7') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
      md.setM1Speed(400);
      md.setM2Speed(-400);
      Serial.println(" Left pressed");

    } else {
      md.setM1Speed(0);
      md.setM2Speed(0);
      Serial.println(" Left released");
    }
  }

  BTLEserial.pollACI();

  if (packetbuffer[2] == '8') {
    uint8_t buttnum = packetbuffer[2] - '0';
    boolean pressed = packetbuffer[3] - '0';
    Serial.print ("Button "); Serial.print(buttnum);
    if (pressed) {
      md.setM1Speed(-400);
      md.setM2Speed(400);
      Serial.println(" Right pressed");

    } else {

      md.setM1Speed(0);
      md.setM2Speed(0);
      Serial.println(" Right released");
    }
 
  }
}