Hi,
1st of all, many people will think that it's the same post i done few hours ago but it's not the same
so please help if you know the answer in my problem, thanks.
I got a L298N on my balancing robot:
Fig. 1.1 L298N
My Code pins are:
#define ENA 5 //PWM
#define ENB 6 //PWM
#define IN1 7 //Digital pin
#define IN2 8 //Digital pin
#define IN3 2 //Digital pin
#define IN4 3 //Digital pin
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(ENA, OUTPUT);
pinMode(ENB, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(IN3, OUTPUT);
pinMode(IN4, OUTPUT);
My only code to balance the robot and control the H-Bridge L298N is this code in loop:
int sensorValueY = 0;
int sensorValueY2 = 0;
int motorPWMa = 0;
int motorPWMb = 0;
sensorValueY = compAngleY;
sensorValueY2 = compAngleY;
motorPWMa = map(sensorValueY, 5, 15, 100, 255);
motorPWMb = map(sensorValueY2, -5, -15, 100, 255);
//Backward
if (compAngleY > 5.00){
//turn clockwise Motor A
digitalWrite(IN1, HIGH);
digitalWrite(IN2, LOW);
analogWrite(ENA, motorPWMa);
//turn clockwise Motor B
digitalWrite(IN3, HIGH);
digitalWrite(IN4, LOW);
analogWrite(ENB, motorPWMa);
}
//Forward
if (compAngleY < -5.00){
//turn clockwise Motor A
digitalWrite(IN1, LOW);
digitalWrite(IN2, HIGH);
analogWrite(ENA, motorPWMb);
//turn clockwise Motor B
digitalWrite(IN3, LOW);
digitalWrite(IN4, HIGH);
analogWrite(ENB, motorPWMb);
}
if(compAngleY > 45 || compAngleY < -45){
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
}
delay(100);
I created 2 maping codes to create the speed between MPU6050 degree angle and the PWM of
the motors. When I use this code once wheel spins faster than the other (I tested with "case" code and they worked fine). After the robot gets over 20 degree angle or the robots movement is big
the motors start spin in a single direction (for no reason "no code") till it stops by itself (after 8+sec)
or until I reset the board.
Info:
For the MPU6050 i use Complimentary filter.
no other code it's used for H-Bridge (only what's in loop).
..etc
D60