So the idea is to control the motor using L289N and arduino. the arduino will read from MU6050 sensor and will control the motor depending on the sensor value.
however, the program loops about 10 times and stops(around 10 times, not fix everytime), and i realize that the program works longer when i supply a lower voltage( i tried with 4v and 8v), the motor is rated 3v-12v.
I am really desperate.
#include "Wire.h" // This library allows you to communicate with I2C devices.
const int MPU_ADDR = 0x68; // I2C address of the MPU-6050. If AD0 pin is set to HIGH, the I2C address will be 0x69.
double accelerometer_x, accelerometer_y, accelerometer_z; // variables for accelerometer raw data
double gyro_x, gyro_y, gyro_z; // variables for gyro raw data
double temperature; // variables for temperature data
char tmp_str[7]; // temporary variable used in convert function
char* convert_int16_to_str(int16_t i) { // converts int16 to string. Moreover, resulting strings will have the same length in the debug monitor.
sprintf(tmp_str, "%6d", i);
return tmp_str;
}
void setup() {
Serial.begin(9600);
Wire.begin();
Wire.beginTransmission(MPU_ADDR); // Begins a transmission to the I2C slave (GY-521 board)
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
pinMode(3,OUTPUT);
pinMode(5,OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);// this is just to make the debugging easier, you can ignore this.
}
void loop() {
float totalx = 0;
float totaly = 0;
float totalz = 0;
for (int i = 0; i<50;i++){
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) [MPU-6000 and MPU-6050 Register Map and Descriptions Revision 4.2, p.40]
Wire.endTransmission(false); // the parameter indicates that the Arduino will send a restart. As a result, the connection is kept active.
Wire.requestFrom(MPU_ADDR, 72, true); // request a total of 72=14 registers
accelerometer_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x3B (ACCEL_XOUT_H) and 0x3C (ACCEL_XOUT_L)
accelerometer_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x3D (ACCEL_YOUT_H) and 0x3E (ACCEL_YOUT_L)
accelerometer_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x3F (ACCEL_ZOUT_H) and 0x40 (ACCEL_ZOUT_L)
temperature = Wire.read()<<8 | Wire.read(); // reading registers: 0x41 (TEMP_OUT_H) and 0x42 (TEMP_OUT_L)
gyro_x = Wire.read()<<8 | Wire.read(); // reading registers: 0x43 (GYRO_XOUT_H) and 0x44 (GYRO_XOUT_L)
gyro_y = Wire.read()<<8 | Wire.read(); // reading registers: 0x45 (GYRO_YOUT_H) and 0x46 (GYRO_YOUT_L)
gyro_z = Wire.read()<<8 | Wire.read(); // reading registers: 0x47 (GYRO_ZOUT_H) and 0x48 (GYRO_ZOUT_L)
totalx = totalx + accelerometer_x;
totaly = totaly + accelerometer_y;
totalz = totalz + accelerometer_z;
}
float averagex= totalx/50.0/16384.0;
float averagey= totaly/50.0/16384.0;
float averagez= totalz/50.0/16384.0;
if(averagex>=0){
analogWrite(3,200);
analogWrite(5,0);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println(averagex);
Serial.println("left");}
else if (averagex<0){
analogWrite(3,0);
analogWrite(5,200);
digitalWrite(LED_BUILTIN, LOW);
Serial.println(averagex);
Serial.println("right");
}
