Problem with using analogWrite() in arduino

I am using Arduino Mega, Dc Motor and Cytron 10A motor driver. I want to control the speed of the DC motor. I am using HC-05 to control the robot. Here is my code:

Arduino:
#define AN1 12
#define AN2 9
#define IN1 13
#define IN2 11
char inChar;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(AN1,OUTPUT);
pinMode(AN2,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
}

void loop() {

if (Serial.available() > 0)
{
inChar = Serial.read();
Serial.println(inChar);
}

if (inChar =='F')
{
analogWrite(AN1,100);
// analogWrite(AN2,100);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,HIGH);
}
if (inChar =='S')
{
analogWrite(AN1,0);
analogWrite(AN2,0);
digitalWrite(IN1,HIGH);
digitalWrite(IN2,HIGH);
}
}

Each motor is able to control speed when connected individually or commenting either analogWrite(AN1,100) or analogWrite(AN2,100). When both are present in code, code doesn't work. Is the problem due to higher baud rate?

However, my code works perfectly on using digitalWrite instead of analogWrite. Any help would be appreciated.

Also posted at:

Each motor is able to control speed when connected individually or commenting either analogWrite(AN1,100) or analogWrite(AN2,100). When both are present in code, code doesn't work. Is the problem due to higher baud rate?

What higher baud rate are you talking about?

Both pwm pins should be at 490 Hz.

I see nothing obviously wrong with your code. For trouble shooting, I would separate issues of the code/Arduino from those of the motor driver/motors.

Run you code with AN1 and AN2 outputs to leds on a breadboard. Be sure to use a series resistor and correct polarity with the led.

If you can get the leds to work, you can focus on the Cytron and the motors.

cattledog:
What higher baud rate are you talking about?

Both pwm pins should be at 490 Hz.

I see nothing obviously wrong with your code. For trouble shooting, I would separate issues of the code/Arduino from those of the motor driver/motors.

Run you code with AN1 and AN2 outputs to leds on a breadboard. Be sure to use a series resistor and correct polarity with the led.

If you can get the leds to work, you can focus on the Cytron and the motors.

I tried with LEDs, It doesnt work.

Also the baud rate of bluetooth is set as 115200 instead of 9600... Is that creating problem?

Also the baud rate of bluetooth is set as 115200 instead of 9600... Is that creating problem?

if (Serial.available() > 0)
  {
  inChar = Serial.read();
  Serial.println(inChar);
  }

Do you receive and print the 'F' and the 'S'?

If you disconnect the HC05 and take your input from the Serial Monitor, can you see the two leds illuminated at the same time?

Arduino Mega

Typically with a Mega you would put the bluetooth module on Serial1, and leave the monitor on Serial.

cattledog:

if (Serial.available() > 0)

{
  inChar = Serial.read();
  Serial.println(inChar);
  }




Do you receive and print the 'F' and the 'S'?

If you disconnect the HC05 and take your input from the Serial Monitor, can you see the two leds illuminated at the same time?

Typically with a Mega you would put the bluetooth module on Serial1, and leave the monitor on Serial.

I am able to receive F and S on Serial monitors.

I tried giving input from Serial monitor. Only One LED is On at a time. I dont know what is the problem with the code.

In which hardware serial port (TX0/RX0, TX1/RX1, TX2/RX2, TX3/RX3) of MEGA, you have connected your HC05 BT device.

Is it TX0/RX0 port?

TX/RX (PIN 0 AND 1).
It used to work perfectly before.

Your Serial Monitor is also connected with the same DPins -- do you know it? Now, there are two devices at the UART0 (TX0/RX0) Port which is not recommended. Try to change the connection of HC05 at TX1/RX1 (UART1) Port, upload the following sketch, and report the result.

#define AN1 12
#define AN2 9
#define IN1 13
#define IN2 11
char inChar;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial1.begin(115200);
  //pinMode(AN1, OUTPUT);
  //pinMode(AN2, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
}

void loop()
{
  byte n = Serial1.available();
  if (n != 0)
  {
    inChar = Serial1.read();
    Serial.println(inChar);
    //---------------------
    if (inChar == 'F')
    {
      analogWrite(AN1, 100);
      analogWrite(AN2,100);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, HIGH);
    }
    
    if (inChar == 'S')
    {
      analogWrite(AN1, 0);
      analogWrite(AN2, 0);
      digitalWrite(IN1, HIGH);
      digitalWrite(IN2, HIGH);
    }
  }
}

I tried giving input from Serial monitor. Only One LED is On at a time.

I do not have a Mega to test, but your two pwm outputs are on pins using different timers and should be independent.

Have you tried different pwm pins?

It used to work perfectly before.

What does this mean? What changed?

cattledog:
I do not have a Mega to test, but your two pwm outputs are on pins using different timers and should be independent.

Have you tried different pwm pins?

What does this mean? What changed?

Nothing changed,
Same hardware, same wires, same hc-05.

Anyways, I tested it with NRF instead of Bluetooth and its working fine.
I thing there is some problem with RX0/TXO.

I will try using hc-05 on RX1/TX1 in sometime.

Thank you for the help.