So I am trying to build a device that will interface with an Android app over Bluetooth. There is an MPU6050 accelerometer/gyro and HC05 Bluetooth connected to the Arduino Mega 2560. I also have a 10hz GPS that is connected directly to a second HC05 Bluetooth, as well as connected to one of the serial ports.
The code is modified from the tinyGPS++ and the i2cdevlib raw examples.
The phone app says that the GPS is reading at 10hz and the accel/gyro is only at 1hz. I realize that it takes time to execute the data from the accel/gyro but was hoping it would at least be 5hz. The goal is to add several analog sensors as well so that will slow it down even more.
Are there different ways to make the code run faster? Modify the tinyGPS?
Should I be looking into a Arduino Due instead?
#include <Wire.h>
#include <TinyGPS++.h>
#include "I2Cdev.h" // For accel/gyro
#include "MPU6050.h" // For accel/gyro
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro;
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define OUTPUT_READABLE_ACCELGYRO
// The TinyGPS++ object
TinyGPSPlus gps;
void setup()
{
Serial.begin(9600); // Computer
Serial1.begin(115200); // Bluetooth accel/gyro data
Serial2.begin(9600); // Recieve from GPS
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void loop()
{
while (Serial2.available() > 0)
if (gps.encode(Serial2.read()))
{
//displayInfo();
if (gps.location.isValid())
displayInfo();
else
{
delay(5000);
Serial.println("wait position");
}
}
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
while (true);
}
}
void displayInfo()
{
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// GPS time
if (gps.time.isValid())
{
/* // Print to computer serial for debug
Serial.print("$RC3,");
Serial.print(gps.time.value()); Serial.print(",");
Serial.print(",");
Serial.print(ax / 16384.0); Serial.print(",");
Serial.print(ay / 16384.0); Serial.print(",");
Serial.print(az / 16384.0); Serial.print(",");
Serial.print(gx / 131.0); Serial.print(",");
Serial.print(gy / 131.0); Serial.print(",");
Serial.print(gz / 131.0); Serial.print(",");
Serial.println(",,,,,,,,,,,,,,,,,,*checksum");
*/
// Print accel/gyro data to serial bluetooth
Serial1.print("$RC3,");
Serial1.print(gps.time.value()); Serial1.print(",");
Serial1.print(",");
Serial1.print(ax / 16384.0); Serial1.print(",");
Serial1.print(ay / 16384.0); Serial1.print(",");
Serial1.print(az / 16384.0); Serial1.print(",");
Serial1.print(gx / 131.0); Serial1.print(",");
Serial1.print(gy / 131.0); Serial1.print(",");
Serial1.print(gz / 131.0); Serial1.print(",");
Serial1.println(",,,,,,,,,,,,,,,,,,*checksum");
}
}
// Format that the Android app can read.
// $RC3,[time],[count],[xacc],[yacc],[zacc],[gyrox],[gyroy],[gyroz],[rpm/d1],[d2],[a1],[a2],[a3],[a4],[a5],[a6],[a7],[a8],[a9],[a10],[a11],[a12],[a13],[a14],[a15]*checksum