Hello! I am a high schooler that bit off a little more than he could chew for his project. I am almost through, but I have no idea about my programs.
I am using a MPU 6050 and a HMC5883L with the I2Cdev Libraries. I have used them to calculate data in the Serial Monitor. Now, however, I am implementing them into an RC truck. These programs will control only the servo for now.
I know these programs won't be accurate; that's what I'm looking for! I am testing sensor accuracy.
Could you guys please help me review my programs? (I got another, simpler one to work, so now it's only these two)
MPU 6050 code:
#include "I2Cdev.h"
#include "MPU6050.h"
#include <Servo.h>
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
Servo myservo;
int currentGravities;
int xFinal;
MPU6050 accelgyro;
int ax, ay, az;
long previousMillis = 0; // variable to store last time it was updated
long startTime ; // start time for stop watch
long elapsedTime ; // elapsed time for stop watch
int fractional; // variable used to store fractional part of time
void setup() {
// put your setup code here, to run once:
myservo.attach(9);
myservo.write(90);
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
accelgyro.initialize();
}
void loop() {
// put your main code here, to run repeatedly:
accelgyro.getAcceleration(&ax, &ay, &az);
currentGravities = (ax)/16384;
startTime = (millis);
xFinal = 0.5*(currentGravities)*(startTime*startTime);
}
That one ^ takes in sensor data, converts it into g's, and figures out the final position in meters using time^2 since starting the loop. I am going to use it to turn a servo using an if statement (if xFinal > (this position), turn servo)
How will I be able to turn the servo at different times during the program? Will the variable xFinal continue to update itself? How do I restart the timer (if it even works) after turning the servo?
This is my HMC5883L code: ( I have tested some of it but not all yet)
#include <Wire.h>
#include <Servo.h>
#define Addr 0x1E
Servo myservo;
void setup() {
// put your setup code here, to run once:
myservo.attach(9);
myservo.write(90);
// Set operating mode to continuous
Wire.beginTransmission(Addr);
Wire.write(byte(0x02));
Wire.write(byte(0x00));
Wire.endTransmission();
}
void loop() {
// put your main code here, to run repeatedly:
int x, y, z;
Wire.beginTransmission(Addr);
Wire.write(byte(0x03)); // Send request to X MSB register
Wire.endTransmission();
Wire.requestFrom(Addr, 6); // Request 6 bytes; 2 bytes per axis
if(Wire.available() >=6) { // If 6 bytes available
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
}
// If compass module lies flat on the ground with no tilt,
// just x and y are needed for calculation
float heading=atan2(x, y)/0.0174532925;
if(heading < 0) heading+=360;
heading=360-heading; // N=0/360, E=90, S=180, W=270
Serial.println(heading);
delay(500);
// Drive Section
if (heading < 180) {
myservo.write(110);
if (heading < 25 or heading > 335){
myservo.write(90);
} else if (heading > 180) {
myservo.write(80);
if (heading < 25 or heading > 335){
myservo.write(90);
}
}
}
delay(3000);
}
Keep in mind, the section comment labeled Drive Section has the vehicle moving forward from the start of the program, ensuring that it will actually change heading after turning the servo.
Thank you for any help, I'm lost and pretty stressed that I won't get this done.