Pages: [1]   Go Down
Author Topic: Gyroscope Library Review Request  (Read 949 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Arduino community! I have been working with the arduino for almost a year now, and recently decided to get more in depth and write my own library. After much research I finally made a library. It doesn't work, at all. That is why I am asking those whom have the greater knowledge than I to review my code.

header:
Code:
//gyro header

#ifndef gyro_h
#define gyro_h

#include <Wire.h>

class  gyro
{
 public:
 gyro();
 void begin();
 void xValue(int x);
 void yValue(int y);
 void zValue(int z);

 private:
 void readI2C();
 void writeI2C();
 
 
 };
 
 #endif

code
Code:
//This is a library for reading and reporting the axis
//states of the sensor.
//Anonymous Parallax
// and Nicolas Guerra-Contreras
//
#include <Wire.h>
#include "gyro.h"

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define Addr = 105;

#define int x
#define int y
#define int z

gyro::gyro(){
}

void gyro::begin() //start the gyro
{
Wire.begin();
writeI2C(CTRL_REG1, 0x1F);    // Turn on all axes, disable power down
    writeI2C(CTRL_REG3, 0x08);    // Enable control ready signal
    writeI2C(CTRL_REG4, 0x80);    // Set scale (500 deg/sec)
    delay(100);                   // Wait to synchronize
}

void gyro::xValue() //gets x value
{
 byte MSB, LSB; //byte stores an 8-bit unsigned number(MSB,LSB) from 0 to 255.

 MSB = readI2C(0x29); //reads x memreg MSB
 LSB = readI2C(0x28); //reads x memreg LSB
 x = ((MSB << 8) | LSB); //moves MSB out of memo
}

void gyro::yValue()
{
 byte MSB, LSB;

 MSB = readI2C(0x2B);
 LSB = readI2C(0x2A);
 y = ((MSB << 8) | LSB);
}

void gyro::zValue()
{
 byte MSB, LSB;

 MSB = readI2C(0x2D);
 LSB = readI2C(0x2C);
 z = ((MSB << *) | LSB);
}

void gyro::readI2C(byte regAddr)
{
 Wire.beginTransmission(Addr);
 Wire.write(regAddr);                // Register address to read
 Wire.endTransmission();             // Terminate request
 Wire.requestFrom(Addr, 1);          // Read a byte
 while(!Wire.available()) { };       // Wait for receipt
 return(Wire.read()); // Get result
}

void gyro::writeI2C(byte regAddr, byte val)
{
 Wire.beginTransmission(Addr);
 Wire.write(regAddr);
 Wire.write(val);
 Wire.endTransmission();
}


Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
Power up to your potential
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

We recently purchased a l3gd20 board from Pololu which came with a library and example. It took around 10 minutes on a UNO to get the gyro working. Have you had a look at the Pololu library?
Logged


Pages: [1]   Go Up
Jump to: