I'm making my own bluetooth rc-car. But my code is not really working well. I can't steer left or right. I can only go forward and backwards but after 1 min the arduino freezes our something and keeps repeating the last input. I don't understand why it's not working because I looked at other codes and they are almost the same and they work perfect. I would appreciate it if someone could help me with this. thanks in advance!
code:
// right motors
int Input_MC_1A = 6;
int Input_MC_1B = 9;
//left motors
int Input_MC_2A = 10;
int Input_MC_2B = 11;
char commando;
void setup() {
pinMode(Input_MC_1A, OUTPUT);
pinMode(Input_MC_1B, OUTPUT);
pinMode(Input_MC_2A, OUTPUT);
pinMode(Input_MC_2B, OUTPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
pinMode(7, OUTPUT);
digitalWrite(7, LOW);
}
void loop() {
if(Serial.available()>0)
{
commando = Serial.read();
switch(commando)
{
case 'F':
forward();
break;
case 'B':
backward();
break;
case 'L':
left();
break;
case 'R':
right();
break;
case 'a': digitalWrite(7, HIGH);
break;
case 'b': digitalWrite(7, LOW);
break;
default: break;
}
}
}
void forward()
{
analogWrite(3, 100);
analogWrite(5, 100);
//RightMotor
digitalWrite(Input_MC_1A, HIGH); // clockwise
digitalWrite(Input_MC_1B, LOW); // against the clock
//LeftMotor
digitalWrite(Input_MC_2A, HIGH); // clockwise
digitalWrite(Input_MC_2B, LOW); //against the clock
}
void backward()
{
analogWrite(3, 100);
analogWrite(5, 100);
//RightMotor
digitalWrite(Input_MC_1A, LOW); // clockwise
digitalWrite(Input_MC_1B, HIGH); //against the clock
//LeftMotor
digitalWrite(Input_MC_2A, LOW); // clockwise
digitalWrite(Input_MC_2B, HIGH); // against the clock
}
void left()
{
analogWrite(3, 100);
analogWrite(5, 100);
//RightMotor
digitalWrite(Input_MC_1A, HIGH); // clockwise
digitalWrite(Input_MC_1B, LOW); // against the clock
//LeftMotor
digitalWrite(Input_MC_2A, LOW); // clockwise
digitalWrite(Input_MC_2B, HIGH); // against the clock
}
void right()
{
analogWrite(3, 100);
analogWrite(5, 100);
//RightMotor
digitalWrite(Input_MC_1A, LOW); // clockwise
digitalWrite(Input_MC_1B, HIGH); //against the clock
//LeftMotor
digitalWrite(Input_MC_2A, HIGH); // clockwise
digitalWrite(Input_MC_2B, LOW); //against the clock
}
Thank you for answering my left and right works now but when I click 1 time forward, backward, left or right on the controller app it executes but then it blocks and loses connection with my HC-05. Do you know why this is happening?
As previously stated, the only way the phone can disconnect from the HC05 is if the module Vcc drops too low when the motors run.
You can not power an Arduino with 5v on the Vin pin. That pin is before the voltage regulator and requires at least 7 volts. Using the 5v output from the LN298 to power through the Vin is not correct. Better would be to take the 9v of the battery directly to Vin.
Explain more about the 9v battery. What kind is it and what current can it supply.
With an adequate power supply to the Arduino, the 5v pin should work fine as Vcc for the HC05 module.
My HC-05 no longer loses the connection because I connected my arduino directly to the 9v battery. Forward and backward now works perfectly but if I want to go left and right my motors need more power to turn but if I give my motors more power by increasing my analog power to 200 it still loses the connection. The battery I use is a Panasonic Pro Power 9v battery. Do you recommend another battery? Thank you so much for all the help
What are the specifications on the motors? Unless you know what the current draw of the motors is you are working blind.
How long does the car need to run before recharging the batteries?
Can you change the LN298 to a more modern motor controller which less of a voltage drop across it? You could use a 7.2v NiMh battery pack instead of 9v.