How can I limit the degree of the accelerometer?

like this

void loop()
{
{ 
{
  for (int y = 90; y <= 150; y++)
  
  Serial.println(y);


    float result = 63.0 / 90.0 * y;
    Serial.println(result);

}
}
    Wire.beginTransmission(MPU_addr); //Begins I2C transmission
    Wire.write(0x3B);                 //Start with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr, 14, true); //Request 14 Registers from MPU6050

    axis_X = Wire.read() << 8 | Wire.read(); //Obtain 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
    axis_Y = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_YOUT_H) & 0x3C (ACCEL_YOUT_L)
    axis_Z = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_ZOUT_H) & 0x3C (ACCEL_ZOUT_L)

    int xAng = map(axis_X, minVal, maxVal, -90, 90);
    int yAng = map(axis_Y, minVal, maxVal, -90, 90);
    int zAng = map(axis_Z, minVal, maxVal, -90, 90);

    y = RAD_TO_DEG * (atan2(-zAng, -xAng));  //Formula to calculate x values in degree


    displayNumber((result));
    delay(500);
  }
  void displayNumber(int num) {
    tm.display(3, num % 10);
    tm.display(2, num / 10 % 10);
    tm.point(1);
    tm.display(1, num / 100 % 10);
    tm.display(0, num / 1000 % 10);
  }

Here's a start

 for (int y = 90; y <= 150; y++) {
void loop()
{
 

  for (int y = 90; y <= 150; y++) {
  
  Serial.println(y);


    float result = 63.0 / 90.0 * y;
    Serial.println(result);
}


    Wire.beginTransmission(MPU_addr); //Begins I2C transmission
    Wire.write(0x3B);                 //Start with register 0x3B (ACCEL_XOUT_H)
    Wire.endTransmission(false);
    Wire.requestFrom(MPU_addr, 14, true); //Request 14 Registers from MPU6050

    axis_X = Wire.read() << 8 | Wire.read(); //Obtain 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
    axis_Y = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_YOUT_H) & 0x3C (ACCEL_YOUT_L)
    axis_Z = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_ZOUT_H) & 0x3C (ACCEL_ZOUT_L)

    int xAng = map(axis_X, minVal, maxVal, -90, 90);
    int yAng = map(axis_Y, minVal, maxVal, -90, 90);
    int zAng = map(axis_Z, minVal, maxVal, -90, 90);

    y = RAD_TO_DEG * (atan2(-zAng, -xAng));  //Formula to calculate x values in degree


    displayNumber((result));
    delay(500);
  }
  void displayNumber(int num) {
    tm.display(3, num % 10);
    tm.display(2, num / 10 % 10);
    tm.point(1);
    tm.display(1, num / 100 % 10);
    tm.display(0, num / 1000 % 10);
  }

As it is, the code works but prints at 90-150, but not depending on how the accelerometer is tilted

Maybe this will do what you want.

#include <Wire.h>                  //Include WIre library for using I2C
#include <TM1637.h>


const int MPU_addr = 0x68;       //I2C MPU6050 Address


int16_t axis_X, axis_Y, axis_Z;


int minVal = 0;
int maxVal = 180;


double x;
double y;
double z;


int pos = 0;
float result;


int CLK = 2;
int DIO = 3;


TM1637 tm(CLK, DIO);


void setup()
{
  tm.init();


  //set brightness; 0-7
  tm.set(5);


  Wire.begin();                        //Begins I2C communication
  Wire.beginTransmission(MPU_addr);    //Begins Transmission with MPU6050
  Wire.write(0x6B);                    //Puts MPU6050 in Sleep Mode
  Wire.write(0);                       //Puts MPU6050 in power mode
  Wire.endTransmission(true);          //Ends Trasmission


  Serial.begin(9600);
  delay(1000);
}


void loop()
{
  Wire.beginTransmission(MPU_addr); //Begins I2C transmission
  Wire.write(0x3B);                 //Start with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 14, true); //Request 14 Registers from MPU6050


  axis_X = Wire.read() << 8 | Wire.read(); //Obtain 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  axis_Y = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_YOUT_H) & 0x3C (ACCEL_YOUT_L)
  axis_Z = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_ZOUT_H) & 0x3C (ACCEL_ZOUT_L)


  int xAng = map(axis_X, minVal, maxVal, -90, 90);
  int yAng = map(axis_Y, minVal, maxVal, -90, 90);
  int zAng = map(axis_Z, minVal, maxVal, -90, 90);


  y = RAD_TO_DEG * (atan2(-zAng, -xAng));
  Serial.print("Raw angle: ");
  Serial.print(y);


  y = constrain(y, 90, 150);
  Serial.print("\tConstrained angle: ");
  Serial.print(y);

  result = 63.0 / 90.0 * y;
  Serial.print("\tResult: ");
  Serial.println(result);


  displayNumber(result);
  delay(500);
}


void displayNumber(int num)
{
  tm.display(3, num % 10);
  tm.display(2, num / 10 % 10);
  tm.point(1);
  tm.display(1, num / 100 % 10);
  tm.display(0, num / 1000 % 10);
}

Thank you :slight_smile: , but it works, but again it does not show the value of the tilt accelerometer, show Raw angle 45.0 constrained angle 90.0 Result 63.0 and again,i can send i printscreen

what with this ?

stromecek_555:
what with this ?

sp. "bump"

this code works but again omits the accelerometer, what about that? can I send a print screen?

stromecek_555:
Thank you :slight_smile: , but it works, but again it does not show the value of the tilt accelerometer, show Raw angle 45.0 constrained angle 90.0 Result 63.0 and again,i can send i printscreen

You are using the accelerometer wrong. Start with a simple sketch that shows just the value you get from the accelerometer. Until you can get good raw data from the accelerometer you can't expect to calculate a good tilt.

NOTE: DO NOT post a 'print screen'. You can copy text from Serial Monitor. You can copy error messages from the text box below your sketch. There is almost never a good reason to post a picture of text.

#include <Wire.h>                  //Include WIre library for using I2C


const int MPU_addr = 0x68;       //I2C MPU6050 Address

int16_t axis_X, axis_Y, axis_Z;

void setup()
{
  Serial.begin(9600);
  delay(1000);


  Wire.begin();                        //Begins I2C communication
  Wire.beginTransmission(MPU_addr);    //Begins Transmission with MPU6050
  Wire.write(0x6B);                    //Puts MPU6050 in Sleep Mode
  Wire.write(0);                       //Puts MPU6050 in power mode
  Wire.endTransmission(true);          //Ends Trasmission
}


void loop()
{
  Wire.beginTransmission(MPU_addr); //Begins I2C transmission
  Wire.write(0x3B);                 //Start with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr, 6, true); //Request 6 Registers from MPU6050


  axis_X = Wire.read() << 8 | Wire.read(); //Obtain 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  axis_Y = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_YOUT_H) & 0x3C (ACCEL_YOUT_L)
  axis_Z = Wire.read() << 8 | Wire.read(); //0x3B (ACCEL_ZOUT_H) & 0x3C (ACCEL_ZOUT_L)


  Serial.print("X=");
  Serial.print(axis_X);
  Serial.print("    Y=");
  Serial.print(axis_Y);
  Serial.print("    Z=");
  Serial.println(axis_Z);
  
  delay(500);
}

thank you very much :slight_smile:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.