3

I have a 9 DOF sensor attached to a BLE chip and Arduino Uno. I want to send the position measurements (i.e. x, y and z coordinates) from the sensor to my iPhone app (Bluefruit LE). The code below starts off well and I am able to see the coordinates on the iPhone. But after showing 6 sets of x-y-z coordinates on the phone all it prints are the x-coordinates. The y and z coordinates do not get shown on the phone at all. I have put the code below (likely not the most efficient code). Any suggestions on how to fix it will be very helpful. thanks in advance.

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BNO055.h>
#include <utility/imumaths.h>
#include <SPI.h>
#include "Adafruit_BLE_UART.h"

/* Set the delay between fresh samples */
#define BNO055_SAMPLERATE_DELAY_MS (100)

#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2
#define ADAFRUITBLE_RST 9


Adafruit_BNO055 bno = Adafruit_BNO055(55);
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ,     ADAFRUITBLE_RDY, ADAFRUITBLE_RST);


void displaySensorDetails(void)
{
sensor_t sensor;
bno.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print  ("Sensor:       "); Serial.println(sensor.name);
Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
Serial.print  ("Max Value:    "); Serial.print(sensor.max_value);
Serial.print  ("Min Value:    "); Serial.print(sensor.min_value);
Serial.print  ("Resolution:   "); Serial.print(sensor.resolution);
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}


/*
Arduino setup function (automatically called at startup)
*/


int x = 0;
int row = 0;
void setup(void) 
{
Serial.begin(9600);
Serial.println("Orientation Sensor Test"); Serial.println("");
Serial.println("CLEARDATA");
Serial.println("LABEL,x,y,z");

/* Initialise the sensor */
if(!bno.begin())
{
/* There was a problem detecting the BNO055 ... check your connections */
Serial.print("Ooops, no BNO055 detected ... Check your wiring or I2C ADDR!");
while(1);
}

delay(1000);

/* Display some basic information on this sensor */
displaySensorDetails();
bno.setExtCrystalUse(true);

BTLEserial.begin();

}



aci_evt_opcode_t laststatus = ACI_EVT_DISCONNECTED;

void loop(void) 
{


BTLEserial.pollACI();


/* Get a new sensor event */ 
sensors_event_t event; 
bno.getEvent(&event);

/* Display the floating point data */

Serial.print("X: ");
Serial.print(event.orientation.x, 4);
Serial.print("\tY: ");
Serial.print(event.orientation.y, 4);
Serial.print("\tZ: ");
Serial.print(event.orientation.z, 4);
Serial.println("");

/* This is the code that sends x, y and z coordinates to BLE */
  uint8_t sendbuffernew_x[20];
  char buff_x[20] = "";
  String xorient = "";
  dtostrf(event.orientation.x, 4, 1, buff_x);  //4 is mininum width, 6 is precision
  xorient += buff_x;


  xorient.getBytes(sendbuffernew_x, 20);
  char sendbuffersizenew_x = min(20, xorient.length());


  // write the data
  BTLEserial.write(sendbuffernew_x, sendbuffersizenew_x);
  memset(sendbuffernew_x, 0, sizeof(sendbuffernew_x));

  uint8_t sendbuffernew_y[20];
  char buff_y[20]= "";
  String yorient = "";
  dtostrf(event.orientation.y, 4, 1, buff_y);  //4 is mininum width, 6 is precision
  yorient += buff_y;


  yorient.getBytes(sendbuffernew_y, 20);
  char sendbuffersizenew_y = min(20, yorient.length());


  // write the data
  BTLEserial.write(sendbuffernew_y, sendbuffersizenew_y);
  memset(sendbuffernew_y, 0, sizeof(sendbuffernew_y));

  uint8_t sendbuffernew_z[20];
  char buff_z[20]="";
  String zorient = "";
  dtostrf(event.orientation.z, 4, 1, buff_z);  //4 is mininum width, 6 is precision
  zorient += buff_z;


  zorient.getBytes(sendbuffernew_z, 20);
  char sendbuffersizenew_z = min(20, zorient.length());


  // write the data
  BTLEserial.write(sendbuffernew_z, sendbuffersizenew_z);
  memset(sendbuffernew_z, 0, sizeof(sendbuffernew_z));

delay(1000);

delay(BNO055_SAMPLERATE_DELAY_MS);
}
  • Voting to close as this has sat for over a year without any effort on your part to clarify or work the issue. The problem could be so many things; if you find yourself wanting to solve it, it would be good to obtain a distinct USB-serial converter and connect the transmit data line from the Arduino to the BTLE to the receive line of that, as well as connect the grounds (do not connect the other serial line). Then you can use a terminal or the serial monitor to see what the Arduino is actually sending, and so determine if this is an Arduino problem, a BTLE problem, or maybe even an iOS one. – Chris Stratton Oct 15 '16 at 23:37
1

That's an incredibly complex way of outputting a floating point number as a string. You may possibly be fragmenting RAM with your use of String, and those String additions. Try replacing:

  uint8_t sendbuffernew_x[20];
  char buff_x[20] = "";
  String xorient = "";
  dtostrf(event.orientation.x, 4, 1, buff_x);  //4 is mininum width, 6 is precision
  xorient += buff_x;


  xorient.getBytes(sendbuffernew_x, 20);
  char sendbuffersizenew_x = min(20, xorient.length());


  // write the data
  BTLEserial.write(sendbuffernew_x, sendbuffersizenew_x);
  memset(sendbuffernew_x, 0, sizeof(sendbuffernew_x));

  uint8_t sendbuffernew_y[20];
  char buff_y[20]= "";
  String yorient = "";
  dtostrf(event.orientation.y, 4, 1, buff_y);  //4 is mininum width, 6 is precision
  yorient += buff_y;


  yorient.getBytes(sendbuffernew_y, 20);
  char sendbuffersizenew_y = min(20, yorient.length());


  // write the data
  BTLEserial.write(sendbuffernew_y, sendbuffersizenew_y);
  memset(sendbuffernew_y, 0, sizeof(sendbuffernew_y));

  uint8_t sendbuffernew_z[20];
  char buff_z[20]="";
  String zorient = "";
  dtostrf(event.orientation.z, 4, 1, buff_z);  //4 is mininum width, 6 is precision
  zorient += buff_z;


  zorient.getBytes(sendbuffernew_z, 20);
  char sendbuffersizenew_z = min(20, zorient.length());


  // write the data
  BTLEserial.write(sendbuffernew_z, sendbuffersizenew_z);
  memset(sendbuffernew_z, 0, sizeof(sendbuffernew_z));

by:

  char buff[10];

  dtostrf(event.orientation.x, 4, 1, buff); 
  BTLEserial.write(buff, strlen (buff));
  dtostrf(event.orientation.y, 4, 1, buff); 
  BTLEserial.write(buff, strlen (buff));
  dtostrf(event.orientation.z, 4, 1, buff);
  BTLEserial.write(buff, strlen (buff));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.