Thank you for that.
I need to read the website in more detail, but have made I quick test code based on you code and what I had for initial motion testing. I found that it is actually fast enough to get erroneous results if at least some delay isn't used before taking a reading - 10 milliseconds seems to be enough at the moment. That kind of a pause isn't really perceptatble to most humans.
// minimal MPU-6050 tilt and roll (sjr)
// works perfectly with GY-521, pitch and roll signs agree with arrows on sensor module 7/2019
// tested with eBay Pro Mini, **no external pullups on SDA and SCL** (works with internal pullups!)
//
#include<Wire.h>
const int MPU_addr1 = 0x68;
float xa, ya, za, roll, pitch;
int enabl = 8;
int z_dir = 7;
int z_stp = 4;
int x_dir = 5;
int x_stp = 2;
int y_dir = 6;
int y_stp = 3;
int count = byte();
int i = int();
int direct = byte();
int stp_out = byte();
int q = byte();
int stp_dir = byte();
void setup() {
Wire.begin(); //begin the wire communication
Wire.beginTransmission(MPU_addr1); //begin, send the slave adress (in this case 68)
Wire.write(0x6B); //make the reset (place a 0 into the 6B register)
Wire.write(0);
Wire.endTransmission(true); //end the transmission
Serial.begin(9600);
DDRD = B11111111;
pinMode(enabl, OUTPUT);
digitalWrite(enabl, LOW);
i = 1;
q = B00000000;
stp_out = B00011100;
direct = B11100000;
count = 0;
Serial.println("Start");
Sends();
}
void loop() {
if (i == 1){
PORTD = B00011100;
//digitalWrite(z_dir, LOW);
//digitalWrite(x_dir, LOW);
//digitalWrite(y_dir, LOW);
i = 0;
}
else {
PORTD = B11111100;
//digitalWrite(z_dir, HIGH);
//digitalWrite(x_dir, HIGH);
//digitalWrite(y_dir, HIGH);
i = 1;
}
//Serial.println(" ");
for (count =0; count <=800; count++){
digitalWrite(z_stp, HIGH);
//digitalWrite(x_stp, HIGH);
//digitalWrite(y_stp, HIGH);
delay (1);
digitalWrite(z_stp, LOW);
//digitalWrite(x_stp, LOW);
//digitalWrite(y_stp, LOW);
delay (1);
}
delay (10);
Sends();
for (count =0; count <=800; count++){
//digitalWrite(z_stp, HIGH);
digitalWrite(x_stp, HIGH);
//digitalWrite(y_stp, HIGH);
delay (1);
//digitalWrite(z_stp, LOW);
digitalWrite(x_stp, LOW);
//digitalWrite(y_stp, LOW);
delay (1);
}
delay (10);
Sends();
for (count =0; count <= 800; count++){
//digitalWrite(z_stp, HIGH);
//digitalWrite(x_stp, HIGH);
digitalWrite(y_stp, HIGH);
delay (1);
//digitalWrite(z_stp, LOW);
//digitalWrite(x_stp, LOW);
digitalWrite(y_stp, LOW);
delay (1);
}
//Serial.println("Level");
delay (10);
Sends();
Serial.println();
}
void Sends(){
Wire.beginTransmission(MPU_addr1);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr1, 6, true); //get six bytes accelerometer data
xa = Wire.read() << 8 | Wire.read();
ya = Wire.read() << 8 | Wire.read();
za = Wire.read() << 8 | Wire.read();
// formula from https://wiki.dfrobot.com/How_to_Use_a_Three-Axis_Accelerometer_for_Tilt_Sensing
roll = atan2(ya , za) * 180.0 / PI;
pitch = atan2(-xa , sqrt(ya * ya + za * za)) * 180.0 / PI; //account for roll already applied
//Serial.print("roll = ");
Serial.print(roll,0);
Serial.print(" ");
//Serial.print(", pitch = ");
Serial.println(pitch,0);
return;
}