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.
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.
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.
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.