I am facing an issue with the Serial monitor stopping all print statements and the execution of program also in the Mega. I am using the Serial monitor to give inputs to the mega to decide on a case statement to execute which sets PWM values for given pins connected to a motor driver. If I attempt to add code to use an I2C based IMU (MPU6050) into the program, the execution of the program is halted and the motors continue to move in the state it was moving in before the program stopped executing. This only happens if the power supply to the motor driver (through an SMPS) is turned on. The code used for this issue is given below and the issue occurs if all the commented parts are uncommented.
#include <Wire.h>
#include <MPU6050_light.h>
MPU6050 mpu(Wire);
int RPWM_Output1 = 3;
int LPWM_Output1 = 2;
int RPWM_Output2 = 5;
int LPWM_Output2 = 6;
int RPWM_Output3 = 7;
int LPWM_Output3 = 8;
int RPWM_Output4 = 9;
int LPWM_Output4 = 10;
int RPWM_Output5 = 11;
int LPWM_Output5 = 12;
char input;
void setup()
{
Serial.begin(115200);
Serial.println("HelloThere");
byte status = mpu.begin();
while(status!=0){ }
Serial.println(F("Calculating offsets, do not move MPU6050 1"));
delay(2000);
mpu.calcOffsets(true,true);
pinMode(RPWM_Output1, OUTPUT);
pinMode(LPWM_Output1, OUTPUT);
pinMode(RPWM_Output2, OUTPUT);
pinMode(LPWM_Output2, OUTPUT);
pinMode(RPWM_Output3, OUTPUT);
pinMode(LPWM_Output3, OUTPUT);
pinMode(RPWM_Output4, OUTPUT);
pinMode(LPWM_Output4, OUTPUT);
pinMode(RPWM_Output5, OUTPUT);
pinMode(LPWM_Output5, OUTPUT);
setMotor(511, LPWM_Output1, RPWM_Output1);
setMotor(511, LPWM_Output2, RPWM_Output2);
setMotor(511, LPWM_Output3, RPWM_Output3);
setMotor(511, LPWM_Output4, RPWM_Output4);
setMotor(511, LPWM_Output5, RPWM_Output5);
}
void loop()
{
// mpu.update();
// Serial.println(mpu.getAngleX());
if(Serial.available()){
input = Serial.read();
}
switch(input)
{
case 'w':
setMotor(511, LPWM_Output1, RPWM_Output1);
setMotor(600, LPWM_Output2, RPWM_Output2);
setMotor(422, LPWM_Output3, RPWM_Output3);
break;
case 's':
setMotor(511, LPWM_Output1, RPWM_Output1);
setMotor(422, LPWM_Output2, RPWM_Output2);
setMotor(600, LPWM_Output3, RPWM_Output3);
break;
case 'a':
setMotor(333, LPWM_Output1, RPWM_Output1);
setMotor(600, LPWM_Output2, RPWM_Output2);
setMotor(600, LPWM_Output3, RPWM_Output3);
break;
case 'd':
setMotor(689, LPWM_Output1, RPWM_Output1);
setMotor(422, LPWM_Output2, RPWM_Output2);
setMotor(422, LPWM_Output3, RPWM_Output3);
break;
case '[':
setMotor(600, LPWM_Output1, RPWM_Output1);
setMotor(600, LPWM_Output2, RPWM_Output2);
setMotor(600, LPWM_Output3, RPWM_Output3);
break;
case ']':
setMotor(422, LPWM_Output1, RPWM_Output1);
setMotor(422, LPWM_Output2, RPWM_Output2);
setMotor(422, LPWM_Output3, RPWM_Output3);
break;
case 'q':
setMotor(322, LPWM_Output1, RPWM_Output1);
setMotor(542, LPWM_Output2, RPWM_Output2);
setMotor(542, LPWM_Output3, RPWM_Output3);
break;
case 'e':
setMotor(700, LPWM_Output1, RPWM_Output1);
setMotor(480, LPWM_Output2, RPWM_Output2);
setMotor(480, LPWM_Output3, RPWM_Output3);
break;
case 'i':
Serial.println("In case i");
setMotor(800, LPWM_Output4, RPWM_Output4);
break;
case 'k':
Serial.println("In case k");
setMotor(222, LPWM_Output4, RPWM_Output4);
break;
case 'l':
Serial.println("In case l");
setMotor(650, LPWM_Output5, RPWM_Output5);
break;
case 'j':
Serial.println("In case j");
setMotor(372, LPWM_Output5, RPWM_Output5);
break;
case 'o':
Serial.println(" **STOP** ");
setMotor(511, LPWM_Output1, RPWM_Output1);
setMotor(511, LPWM_Output2, RPWM_Output2);
setMotor(511, LPWM_Output3, RPWM_Output3);
setMotor(511, LPWM_Output4, RPWM_Output4);
setMotor(511, LPWM_Output5, RPWM_Output5);
}
delay(100);
}
void setMotor(int Motorval, int LPWM_Output, int RPWM_Output)
{
if (Motorval <= 512)
{
int reversePWM = -(Motorval - 511) / 2;
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);
}
else
{
int forwardPWM = (Motorval - 512) / 2;
analogWrite(RPWM_Output, 0);
analogWrite(LPWM_Output, forwardPWM);
}
}