I am using an accelerometer which uses serial communication on Arduino (UART), I wish to transmit the data to a bluetooth module (blusmirf gold) using I2C and finally read it from my android application.
I am able to detect the correct reading from accelerometer, I am also able to transmit the data through bluetooth. However, when i put these two things together, it only transmit once and stop transmitting anything afterwards.
Is there anyone can help me with this? Thanks in advance!
/*Hardware hookup:
Arduino --------------- MMA8452Q Breakout
3.3V --------------- 3.3V
GND --------------- GND
SDA (A4) --\/330 Ohm\/-- SDA
SCL (A5) --\/330 Ohm\/-- SCL
*/
#include <Wire.h> // Must include Wire library for I2C
#include <SFE_MMA8452Q.h> // Includes the SFE_MMA8452Q library
#include <Event.h>
#include <Timer.h>
#include<stdlib.h>
#include <SoftwareSerial.h>
int bluetoothTx = 16; // TX-O pin of bluetooth mate, Arduino A2,16
int bluetoothRx = 17; // RX-I pin of bluetooth mate, Arduino A3,17
MMA8452Q accel;
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx); // for communication between ATMEGA 328 and BlueSMiRF Gold
int Start_But =4;
int LED_init =7;
int LED_start =8;
int x[20];
int y[20];
int sum=0;
int sum1=0;
float begin_val1;
float begin_val2;
int type;
int start_ref1;
int start_ref2;
float start_point ;
float force_sum;
float force_value1 ;
float force_value2;
float voltage_value ;
char value[10];
char value2[10];
// taking the sensor reading and convert it to the force value and transmit
void setup()
{
delay(300);
Serial.begin(9600); // Begin the serial monitor at 57600bps
Serial.println("lalalalalalalalalaaaaaaaaaaaaaa");
// bluetooth.println("MMA8452Q Test Code!");
delay(200);
Wire.begin();
delay(100);
Serial.println("lalalalalalalalalsssssss");
accel.init();
Serial.println("lalalal");
//delay(2000);
bluetooth.begin(9600); // The Bluetooth Mate defaults to 115200bps
pinMode(Start_But,INPUT);
pinMode(LED_start,OUTPUT);
pinMode(LED_init,OUTPUT);
digitalWrite(LED_init,1);
digitalWrite(LED_start,0);
analogReference(INTERNAL);//set the analog output reference to be 3.3V instead of 5V since the output voltage from the sensors are small
//Taking the several readings under no load condition to decide which relationship between voltage and force the sensor is following
x[0]=analogRead(1);
delay(100);
x[1]=analogRead(1);
delay(100);
sum+=x[1];
x[2]=analogRead(1);
delay(100);
sum+=x[2];
x[3]=analogRead(1);
delay(100);
sum+=x[3];
x[4]=analogRead(1);
delay(100);
sum+=x[4];
x[5]=analogRead(1);
delay(100);
sum+=x[5];
x[6]=analogRead(1);
delay(100);
sum+=x[6];
x[7]=analogRead(1);
delay(100);
sum+=x[7];
x[8]=analogRead(1);
delay(100);
sum+=x[8];
x[9]=analogRead(1);
delay(100);
sum+=x[9];
x[10]=analogRead(1);
delay(100);
sum+=x[10];
begin_val1=sum/10;
y[0]=analogRead(0);
delay(100);
y[1]=analogRead(0);
delay(100);
sum1+=y[1];
y[2]=analogRead(0);
delay(100);
sum1+=y[2];
y[3]=analogRead(0);
delay(100);
sum1+=x[3];
y[4]=analogRead(0);
delay(100);
sum1+=x[4];
y[5]=analogRead(0);
delay(100);
sum1+=x[5];
y[6]=analogRead(0);
delay(100);
sum1+=x[6];
y[7]=analogRead(0);
delay(100);
sum1+=x[7];
y[8]=analogRead(0);
delay(100);
sum1+=x[8];
y[9]=analogRead(0);
delay(100);
sum1+=x[9];
y[10]=analogRead(0);
delay(100);
sum1+=x[10];
begin_val2=sum1/10;
digitalWrite(LED_init,0);
digitalWrite(LED_start,1);
Serial.println("lalalalalalalalal");
}
void loop()
{
while(1)
{
Serial.println ("oi");
accel.read();
Serial.println("aa");
//
int xAcceleration = accel.cx*100;
int yAcceleration = accel.cy*100;
int total =xAcceleration+yAcceleration;
Serial.print("ACC");
Serial.println(total);
//bluetooth.print("ACCB");
//bluetooth.println(total);
//delay(15);
int start_ref1=(analogRead(1));
int start_ref2=(analogRead(0));
force_value1=(start_ref1-begin_val1)/25;
if(force_value1<=0)
{
force_value1=0.0;
}
if(force_value1>=25)
{
force_value1=force_value1*0.8;
}
force_value2=(start_ref2-begin_val2)/5;
if(force_value2<=0)
{
force_value2=0.0;
}
if(force_value2>=25)
{
force_value2=force_value2*0.8;
}
force_sum=(force_value1+force_value2)*0.8;
dtostrf(force_sum,3,2,value);
String str(value);
str="N"+str;
delay(20);
//Serial.print("force");
//Serial.println(str);
bluetooth.print("FORCEB");
bluetooth.println(str);
delay(500);
}
}