Show Posts
Pages: [1]
1  Development / Other Software Development / Gyroscope Library Review Request on: December 06, 2012, 12:34:44 pm
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();
}


2  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 12:10:06 pm
The other dude pretty much explained bitshifting, here is the ref page
http://arduino.cc/en/Reference/Bitshift

the temp is received in raw data, you need to convert the raw value into Celsius

writing some variables that hold the temperature value might be helpful
3  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 12:03:12 pm
put the regs here
    int temp1 = Wire.read(0x00); // receive a byte as character
    int temp2 = Wire.read(0x00); // receive a byte as character
4  Using Arduino / Programming Questions / Re: Library for Gyroscope needing review on: December 06, 2012, 12:00:22 pm
Hell, if you could point me to a debugger for libraries that would be cool too.
5  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 11:39:39 am
First step, get the data sheet for the hardware, it'll list the location of each register.
6  Using Arduino / Programming Questions / Library for Gyroscope needing review on: December 06, 2012, 11:27:05 am
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 first:
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

and the body:

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(ox28); //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();
}

all help and criticism is greatly appreciated.
Pages: [1]