Balboa and Bluetooth with Cell Phone

All,

Here is the project I’ve been working on for STEM outreach and the Balboa. We had the students build the robot and follow the instruction provided at: https://www.pololu.com/docs/0J70/4 and https://www.pololu.com/docs/0J70/5.2
and how to make the balboa balance parts 1-5. All very good. https://www.pololu.com/docs/0J70/all

So I thought I would add the ability to drive the balboa via a blue tooth interface and my Iphone. Works with other phones too. I used a Robotis BT-410, (note this is a 3.3 volt device).

Here is the result: https://www.youtube.com/watch?v=TfpY4okhKGA

I use the Adafruit Bluetooth App: “BlueFruit.” In all it was a good experience for me and the students figuring out how to make all of this work. I’ll just provide the final program and any details that anyone wants:

//
// Start on BlueTooth New 11/29/2018
//

#include <Balboa32U4.h>
#include <Wire.h>
#include <LSM6.h>
#include "Balance.h"

LSM6 imu;
Balboa32U4Motors motors;
Balboa32U4Encoders encoders;
Balboa32U4Buzzer buzzer;
Balboa32U4ButtonA buttonA;
//
// Global Constants
//
char c; // Character read one at a time from serial port
char readString[10]; // I put all of the button push in here
int i = 0;
uint16_t leftSpeed, rightSpeed;

void setup()
{
  // initialize both serial ports:
  //Serial.begin(9600);
  Serial1.begin(57600);
  //
  // Turn all of the lights off
  //
  ledYellow(0);
  ledGreen(0);
  ledRed(0);
  balanceSetup();
}

void loop()  { 
  balanceUpdate();
  if (isBalancing())
  {
//
// This gets the data from the phone
//
    if (Serial1.available()) {
      c = Serial1.read();
      i = i + 1;
      readString[i] = c;
      if (i > 4) {
        i = 0;
      }
//
// Drive the Bot with button pushes
//
      if (readString[3] == '5' && readString[4] != '0') {
        leftSpeed = 7;
        rightSpeed = 7;
        ledRed(1);
      }
      else if (readString[3] == '8' && readString[4] != '0') {
        ledRed(0);
        leftSpeed = 3;
        rightSpeed = 0;
        ledYellow(1);
      }
      else if (readString[3] == '6' && readString[4] != '0') {
        ledRed(0);
        leftSpeed = -5;
        rightSpeed = -5;
        ledYellow(1);
        ledRed(1);
      }
      else if (readString[3] == '7' && readString[4] != '0') {
        ledRed(0);
        leftSpeed = 0;
        rightSpeed = 3;
        ledYellow(1);
      }
      else  {
        ledRed(0);
        ledYellow(0);
        leftSpeed = 0;
        rightSpeed = 0;
      }
    }
    balanceDrive(leftSpeed, rightSpeed);
  }
}