Go Down

Topic: I2C serial read (Read 767 times) previous topic - next topic

PaulS

Quote
Can you please write me a code that implements that task?

There was a library on that page, with example code. Why is that not sufficient? What do you want that the library does not provide, or the example does not do?

minnnnnas

Ok, Here is the simple code you refering too. Well a part of if because it exceeds the 9500 characters


#include <Wire.h>
#define GRAVITY 256  
#define ToRad(x) ((x)*0.01745329252)
#define ToDeg(x) ((x)*57.2957795131)

// L3G4200D gyro: 2000 dps full scale
// 70 mdps/digit; 1 dps = 0.07
#define Gyro_Gain_X 0.07
#define Gyro_Gain_Y 0.07
#define Gyro_Gain_Z 0.07
#define Gyro_Scaled_X(x) ((x)*ToRad(Gyro_Gain_X))
#define Gyro_Scaled_Y(x) ((x)*ToRad(Gyro_Gain_Y))  
#define Gyro_Scaled_Z(x) ((x)*ToRad(Gyro_Gain_Z))
#define M_X_MIN -421
#define M_Y_MIN -639
#define M_Z_MIN -238
#define M_X_MAX 424
#define M_Y_MAX 295
#define M_Z_MAX 472
#define Kp_ROLLPITCH 0.02
#define Ki_ROLLPITCH 0.00002
#define Kp_YAW 1.2
#define Ki_YAW 0.00002
#define OUTPUTMODE 1
#define PRINT_ANALOGS 0
#define PRINT_EULER 1  
#define STATUS_LED 13
float G_Dt=0.02;    // Integration time (DCM algorithm)  We will run the integration loop at 50Hz if possible
long timer=0;   //general purpuse timer
long timer_old;
long timer24=0; //Second timer used to print values
int AN[6]; //array that stores the gyro and accelerometer data
int AN_OFFSET[6]={0,0,0,0,0,0}; //Array that stores the Offset of the sensors

.................

 AN[0] = gyro.g.x;
 AN[1] = gyro.g.y;
 AN[2] = gyro.g.z;
 gyro_x = SENSOR_SIGN[0] * (AN[0] - AN_OFFSET[0]);
 gyro_y = SENSOR_SIGN[1] * (AN[1] - AN_OFFSET[1]);
 gyro_z = SENSOR_SIGN[2] * (AN[2] - AN_OFFSET[2]);
}

void Accel_Init()
{
 compass.init();
 if (compass.getDeviceType() == LSM303DLHC_DEVICE)
 {
   compass.writeAccReg(LSM303_CTRL_REG1_A, 0x47); // normal power mode, all axes enabled, 50 Hz
   compass.writeAccReg(LSM303_CTRL_REG4_A, 0x08); // high resolution output mode
   compass.writeAccReg(LSM303_CTRL_REG4_A, 0x20); // 8 g full scale: FS = 10 on DLHC
 }
 else
 {
   compass.writeAccReg(LSM303_CTRL_REG1_A, 0x27); // normal power mode, all axes enabled, 50 Hz
   compass.writeAccReg(LSM303_CTRL_REG4_A, 0x30); // 8 g full scale: FS = 11 on DLH, DLM
 }
}

// Reads x,y and z accelerometer registers
void Read_Accel()
{
 compass.readAcc();
 
 AN[3] = compass.a.x;
 AN[4] = compass.a.y;
 AN[5] = compass.a.z;
 accel_x = SENSOR_SIGN[3] * (AN[3] - AN_OFFSET[3]);
 accel_y = SENSOR_SIGN[4] * (AN[4] - AN_OFFSET[4]);
 accel_z = SENSOR_SIGN[5] * (AN[5] - AN_OFFSET[5]);
}

void Compass_Init()
{
 compass.writeMagReg(LSM303_MR_REG_M, 0x00); // continuous conversion mode
 // 15 Hz default
}

void Read_Compass()
{
 compass.readMag();
 
 magnetom_x = SENSOR_SIGN[6] * compass.m.x;
 magnetom_y = SENSOR_SIGN[7] * compass.m.y;
 magnetom_z = SENSOR_SIGN[8] * compass.m.z;
}
void printdata(void)
{    
     Serial.print("!");

     #if PRINT_EULER == 1
     Serial.print("ANG:");
     Serial.print(ToDeg(roll));
     Serial.print(",");
     Serial.print(ToDeg(pitch));
     Serial.print(",");
     Serial.print(ToDeg(yaw));
     #endif      
     #if PRINT_ANALOGS==1
     Serial.print(",AN:");
     Serial.print(AN[0]);  //(int)read_adc(0)
     Serial.print(",");
     Serial.print(AN[1]);
     Serial.print(",");
     Serial.print(AN[2]);  
     Serial.print(",");
     Serial.print(AN[3]);
     Serial.print (",");
     Serial.print(AN[4]);
     Serial.print (",");
     Serial.print(AN[5]);
     Serial.print(",");
     Serial.print(c_magnetom_x);
     Serial.print (",");
     Serial.print(c_magnetom_y);
     Serial.print (",");
     Serial.print(c_magnetom_z);
     #endif
     /*#if PRINT_DCM == 1
     Serial.print (",DCM:");
     Serial.print(convert_to_dec(DCM_Matrix[0][0]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[0][1]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[0][2]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[1][0]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[1][1]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[1][2]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[2][0]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[2][1]));
     Serial.print (",");
     Serial.print(convert_to_dec(DCM_Matrix[2][2]));
     #endif*/
     Serial.println();    
     
}

long convert_to_dec(float x)
{
 return x*10000000;
}

It may be simple to you but for me is greek...

So can you please help me with a simpler code? or that code is the simplest to implement a reading from I2C?

minnnnnas

Has to be that hard? noone can help me?

minnnnnas

Trying and searching and reading stuff about I2C i concluded to that all i need to do is just to send a tringging pulse in the clock and then just read the data.

so my code goes like this;

int pin4= A4;


void setup()
{
     pinMode(pin4, OUTPUT);
     Serial.begin(9600);
}

void loop()
{
int sensorValue=analogRead(A5);
digitalWrite(pin4, HIGH);
delay(1);
digitalWrite(pin4, LOW);
Serial.println(sensorValue);
delay(1);
}

Is that correct? will it work?



PaulS

Quote
Ok, Here is the simple code you refering too.

That does not look anything like the code I looked at. The Arduino library link on the polulo site takes you to https://github.com/pololu/L3G. The ZIP link on the page will download a zip file, containing a library. The library comes with an example that looks like:
Code: [Select]
#include <Wire.h>
#include <L3G.h>

L3G gyro;

void setup() {
  Serial.begin(9600);
  Wire.begin();

  if (!gyro.init())
  {
    Serial.println("Failed to autodetect gyro type!");
    while (1);
  }

  gyro.enableDefault();
}

void loop() {
  gyro.read();

  Serial.print("G ");
  Serial.print("X: ");
  Serial.print((int)gyro.g.x);
  Serial.print(" Y: ");
  Serial.print((int)gyro.g.y);
  Serial.print(" Z: ");
  Serial.println((int)gyro.g.z);

  delay(100);
}


Now, that looks pretty simple to me.

Go Up