I'm trying to control the speed of a dc motor with MPU6050, IBT-2, and Arduino UNO. This is the code that I'm uploading on Arduino. The motor doesn't even start when i turn the power on. What's wrong with the code, any idea?
#include <Wire.h>
#include <MPU6050.h>
MPU6050 sensor ;
int16_t ax, ay, az ;
int16_t gx, gy, gz ;
int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
void setup()
{
Wire.begin ( );
Serial.begin (9600);
sensor.initialize ( );
pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);
}
void loop()
{
sensor.getMotion6 (&ax, &ay, &az, &gx, &gy, &gz);
ax = map (ax, -17000, 17000, 0, 1023) ;
int sensorValue = ax;
// sensor value is in the range 0 to 1023
// the lower half of it we use for reverse rotation; the upper half for forward rotation
if (sensorValue < 512)
{
// reverse rotation
int reversePWM = -(sensorValue - 511) / 2;
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);
}
else
{
// forward rotation
int forwardPWM = (sensorValue - 512) / 2;
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);
}
}