I am making a pid line follower robot. So I was tinkering with code to optimize my line follower when i removed two lines of code unknowingly. As soon as I uploaded my code and started the line follower, 4 mosfets on my motor driver smoked and burst into blue flames. Bought a new one and same outcome. Interestingly, The ones corresponding to the left motor alone had this outcome as I removed " return Motorspeed1" and " return MotorSpeed2" from the function corresponding to the left motor. Heres the code :
signed int MotorSpeed = 70;
int MotorSpeed1, MotorSpeed2;
int MotorFunc(signed int a, signed int b){
if(a >= -10 && a < 0)
{
b = -b;
digitalWrite(_brk_m1, LOW);
digitalWrite(_brk_m1, LOW);
digitalWrite(_dir_m1, HIGH);
digitalWrite(_dir_m2, LOW);
MotorSpeed1 = MotorSpeed + b;
MotorSpeed2 = MotorSpeed + b;
MotorSpeed1 = constrain(MotorSpeed1, 0, 255);
MotorSpeed2 = constrain(MotorSpeed2, 0, 255);
analogWrite(_pwm_m1, MotorSpeed1);
analogWrite(_pwm_m1, MotorSpeed2);
return MotorSpeed1;
return MotorSpeed2;
}
if (a < 10 && a > 0)
{
digitalWrite(_brk_m1, LOW);
digitalWrite(_brk_m2, LOW);
digitalWrite(_dir_m1, LOW);
digitalWrite(_dir_m2, HIGH);
MotorSpeed1 = MotorSpeed+ b;
MotorSpeed2= MotorSpeed+ b;
MotorSpeed1 = constrain( MotorSpeed1, 0, 255);
MotorSpeed2= constrain( MotorSpeed2, 0, 255);
analogWrite( _pwm_m1, MotorSpeed1);
analogWrite( _pwm_m2, MotorSpeed2);
return MotorSpeed1;
return MotorSpeed2;
}
else
{
digitalWrite(_brk_m1, LOW);
digitalWrite(_brk_m2, LOW);
digitalWrite(_dir_m1, HIGH);
digitalWrite(_dir_m2, HIGH);
MotorSpeed1 = MotorSpeed+ b;
MotorSpeed2 = MotorSpeed - b;
MotorSpeed1 = constrain(MotorSpeed1, 0, 255);
MotorSpeed1 = constrain(MotorSpeed2, 0, 255);
analogWrite( _pwm_m1, MotorSpeed1);
analogWrite( _pwm_m2, MotorSpeed2);
return MotorSpeed1;
return MotorSpeed2;
}
}
And i called this function in the code using :
MotorFunc(a,b)
This code ran just fine, But when i removed the return functions from the first if condition, I destroyed 2 motordrivers, Is this possible? Edit: I am updating the whole code:
The main code tab :
int _dir_m1 = 2;
int _pwm_m1 = 3;
int _brk_m1 = 4;
int _brk_m2 = 5;
int _pwm_m2 = 6;
int _dir_m2 = 7;
int sen1 = A4;
int sen2 = A3;
int sen3 = A2;
int sen4 = A1;
int sen5 = A0;
signed int Error = 0;
int Kp = 20;
int Ki = 0;
int Kd = 0;
void setup() {
pinMode(_dir_m1, OUTPUT);
pinMode(_pwm_m1, OUTPUT);
pinMode(_brk_m1, OUTPUT);
pinMode(_dir_m2, OUTPUT);
pinMode(_pwm_m2, OUTPUT);
pinMode(_brk_m2, OUTPUT);
pinMode(sen1, INPUT);
pinMode(sen2, INPUT);
pinMode(sen3, INPUT);
pinMode(sen4, INPUT);
pinMode(sen5, INPUT);
}
void loop(){
signed int a = Sensors();
signed int b = PID(a);
MotorFunc(a,b);
}
And pretty much it. I am leaving out PID as Its not required along with error selection.
The setup is -
Arduino mega connected to 5 ir sensors
12v 9ah battery
500rpm 12v motors johnsons
and the motor driver 20 amp 20v-30v