Bluefruit BLE to Mobile to control trains

I have code working to control the Motor Shield V2 working as I need it.

I’m trying to control model trains (4) using digital switched for direction and Analog POTs for speed. This works with physical switches.

What I’m stuck trying to do is use the nRF8001 to connect to a mobile device or a Pi3 to change control to be from a mobile device or web page.

I have to get this to work from a Web App using the Adafruit nRF8001 Bluefruit breakout board. Bluefruit LE - Bluetooth Low Energy (BLE 4.0) - nRF8001 Breakout [v1.0] : ID 1697 : $19.95 : Adafruit Industries, Unique & fun DIY electronics and kits

What I need advice on is how to integrate my current code with the BLE Firmata code.

Any advice is appreciated as I’m really stuck at this point.

Current working code below:

#include <Wire.h>
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
//Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x60); 

// Select which 'port' M1, M2, M3 or M4. This controls the Trains attached to the ports defined.
Adafruit_DCMotor *Train1 = AFMS.getMotor(1);
Adafruit_DCMotor *Train2 = AFMS.getMotor(2);
Adafruit_DCMotor *Train3 = AFMS.getMotor(3);
Adafruit_DCMotor *Train4 = AFMS.getMotor(4);


// Train Motor Array
Adafruit_DCMotor *trains[4] = {Train1, Train2, Train3, Train4};

//Define POT Speed controls
int trainSpeeds[4] = {A0, A1, A2, A3};

// Train state array
int trainStates[4];

//Define Switches that control Direction of Trains
int DirectionSW1 = 5;  
int DirectionSW2 = 6; 
int DirectionSW3 = 7; 
int DirectionSW4 = 8; 

//Direction Switch Array
int switches[4] = { DirectionSW1, DirectionSW2, DirectionSW3, DirectionSW4};

// Define switch for Trolley Mode
// TRUE=Trolley mode, FALSE=Train mode
int TrolleyModeSwitch = 4;  
//Define Switch for Trolley Delay
 // TRUE=30 second delay, FALSE=60 second delay
int TrolleyDelaySwitch = 7; 
// define sensible name for trolley mode
#define TROLLEYMODE LOW


void setup() {
  // Setup Serial library at 9600 bps 
  Serial.begin(9600);           

  //Setup internal PULLUP for trains
  pinMode(DirectionSW1,INPUT_PULLUP); 
  pinMode(DirectionSW2,INPUT_PULLUP); 
  pinMode(DirectionSW3,INPUT_PULLUP); 
  pinMode(DirectionSW4,INPUT_PULLUP); 

  // PULLUP mode for Trolley Switches
  pinMode(TrolleyModeSwitch ,INPUT_PULLUP); 
  pinMode(TrolleyDelaySwitch,INPUT_PULLUP); 
// create with the default frequency 1.6KHz
  AFMS.begin();  

}

void loop()
{
  // Loop through trains (trainArray starts at 0)
  for (int trainIndex = 0; trainIndex < 4; trainIndex++)
  {
    // read the speed from the potentiometer (and divide by 4)
    int speed = analogRead(trainSpeeds[trainIndex]) / 4;
    // get the direction from the switch
    int direction = digitalRead(switches[trainIndex]);


    if (trainIndex < 3 || (trainIndex == 3 && digitalRead(TrolleyModeSwitch) != TROLLEYMODE))
    {
      controlTrain(trainIndex, direction, speed);
    }
    else
    {
      doTrolley(trainIndex, speed);
    }
  }
}

/*
  set trainspeed and direction for train specified by index
*/
void controlTrain(int index, int direction, int speed)
{
  // set direction
  if (direction == LOW)
    trains[index]->run(FORWARD);
  else
    trains[index]->run(BACKWARD);
  // set speed
  trains[index]->setSpeed(speed);
}

/*
  delay for number of milliseconds
  returns false if delay in progress, else true
*/
bool doDelay(unsigned long duration)
{
  // remember the start time of the delay; 0 means that delay is not started
  static unsigned long starttime = 0;
  if (starttime == 0)
  {
    // set the start time
    starttime = millis();
  }
  else
  {
    // if delay lapsed
    if (millis() - starttime > duration)
    {
      // reset start time
      starttime = 0;
      // indicate that delay has lapsed
      return true;
    }
  }
  // indicate delay is in progress
  return false;
}

/*
  statemachine for trolley mode
*/
void doTrolley(int index, int speed)
{
  // remember what trolley is doing
  static int trolleystate = 0;
  // remember trolley direction
  static int trolleydirection = LOW;

 // duration of delay to set runtime
  unsigned long delayduration;
  if(digitalRead(TrolleyDelaySwitch)  == HIGH)
  {
  delayduration = 30000;
  }
  else
  {
   delayduration = 60000;
  }

  switch (trolleystate)
  {
    case 0:
     controlTrain(index, trolleydirection, speed);
       if (doDelay(delayduration))
       {
//         done with this state
        trolleystate++;
        }
   
      break;
      
    case 1:
      // change direction
      if (trolleydirection == LOW)
      {
        trolleydirection = HIGH;
      }
      else
      {
        trolleydirection = LOW;
      }
      // done with this state, back to first state
      trolleystate = 0;
      break;
  }
}

I have looked at the BLE firmata sample code but am more confused. Does anyone have anywhere they can point me or advise. Thanks again

Is there a better way other than firmata?

Any advise is appreciated