How can I limit the degree of the accelerometer?

hi I've been trying for a long time to figure out how to limit the angle of the accelerometer and I don't know what to do with it? writes bad results :frowning:

  for (y = 90; y <= 150; y += 1)
#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()
{

  result = 63.0 / 90.0 * y;
   


  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)); 

  
  
  for (y = 90; y <= 150; y += 1)



  Serial.println(y);
  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);
}

here is the code

What do you expect?
What do you see?

How do those two things differ?

I expect it to count for the calculation I put in there, but it doesn't work, I really want it to start counting me from just 90 to 150 degrees,thank you for your answer :slight_smile:

  for (y = 90; y <= 150; y += 1)
    Serial.println(y);
  Serial.println(result);
  displayNumber((result));
  delay(500);

If you Auto format the code in the IDE it looks as above
The only line of code that uses the value of y from the for loop is

    Serial.println(y);

I presume that is not what you wanted to do

Probably not, but I would like the result to trigger me 63/90 * 100 = 70meters (I'm calculating my own calculation) (90-150)

Note that 60 / 90 is zero, unless you cast one or both operands to "float".

Sorry but I can make no sense of what you want

As written, result is calculated each time through loop() using the value of y. You calculate the value of y each time through loop()

  y = RAD_TO_DEG * (atan2(-zAng, -xAng));

then overwrite it by using y in the for loop

Do you want to calculate result for all values of y from 90 to 150 and do something based on the value of result ?

yes i want to calculate the results from 90-150 degrees :slight_smile:

You have to do the calculation INSIDE the loop, not at the top of the loop() function:

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


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


    displayNumber((result));
    delay(500);
  }
}

thank you I tried to insert it as you wrote but it shows me it the error

#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() {
  {
    for (int y = 90; y <= 150; y++)
    {
      Serial.println(y);


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


      displayNumber((result));
      delay(500);
    }
  }



  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));




  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);
  }
}

stupn__akcel.ino: In function 'void loop()':
stupn__akcel:56:7: error: 'displayNumber' was not declared in this scope
       displayNumber((result));
       ^~~~~~~~~~~~~
stupn__akcel:81:31: error: a function-definition is not allowed here before '{' token
   void displayNumber(int num) {
                               ^
Byly nalezené násobné knihovny "Wire.h"
 Použitý: C:\Program Files\WindowsApps\ArduinoLLC.ArduinoIDE_1.8.42.0_x86__mdqgnx93n4wtt\hardware\arduino\avr\libraries\Wire
 Nepoužitý: C:\Users\Maxim\Documents\Arduino\libraries\Wire-master
exit status 1
'displayNumber' was not declared in this scope

round 2

https://forum.arduino.cc/index.php?topic=734176.0

You can't define a function inside another function

I've already plugged it in but it doesn't work it shows only 105.0 in the serial monitor and when I tilt the accelerometer so nothing changes

You need to show us the corrected code.

Don't make us guess.

#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()
{
{
  for (int y = 90; y <= 150; y++)
  



    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);
  }

I was wrong before,shows it numbers from 90-150 and then 31.50 and then again

Yes, that's what you told it do.

Did you forget a pair of these { } for your for loop?

if I understand you well, then yes, can I send a print screen?

Why not just add the braces to your for loop?