AWOL:
int value;
So, make it static.
Please don't post snippets.
i can't post the whole code because og the 9500 character limit of the post.
const byte button=2;
int value;
int lastgyrostat;
typedef union accel_t_gyro_union
{
struct
{
uint8_t x_accel_h;
uint8_t x_accel_l;
uint8_t y_accel_h;
uint8_t y_accel_l;
uint8_t z_accel_h;
uint8_t z_accel_l;
uint8_t t_h;
uint8_t t_l;
uint8_t x_gyro_h;
uint8_t x_gyro_l;
uint8_t y_gyro_h;
uint8_t y_gyro_l;
uint8_t z_gyro_h;
uint8_t z_gyro_l;
}
reg;
struct
{
int x_accel;
int y_accel;
int z_accel;
int temperature;
int x_gyro;
int y_gyro;
int z_gyro;
}
value;
};
void setup()
{
int error;
uint8_t c;
pinMode(button,INPUT);
digitalWrite(button,HIGH);
Serial.begin(9600);
Serial.println(F("InvenSense MPU-6050"));
Serial.println(F("June 2012"));
// Initialize the 'Wire' class for the I2C-bus.
Wire.begin();
// default at power-up:
// Gyro at 250 degrees second
// Acceleration at 2g
// Clock source at internal 8MHz
// The device is in sleep mode.
//
error = MPU6050_read (MPU6050_WHO_AM_I, &c, 1);
Serial.print(F("WHO_AM_I : "));
Serial.print(c,HEX);
Serial.print(F(", error = "));
Serial.println(error,DEC);
// According to the datasheet, the 'sleep' bit
// should read a '1'. But I read a '0'.
// That bit has to be cleared, since the sensor
// is in sleep mode at power-up. Even if the
// bit reads '0'.
error = MPU6050_read (MPU6050_PWR_MGMT_2, &c, 1);
Serial.print(F("PWR_MGMT_2 : "));
Serial.print(c,HEX);
Serial.print(F(", error = "));
Serial.println(error,DEC);
// Clear the 'sleep' bit to start the sensor.
MPU6050_write_reg (MPU6050_PWR_MGMT_1, 0);
}
void loop()
{
int error;
double dT;
accel_t_gyro_union accel_t_gyro;
//Serial.println(F(""));
//Serial.println(F("MPU-6050"));
// Read the raw values.
// Read 14 bytes at once,
// containing acceleration, temperature and gyro.
// With the default settings of the MPU-6050,
// there is no filter enabled, and the values
// are not very stable.
error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));
//Serial.print(F("Read accel, temp and gyro, error = "));
//Serial.println(error,DEC);
// Swap all high and low bytes.
// After this, the registers values are swapped,
// so the structure name like x_accel_l does no
// longer contain the lower byte.
uint8_t swap;
#define SWAP(x,y) swap = x; x = y; y = swap
SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l);
SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l);
SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l);
SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l);
SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l);
SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l);
SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l);
// Print the raw acceleration values
//Serial.print(F("accel x,y,z: "));
// Serial.print(accel_t_gyro.value.x_accel, DEC);
// Serial.print(F(", "));
// Serial.print(accel_t_gyro.value.y_accel, DEC);
// Serial.print(F(", "));
// Serial.print(accel_t_gyro.value.z_accel, DEC);
// Serial.print(F(", "));
// The temperature sensor is -40 to +85 degrees Celsius.
// It is a signed integer.
// According to the datasheet:
// 340 per degrees Celsius, -512 at 35 degrees.
// At 0 degrees: -512 - (340 * 35) = -12412
//Serial.print(F("temperature: "));
//dT = ( (double) accel_t_gyro.value.temperature + 12412.0) / 340.0;
//Serial.print(dT, 3);
//Serial.print(F(" degrees Celsius"));
//Serial.println(F(""));
// Print the raw gyro values.
//Serial.print(F("gyro x,y,z : "));
// Serial.print(accel_t_gyro.value.x_gyro, DEC);
// Serial.print(F(", "));
// Serial.print(accel_t_gyro.value.y_gyro, DEC);
// Serial.print(F(", "));
// Serial.print(accel_t_gyro.value.z_gyro, DEC);
//Serial.print(F(", "));
// Serial.println(F(""));
byte buttonv=digitalRead(button);
if (buttonv == LOW) {
int gyrostat=map(accel_t_gyro.value.y_accel,-16000,16000,-127,127);
if (gyrostat!=lastgyrostat){
if (gyrostat>lastgyrostat){
value++;
}
else
value--;
}
Serial.print(value, DEC);
lastgyrostat=gyrostat;
}
// delay(500);
}
// --------------------------------------------------------
// MPU6050_read
//
// This is a common function to read multiple bytes
// from an I2C device.
//
// It uses the boolean parameter for Wire.endTransMission()
// to be able to hold or release the I2C-bus.
// This is implemented in Arduino 1.0.1.
//
// Only this function is used to read.
// There is no function for a single byte.
//
int MPU6050_read(int start, uint8_t *buffer, int size)
{
int i, n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start);
if (n != 1)
return (-10);
n = Wire.endTransmission(false); // hold the I2C-bus
if (n != 0)
return (n);
// Third parameter is true: relase I2C-bus after data is read.
Wire.requestFrom(MPU6050_I2C_ADDRESS, size, true);
i = 0;
while(Wire.available() && i<size)
{
buffer[i++]=Wire.read();
}
if ( i != size)
return (-11);
return (0); // return : no error
}
// --------------------------------------------------------
// MPU6050_write
//
// This is a common function to write multiple bytes to an I2C device.
//
// If only a single register is written,
// use the function MPU_6050_write_reg().
//
// Parameters:
// start : Start address, use a define for the register
// pData : A pointer to the data to write.
// size : The number of bytes to write.
//
// If only a single register is written, a pointer
// to the data has to be used, and the size is
// a single byte:
// int data = 0; // the data to write
// MPU6050_write (MPU6050_PWR_MGMT_1, &c, 1);
//
int MPU6050_write(int start, const uint8_t *pData, int size)
{
int n, error;
Wire.beginTransmission(MPU6050_I2C_ADDRESS);
n = Wire.write(start); // write the start address
if (n != 1)
return (-20);
n = Wire.write(pData, size); // write data bytes
if (n != size)
return (-21);
error = Wire.endTransmission(true); // release the I2C-bus
if (error != 0)
return (error);
return (0); // return : no error
}
// --------------------------------------------------------
// MPU6050_write_reg
//
// An extra function to write a single register.
// It is just a wrapper around the MPU_6050_write()
// function, and it is only a convenient function
// to make it easier to write a single register.
//
int MPU6050_write_reg(int reg, uint8_t data)
{
int error;
error = MPU6050_write(reg, &data, 1);
return (error);
}
PaulS:
byte buttonv=digitalRead(button);
int value;
int lastgyrostat;
int gyrostat=map(accel_t_gyro.value.y_accel,-16000,16000,-127,127);
if (!buttonv) {
The digitalRead() function does not return a boolean. It is poor programming to treat it as such, in my opinion. I hate code where I have to guess what you mean.
if(buttonv == LOW) makes a LOT more sense to me.
> Here is the code, but it doesn't works, everytime i press the button, the variable"value" gets the value of "gyrostat" but i would like it to stay static, unless i move the object, while the button is pressed.
Then, declare it static. You get a new copy of gyrostat and value, and plenty of other values, on every pass through loop.
What it really looks like, though, is that you only want to read the gyroscope when the switch is pressed. So, why not move the code to map the values INSIDE the "if switch is pressed" block?
MarkT:
or global. ditto for lastgyrostat - these values need to persist across calls to loop(), so don't declare them on
entry to loop...
Thanks for the advice, that was a huge mistake...
Now at least i got something what is closer to the goal, with the code above, but these changes are too tiny, but at least its working
Thanks a lot!