Ok, I tried to follow the conflicting recommendations....post all the code....and post only the relevant code.....so the whole thing is below. Serial output is first, code is second.
Thanks for the "hints" (yes, I can use sarcasm too
).
I hope that "int temp, result, error;" is an acceptable way to declare mulitple variables, I've just copied it from other code examples.
Yes, I know that result and error are different variables.
The general logic is that result should be equal to 0 (I haven't looked up all of the answers that can come out of the Wire.endTransmission(), but I assume that is why he used >0).
If result if greater than zero, turn the error flag to 1.
So right before the if statement, error = 0 (output line 4), and result = 0. But right after the if statement, error =1 (output line 5).
So for some reason, error gets changed and I can't figure out why. All of the Serial.print statements were my attempt to figure out exactly when it changes.
Related, I'm used to initializing all of my variables (explicit statement error = 1; at the beginning), but the originator of this code doesn't. Was that bad practice? Or am I missing something.
0
Id = 3
0
Read/Write success...Result code is 0
0
1
Correct ID
Read/Write success...Result code is 0
0
1
Read/Write success...Result code is 0
0
1
Read/Write success...Result code is 0
0
1
1
BMA180 NOT initialized!
X = 20
X = 16
X = 16
X = 16
X = 16
int x;
void initBMA180()
{
int temp, result, error;
Serial.println(error);
Wire.beginTransmission(address);
Wire.write(0x00);
Wire.requestFrom(address, 1);
while(Wire.available())
{
temp = Wire.read();
}
Serial.print("Id = ");
Serial.println(temp);
result = Wire.endTransmission();
Serial.println(error);
checkResult(result);
Serial.println(error);
if(result > 0)
{
error = 1;
Serial.println("error changed to 1");
}
//else
//{
// error=0;
//}
Serial.println(error);
delay(10);
if(temp == 3)
{
Serial.println("Correct ID");
// 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);
Serial.println(result);
if(result > 0)
{
error = 1;
Serial.println("error changed to 1");
}
Serial.println(error);
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);
Serial.println(result);
if(result > 0)
{
error = 1;
Serial.println("error changed to 1");
}
Serial.println(error);
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);
Serial.println(result);
if(result > 0)
{
error = 1;
Serial.println("error changed to 1");
}
Serial.println(error);
delay(10);
}
Serial.println(error);
if(error == 0)
{
Serial.println("BMA180 Init Successful");
}
else
{
Serial.println("BMA180 NOT initialized!");
}
}
void checkResult(int result)
{
if(result >= 1)
{
Serial.print("PROBLEM..... Result code is ");
Serial.println(result);
}
else
{
Serial.print("Read/Write success...Result code is ");
Serial.println(result);
}
}