Hello everyone;
I am working on a project called wireless inertial pointer. I am trying display raw data of gyro and accelerometer on screen using processing but however I can display a maximum of 2 readings as all other get mixed up and comes up on screen after a long time.
following is my arduino code:
#define I2CACCGYROADD 0x68
/* Accelerometer/Gyro register addresses */
#define ACCEL_CONFIG 0x1C
#define GYRO_CONFIG 0x1B
#define ACCEL_XOUT_H 0x3B
#define ACCEL_YOUT_H 0x3D
#define ACCEL_ZOUT_H 0x3F
#define GYRO_XOUT_H 0x43
#define GYRO_YOUT_H 0x45
#define GYRO_ZOUT_H 0x47
#define PWR_MGMT_1 0x6B
/* Different ranges of accelerometer */
#define ACCELRANGE_2g 0
#define ACCELRANGE_4g 1
#define ACCELRANGE_8g 2
#define ACCELRANGE_16g 3
/* different sensitivities of gyroscope */
#define GYRORANGE_250DPS 0
#define GYRORANGE_500DPS 1
#define GYRORANGE_1000DPS 2
#define GYRORANGE_2000DPS 3
#include <Wire.h>
void setup()
{
/* Initialise the I2C bus */
Wire.begin();
/* Initialise the serial interface */
Serial.begin(9600);
Initalise_AccelGyro(ACCELRANGE_2g, GYRORANGE_2000DPS);
}
void loop(){
/*Read the accelerometer X, Y, and Z axis and send it to the serial port */
Serial.print(Read_Acc_Gyro(ACCEL_XOUT_H));
Serial.print(",");
Serial.print(Read_Acc_Gyro(ACCEL_YOUT_H));
Serial.print("-");
Serial.print(Read_Acc_Gyro(ACCEL_ZOUT_H));
Serial.print(".");
}
/* Read one of the accelerometer or gyro axis registers and this method is used above in loop module */
int Read_Acc_Gyro(byte axis)
{
int Data;
Wire.beginTransmission(I2CACCGYROADD);
Wire.write(axis); // sends the register addr
Wire.endTransmission();
Wire.requestFrom(I2CACCGYROADD, 2);
Data = (int)Wire.read() << 8;
Data = Data | Wire.read();
Wire.endTransmission();
return Data;
}
/* This method is used in the setup part of the code /
void Initalise_AccelGyro(byte Accel_Range, byte Gyro_Range)
{
/ handles the sleep mode feature */
Wire.beginTransmission(I2CACCGYROADD);
Wire.write(PWR_MGMT_1);
Wire.write(0);
Wire.endTransmission();
/* Set the sensitivity of the module */
Wire.beginTransmission(I2CACCGYROADD);
Wire.write(ACCEL_CONFIG);
Wire.write(Accel_Range << 3); // check page 12 and 13 of datasheet
Wire.endTransmission();
/* Set the sensitivity of the module */
Wire.beginTransmission(I2CACCGYROADD);
Wire.write(GYRO_CONFIG);
Wire.write(Gyro_Range << 3);
Wire.endTransmission();
}
following is my processing code:
import processing.serial.*;
Serial port;
String acc_X = "";
String acc_Y = "";
String acc_Z = "";
String data = "";
int index = 0;
int index1 = 0;
PFont font;
void setup(){
size (600, 600);
font = loadFont("Algerian-40.vlw");
textFont(font,40);
port = new Serial(this,"COM4",9600);
port.bufferUntil('.');
}
void draw(){
background(0,0,0);
fill(46,209,2);
text("ACC_X = ", 70, 100);
text(acc_X, 250,100);
text(acc_Y, 250, 150);
text("ACC_Y = ", 70, 150);
text("ACC_Z = ", 70,200);
text(acc_Z, 250,200);
}
void serialEvent (Serial port){
try{
data = port.readStringUntil('.');
data = data.substring(0, data.length() -1);
index = data.indexOf(",");
index1 = data.indexOf("-");
acc_X = data.substring(0, index);
acc_Y = data.substring(index+1, index1);
acc_Z = data.substring(index1+1, data.length());
}catch(Exception e){}
}
please help me out. I am currently trying to read 3 data of accelerometer in X,Y and Z direction but only X value is displayed on screen in real time the rest 2 mix up and appear very slowly on the screen
Thanks;
Sufiyan (different.unique@gmail.com)