Thanks for the reply JMeller,
The graph shows a vibration level vs a wheel position.
The negative zero crossing point is the point at which I use to show where the compensation weight needs to be added.
The zero point on the graph is the static value of the accelerometer, so the vibration may be higher or lower depending on where the wheel is when the vibration occurs.
I think that I need to take the wheel position points at which the vibration wave travels through the zero line, and average the wheel position to give me the best compensation point. I may end up having to average out the sine wave instead, and use a single wheel position point. I think one or the other will work fine for what I am doing. But as far as the programming goes, I am unaware which will be more effective, and am unaware how I would achieve this.
I will post the program so you can see how I get this wave.
If there is anything else I can provide you, I am happy to do so.
Thanks!!
#include <Wire.h>
byte DEVICE_ADDRESS = 0x53; //This address is fixed for the board we use because pin 12 is fixed to GND
byte POWER_CTRL = 0x2D; //Power Control Register
byte DATA_FORMAT = 0x31; //Data Format & Measurement Range Register
byte DATAX0 = 0x32; //X-Axis Data 0
byte DATAX1 = 0x33; //X-Axis Data 1
byte DATAY0 = 0x34; //Y-Axis Data 0
byte DATAY1 = 0x35; //Y-Axis Data 1
byte values[10];
int x,y;
int encoder0Pos;
void setup() {
Wire.begin(); //Initiate the Wire library and join the I2C bus as a master. This is called only once.
Serial.begin(230400);
DDRB = B00000000; // set ports to inputs
DDRD = B00000000; // set ports to inputs
writeRegister(DEVICE_ADDRESS, DATA_FORMAT, 0x01); //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the register.
writeRegister(DEVICE_ADDRESS, POWER_CTRL, 0x08); //Put the ADXL345 into Measurement Mode by writing the value 0x08 to the register.
}
void loop() {
readRegister(DEVICE_ADDRESS, DATAX0, 6, values);
y = ((int)values[3]<<8)|(int)values[2];
//y = map(y, -150, 150, 0, 255);
analogWrite(5, y);
Serial.print(y, DEC);
Serial.print(',');
Serial.println(PINB, DEC);
delay(1);
}
void writeRegister(byte device, byte registerAddress, byte value) {
Wire.beginTransmission(device); //Start transmission to device
Wire.write(registerAddress); //Specify the address of the register to be written to
Wire.write(value); //Send the value to be writen to the register
Wire.endTransmission(); //End transmission
}
void readRegister(byte device, byte registerAddress, int numBytes, byte *values) {
byte address = 0x80|registerAddress;
if (numBytes > 1) address = address|0x40;
Wire.beginTransmission(device);
Wire.write(address);
Wire.endTransmission();
Wire.beginTransmission(device);
Wire.requestFrom(device, numBytes); //Request 6 bytes from device
int i = 0;
while (Wire.available()) { //Device may send less than requested
values[i] = Wire.read(); //Receive a byte from device and put it into the buffer
i++;
}
Wire.endTransmission();
}