SparkFun Forums 

Where electronics enthusiasts find answers.

All things pertaining to wireless and RF links
By Ricky53
#190074
Hello I am currently trying to use the bluesmirf gold bluetooth to connect with another bluesmirf gold bluetooth which has the Triple Axis Accelerometer Breakout - MMA8452Q. One of the bluetooths reads the acceleration from an accelerometer while the other is attached to my computer so that the acceleration readings can be sent wirelessly. This project has been smooth sailing up until this point, for some odd reason the code from one bluetooth to another works perfectly and sends the correct acceleration for about 5 trials per day and then it will suddenly stop working and I am no longer able to get the acceleration data to successfully print to the serial terminal. Even weirder, once it begins messing up, if I comment out the line in which I initiate the accelerometer (accel.init(); ) and also comment out the line where I read the acceleration ( accel.read();) and re-compile the code works perfectly fine. Everything that I wanted to print out begins transmitting to the serial terminal once again without failure (of course without the proper acceleration data that I need). For some reason my code does not seem to like the parts that involve the accelerometer, has anyone else run into a problem like this? I am using the arduino IDE to do all of this. I just cant wrap my head around why the code would work perfectly for a little bit and then suddenly stop working without me re-compiling the code. Any suggestions would be extremely helpful!!! Below I have included the code that allows for the bluetooth to send the acceleration readings to the other bluetooth. Thanks in advanced!!

#include <Wire.h> // Must include Wire library for I2C
#include <SparkFun_MMA8452Q.h> // Includes the SFE_MMA8452Q library
#include <SoftwareSerial.h>

MMA8452Q accel;

int bluetoothTx = 2; // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3; // RX-I pin of bluetooth mate, Arduino D3
int j = 0;
float velocityx, velocityy, positionx, positiony, velocityxinitial,velocityyinitial,xacceleration,yacceleration;
double accelerationPreviousx, accelerationPreviousy;
int runtime = 0;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{

Serial.begin(9600); // Begin the serial monitor at 9600bps
accel.init(); // FOR SOME REASON THIS LINE OF CODE IS CAUSING PROBLEMS!!!! Default Setting sets accelerometer to +/- 2g and the fastest data rate 800 Hz
bluetooth.begin(115200); // The Bluetooth Mate defaults to 115200bps
bluetooth.print("$"); // Print three times individually
bluetooth.print("$");
bluetooth.print("$"); // Enter command mode
delay(100); // Short delay, wait for the Mate to send back CMD
bluetooth.println("U,9600,N"); // Temporarily Change the baudrate to 9600, no parity
// 115200 can be too fast at times for NewSoftSerial to relay the data reliably
bluetooth.begin(9600); // Start bluetooth serial at 9600
}

void loop()
{

{

if(j == 0){
bluetooth.println("GET SET");
delay(2000);
while(runtime <5000){
accel.read();
xacceleration=accel.cx;
yacceleration=accel.cy;
if((abs(xacceleration)-abs(accelerationPreviousx))<.6){ // checking to see if two consecutive accelerations are close, if not the acceleration can be declared noise
velocityx = velocityx + (xacceleration+.08)*32.17*.01; // integrating acceleration in x for 10 and converting g to feet/ second
accelerationPreviousx = xacceleration;
bluetooth.print("Velocity x: ");
bluetooth.print(velocityx,5);
bluetooth.print(" ");
}

if((abs(yacceleration)-abs(accelerationPreviousy))<.6){ // checking to see if two consecutive accelerations are close, if not the acceleration can be declared noise
velocityy = velocityy+(yacceleration-.04)*32.17*.01; // integrating acceleration in y for 10 milliseconds

accelerationPreviousy = yacceleration;
bluetooth.print("Velocity y: ");
bluetooth.print(velocityy,5);
bluetooth.print(" ");
}

positionx = positionx+(velocityx* .1+.5*((accel.cx+.08)*32.17)*pow(.01,2)); // integrated velocity to find position in the x direction for 10 milliseconds
bluetooth.print("Position x: ");
bluetooth.print(positionx,5);
bluetooth.print(" ");
positiony = positiony+ (velocityy* .01+.5*((accel.cy-.04)*32.17)*pow(.01,2));
bluetooth.print("Position y: ");
bluetooth.print(positiony,5);
delay(10);
runtime = millis();
bluetooth.println(" ");
}

bluetooth.println(runtime);
j++;

}


}

}