SHOW:
|
|
- or go back to the newest paste.
| 1 | #include <Wire.h> | |
| 2 | ||
| 3 | #define CTRL_REG1 0x20 | |
| 4 | #define CTRL_REG2 0x21 | |
| 5 | #define CTRL_REG3 0x22 | |
| 6 | #define CTRL_REG4 0x23 | |
| 7 | #define CTRL_REG5 0x24 | |
| 8 | ||
| 9 | - | int L3G4200D_Address = 0x69; //I2C address of the L3G4200D |
| 9 | + | int L3G4200D_Address = 105; //I2C address of the L3G4200D 105 = 0x69 |
| 10 | ||
| 11 | int x; | |
| 12 | int y; | |
| 13 | int z; | |
| 14 | void setup(){
| |
| 15 | ||
| 16 | Wire.begin(); | |
| 17 | Serial.begin(9600); | |
| 18 | ||
| 19 | Serial.println("starting up L3G4200D");
| |
| 20 | setupL3G4200D(500); // Configure L3G4200 - 250, 500 or 2000 deg/sec | |
| 21 | pinMode(13, OUTPUT); | |
| 22 | pinMode(12, OUTPUT); | |
| 23 | pinMode(11, OUTPUT); | |
| 24 | pinMode(10, OUTPUT); | |
| 25 | delay(1500); //wait for the sensor to be ready | |
| 26 | } | |
| 27 | ||
| 28 | void loop(){
| |
| 29 | getGyroValues(); // This will update x, y, and z with new values | |
| 30 | if(x<-500) | |
| 31 | {
| |
| 32 | digitalWrite(13, HIGH); | |
| 33 | digitalWrite(12, LOW); | |
| 34 | digitalWrite(11, LOW); | |
| 35 | digitalWrite(10, LOW); | |
| 36 | } | |
| 37 | if(x>500){
| |
| 38 | digitalWrite(10, HIGH); | |
| 39 | digitalWrite(11, LOW); | |
| 40 | digitalWrite(12, LOW); | |
| 41 | digitalWrite(13, LOW); | |
| 42 | } | |
| 43 | if(y<-500){
| |
| 44 | digitalWrite(12, HIGH); | |
| 45 | digitalWrite(10, LOW); | |
| 46 | digitalWrite(11, LOW); | |
| 47 | digitalWrite(13, LOW); | |
| 48 | } | |
| 49 | if(y>500){
| |
| 50 | digitalWrite(11, HIGH); | |
| 51 | digitalWrite(12, LOW); | |
| 52 | digitalWrite(10, LOW); | |
| 53 | digitalWrite(13, LOW); | |
| 54 | } | |
| 55 | Serial.print("X:");
| |
| 56 | Serial.print(x); | |
| 57 | ||
| 58 | Serial.print(" Y:");
| |
| 59 | Serial.print(y); | |
| 60 | ||
| 61 | Serial.print(" Z:");
| |
| 62 | Serial.println(z); | |
| 63 | ||
| 64 | delay(500); //Just here to slow down the serial to make it more readable | |
| 65 | } | |
| 66 | ||
| 67 | void getGyroValues(){
| |
| 68 | ||
| 69 | byte xMSB = readRegister(L3G4200D_Address, 0x29); | |
| 70 | byte xLSB = readRegister(L3G4200D_Address, 0x28); | |
| 71 | x = ((xMSB << 8) | xLSB); | |
| 72 | ||
| 73 | byte yMSB = readRegister(L3G4200D_Address, 0x2B); | |
| 74 | byte yLSB = readRegister(L3G4200D_Address, 0x2A); | |
| 75 | y = ((yMSB << 8) | yLSB); | |
| 76 | ||
| 77 | byte zMSB = readRegister(L3G4200D_Address, 0x2D); | |
| 78 | byte zLSB = readRegister(L3G4200D_Address, 0x2C); | |
| 79 | z = ((zMSB << 8) | zLSB); | |
| 80 | } | |
| 81 | ||
| 82 | int setupL3G4200D(int scale){
| |
| 83 | //From Jim Lindblom of Sparkfun's code | |
| 84 | ||
| 85 | // Enable x, y, z and turn off power down: | |
| 86 | writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111); | |
| 87 | ||
| 88 | // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2: | |
| 89 | writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000); | |
| 90 | ||
| 91 | // Configure CTRL_REG3 to generate data ready interrupt on INT2 | |
| 92 | // No interrupts used on INT1, if you'd like to configure INT1 | |
| 93 | // or INT2 otherwise, consult the datasheet: | |
| 94 | writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000); | |
| 95 | ||
| 96 | // CTRL_REG4 controls the full-scale range, among other things: | |
| 97 | ||
| 98 | if(scale == 250){
| |
| 99 | writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000); | |
| 100 | }else if(scale == 500){
| |
| 101 | writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000); | |
| 102 | }else{
| |
| 103 | writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000); | |
| 104 | } | |
| 105 | ||
| 106 | // CTRL_REG5 controls high-pass filtering of outputs, use it | |
| 107 | // if you'd like: | |
| 108 | writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000); | |
| 109 | } | |
| 110 | ||
| 111 | void writeRegister(int deviceAddress, byte address, byte val) {
| |
| 112 | Wire.beginTransmission(deviceAddress); // start transmission to device | |
| 113 | Wire.write(address); // send register address | |
| 114 | Wire.write(val); // send value to write | |
| 115 | Wire.endTransmission(); // end transmission | |
| 116 | } | |
| 117 | ||
| 118 | int readRegister(int deviceAddress, byte address){
| |
| 119 | ||
| 120 | int v; | |
| 121 | Wire.beginTransmission(deviceAddress); | |
| 122 | Wire.write(address); // register to read | |
| 123 | Wire.endTransmission(); | |
| 124 | ||
| 125 | Wire.requestFrom(deviceAddress, 1); // read a byte | |
| 126 | ||
| 127 | while(!Wire.available()) {
| |
| 128 | // waiting | |
| 129 | } | |
| 130 | ||
| 131 | v = Wire.read(); | |
| 132 | return v; | |
| 133 | } |