I am trying to use my accelerometer and the arduino uno to detect, by putting them inside a car, whether car is at rest or moving. Right now I have the following codes:
#include <Wire.h>
#define address 0x40
void setup()
{
Wire.begin();
Serial.begin(9600);
initBMA180();
delay(2000);
}int temp;
void loop()
{readAccel();
delay(500);
}int y;
void readAccel()
{
int temp, result;temp = 0;
while(temp != 1)
{
Wire.beginTransmission(address);
Wire.write(0x03);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.read() & 0x01;
}
}Wire.beginTransmission(address);
Wire.write(0x04);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp |= Wire.read();
temp = temp >> 2;
}
Serial.print("y = ");
Serial.println(temp);if (temp > 5) {
Serial.println("In motion");
}
else {
Serial.println("At rest");
}result = Wire.endTransmission();
}void initBMA180()
{
int temp, result, error;Wire.beginTransmission(address);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.read();
}
Serial.print("Id = ");
Serial.println(temp);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
if(temp == 3)
{
// Connect to the ctrl_reg1 register and set the ee_w bit to enable writing.
Wire.beginTransmission(address);
Wire.write(0x0D);
Wire.write(B0001);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
// Connect to the bw_tcs register and set the filtering level to 10hz.
Wire.beginTransmission(address);
Wire.write(0x20);
Wire.write(B00001000);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
// Connect to the offset_lsb1 register and set the range to +- 2.
Wire.beginTransmission(address);
Wire.write(0x35);
Wire.write(B0100);
result = Wire.endTransmission();
checkResult(result);
if(result > 0)
{
error = 1;
}
delay(10);
}if(error == 0)
{
Serial.print("BMA180 Init Successful");
}
}void checkResult(int result)
{
if(result >= 1)
{
Serial.print("PROBLEM..... Result code is ");
Serial.println(result);
}
else
{
Serial.println("Read/Write success");
}
}void readId()
{
int temp, result;Wire.beginTransmission(address);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.read();
}
Serial.print("Id = ");
Serial.println(temp);
result = Wire.endTransmission();
checkResult(result);
delay(10);
}
On my serial monitor, I have:
Id = 93
Read/Write success
y = 17
In motion
y = 63
In motion
I just picked a number (5 in this case) in the 'if' condition, so whenever that changes, motion would be assumed. This obviously does not work. I cannot really make sense of the numbers, so I cannot do much with them. It just keeps on outputting 63 or 62, and whenever I move it in the y-axis, forward or backward, the number changes to a small number then quickly returns to 63 again. I am wondering if anyone know how to code this to get actual readings of the acceleration, maybe then I can convert outputs to velocity. And if that fails, I'm wondering if I can obtain at least positive and negative accelerations, so the thing would know car is accelerating or decelerating.