MPU6050

Hi i am trying to get the angle from the MPU6050 but the code fails to load on my ide, can someone help thanks:)

Ac_OlcerMPU_6050.ino (1.15 KB)

First what do you mean by

but the code fails to load on my ide

Second, your code is small enough to add in the post using code tags

type
** **[code]** **

paste your code after that
type
** **[/code]** **
after the pasted code

//Written by Ahmet Burkay KIRNIK
//TR_CapaFenLisesi
//Measure Angle with a MPU-6050(GY-521)

#include<Wire.h>

const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;

int minVal=265;
int maxVal=402;

double x;
double y;
double z;
 


void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);
  AcX=Wire.read()<<8|Wire.read();
  AcY=Wire.read()<<8|Wire.read();
  AcZ=Wire.read()<<8|Wire.read();
    int xAng = map(AcX,minVal,maxVal,-90,90);
    int yAng = map(AcY,minVal,maxVal,-90,90);
    int zAng = map(AcZ,minVal,maxVal,-90,90);

       x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
       y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
       z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

     Serial.print("AngleX= ");
     Serial.println(x);

     Serial.print("AngleY= ");
     Serial.println(y);

     Serial.print("AngleZ= ");
     Serial.println(z);
     Serial.println("-----------------------------------------");
     delay(400);
}

(Why is it it so hard to just post the code?)

but the code fails to load on my ide

What does that mean?
You can’t open the source?
It doesn’t compile?
It compiles but doesn’t upload?

The code posted compiles for me. You must be doing something wrong.

Dear Friends,
thank you for replying, arduino did not send the alerts to me so i thought that my post was lost in the sea, but now, i can see that you guys are helping,
for the error of compiling the code, i updated the IDE and it compiled well for me, however the angle i got has some errors, i have uploaded a video for this problem under: https://youtu.be/4hzWI1RXHHc

i have another problem with the MPU6050 where by the teapot demo loads perfectly after calibration but my plane keeps sinking down, is it my gyro problem? + i dont understand the code… i am too stupid i think, please help. here is the video link: https://youtu.be/71yew05jLPU

Now i have another question, i am trying to make an if statement for my stepper motors (balancing robot), the code compiles perfectly, but i think i am missing steps or losing power, here is the code

int Stepping = false;
void setup() {
  //need to set enable pin
  Serial.begin(9600); 
  //pinMode(4,OUTPUT); //enable pin
  //digitalWrite(4,LOW);              
  pinMode(7, OUTPUT);     
  pinMode(10, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(10, LOW);

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);

  if (Stepping == true){
  digitalWrite( 9, HIGH);
  delayMicroseconds(100);          
  digitalWrite(9, LOW); 
  delayMicroseconds(100);
      }     
  
}

void loop() {
//assume not moving, direction low is ->, direction High is <-.
//read voltage at standing = 514
//if voltage read more than 514, CW to make it back to 514 <--
//if voltage read less than 514, ACW to make it back to 514 <-- 
//
//Serial.println(analogRead(0));

if (500>=analogRead(0) <= 530)
  {
    Stepping = false;//(stop motor)
  }

if (analogRead(0) < 500)
{
  digitalWrite(8, HIGH); 
  Stepping = true ;
}
if (analogRead(0) > 530)
{
  digitalWrite(8, LOW);
  Stepping = true;
}
if (Stepping == true){
  digitalWrite( 9, HIGH);
  delayMicroseconds(100);          
  digitalWrite(9, LOW); 
  delayMicroseconds(100);
      }    
       
}

compare with this code to see difference in speed/ power/ steps, dont know why.

int val = analogRead(0);

void setup() { 
  Serial.begin(9600);               
  pinMode(7, OUTPUT);     
  pinMode(10, OUTPUT);
  digitalWrite(7, LOW);
  digitalWrite(10, LOW);

  pinMode(8, OUTPUT);     
  pinMode(9, OUTPUT);
  digitalWrite(8, HIGH);
  digitalWrite(9, LOW);
}

void loop() {
      
   
  digitalWrite(9, HIGH);
  delayMicroseconds(100);          
  digitalWrite(9, LOW); 
  delayMicroseconds(100); 
}

Hope to hear from you guys soon, all my failures are demoralising me :(:frowning: thanks haha

Change

if (500>=analogRead(0) <= 530)
  {
    Stepping = false;//(stop motor)
  }

to

  if (500 >= analogRead(0) && analogRead(0) <= 530)
  {
    Stepping = false;//(stop motor)
  }

Combining logic in a condition is done using && (logical and) and || (logical or).

It might be better to read once in loop and use that value

void loop()
{
  int val = analogRead(0);
  if (500 >= val && val <= 530)
  {
    Stepping = false;//(stop motor)
  }

  ...
  ...
  
}

Hey sterretje,
your code elevated the problem, however the stepper is still not at full power/stepping, what should i do ? thanks:)