Go Down

Topic: Almost there! A bit more help please!!! (Read 1 time) previous topic - next topic


Hi again,
Well with regards to my earlier post  http://arduino.cc/forum/index.php/topic,126524.0.html I  have now replaced the 3 servos with a AD5206 digital pot... And so far it works and is controlling the replaced analog pots that were on my xbox controller, so far so good! 


I need some help with a little bit of code that would hold the last position untill the gyro moves again either back to level or the next position!
Example:  Tilt forward with the gyro (x axis) the pot dose the same but if held in this possistion the gyro data returns imedeatly back to it's starting point even when it hasn't moved. But Ijust want it to hold or sustain that last position untill i move it... Just as if you pushed a joystick forward and held it there the analoge pots would stay in that position untill you leveled off...
However with the gyro it wont, it simply returns to it's starting value..
Any Ideas appritiated..

Here's the code I have sofar.
Code: [Select]

#include <Wire.h>
#include <SPI.h>

//control registers for accel
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23

int Addr = 105;                 // I2C address of gyro

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;

#define channelx 1
#define channely 2
#define channelz 3

int x, y, z;
int a, b, c;

void setup(){
  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
    // set the slaveSelectPin as an output:
   pinMode (slaveSelectPin, OUTPUT);
   // initialize SPI:

// ae mod to outout to digital pot//

void loop(){
  getGyroValues();              // Get new values
  // In following Dividing by 114 reduces noise
  a=(x / 285)+128;
  Serial.print("Raw X:");  Serial.print(a);
  b=(y / 285)+128; 
  Serial.print(" Raw Y:"); Serial.print(b);
  c= (z / 285)+128;
  Serial.print(" Raw Z:"); Serial.println(c);
  delay(200);                   // Short delay between reads

void getGyroValues () {
  byte MSB, LSB;

  MSB = readI2C(0x29);
  LSB = readI2C(0x28);
  x = ((MSB << 8) | LSB);

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

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

int readI2C (byte regAddr) {
    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 writeI2C (byte regAddr, byte val) {

int digitalPotWrite(int address, int value) {
   // take the SS pin low to select the chip:
   //  send in the address and value via SPI:
   // take the SS pin high to de-select the chip:



The gyro tells you how fast you are turning it.  It has no absolute reference.  That's why most controllers us an accelerometer: to detect deviations from level to use as controls.

To measure "how far did I turn" in order to determine when you move back to that position you have to take the speed of rotation times the amount of time it has been rotating at that speed and add that to the last known angle.  The gyroscope will tend to drift.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

Go Up