Hey everyone,
I have done research and not been able to find much on the specifics of my problem. I have a v2.3 Motor Shield connecting two 9v DC motors through M1 and M4. I am trying to control these two motors with the Bluefruit LE SPI Friend.
I have been able to write programs for each unit independently but the problem comes when I try to control the motors using my iphone and the adafruit app through the LE SPI Friend. The response time suddenly becomes extremely slow, taking up to a minute at times to recognize that I have let go of one of the buttons.
I am a novice coder at best and have used the Controller example from the BlueFruitLE nRF51 Library (Controller | Bluefruit LE Connect for iOS and Android | Adafruit Learning System) to base my controller code off.
Here is what I am using:
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#if not defined (VARIANT_ARDUINO_DUE_X) && not defined (VARIANT_ARDUINO_ZERO)
#include <SoftwareSerial.h>
#endif
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *steeringMotor = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *driveMotor = AFMS.getMotor(4);
#define FACTORYRESET_ENABLE 1
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
//LED Variables
const int LED = 2;
void error(const __FlashStringHelper*err) {
Serial.println(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[];
void setup(void)
{
// LED Pin Setup
pinMode(LED,OUTPUT);
AFMS.begin();
// Set the speed to start, from 0 (off) to 255 (max speed)
steeringMotor->setSpeed(255);
steeringMotor->run(FORWARD);
// turn on motor
steeringMotor->run(RELEASE);
driveMotor->setSpeed(75);
driveMotor->run(FORWARD);
// turn on motor
driveMotor->run(RELEASE);
while (!Serial); // required for Flora & Micro
delay(500);
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit App Controller Example"));
Serial.println(F("-----------------------------------------"));
/* 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?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
Serial.println(F("******************************"));
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
}
// Set Bluefruit to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
Serial.println(F("******************************"));
}
void loop(void)
{
/* Wait for new data to arrive */
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;
// Buttons
if (packetbuffer[1] == 'B') {
uint8_t buttnum = packetbuffer[2] - '0';
boolean pressed = packetbuffer[3] - '0';
Serial.print ("Button "); Serial.print(buttnum);
if (pressed) {
Serial.println(" pressed");
} else {
Serial.println(" released");
}
if (buttnum == 5 && pressed){
driveMotor->run(FORWARD);
driveMotor->setSpeed(75);
Serial.print("Forward\n");
}
else if (buttnum == 5 &! pressed){
driveMotor->run(FORWARD);
driveMotor->setSpeed(0);
Serial.print("Motor off\n");
}
if (buttnum == 6 && pressed){
driveMotor->run(BACKWARD);
driveMotor->setSpeed(50);
Serial.print("Backward\n");
}
else if (buttnum == 6 &! pressed){
driveMotor->run(FORWARD);
driveMotor->setSpeed(0);
Serial.print("Motor off\n");
}
if (buttnum == 1 && pressed){
steeringMotor->run(FORWARD);
steeringMotor->setSpeed(255);
Serial.print("Turn Left\n");
}
else if (buttnum == 1 &! pressed){
steeringMotor->run(FORWARD);
steeringMotor->setSpeed(0);
Serial.print("Forward\n");
}
if (buttnum == 2 && pressed){
steeringMotor->run(BACKWARD);
steeringMotor->setSpeed(255);
Serial.print("Turn Right\n");
}
else if (buttnum == 2 &! pressed){
steeringMotor->run(FORWARD);
steeringMotor->setSpeed(0);
Serial.print("Forward\n");
}
}
}