Hello, i'm very new on Arduino world but i have some experience of programming in other language.
i'm try to make my first robot car, i have 2 DC motor connect to a module L298N , all connected to a 9V battery and arduino uno.
DC motor connected to port 5,6,7 for the motor 1 and port 8,9,10 for motor 2
the code working fine to go forward and back.
At this stage I want to connect a dc servo , which i connected to the port 13, to the 5 volt and to ground, now here the issue ..
with the following code only one DC motor and servo moving but the second DC motor is stuck!
i notice that if i remove from the void setup the command "servo_motor.attach(13);" both dc motor running.
it should move servo and both DC motor...
any reason why?
thanks for help
Dm1886
#include <Arduino.h>
#include <Servo.h>
const int mot2 = 10;
const int ava2 = 9;
const int ind2 = 8;
const int mot1 = 5;
const int ava1 = 6;
const int ind1 = 7;
Servo servo_motor; // create servo object to control a servo
int pos = 0;
void moveForward()
{
Serial.print("Going Forward\n");
// turn on motor A
digitalWrite(ava1, HIGH);
digitalWrite(ind1, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(mot1, 100);
// turn on motor B
digitalWrite(ava2, LOW);
digitalWrite(ind2, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(mot2, 100);
delay(2000);
}
void moveBack()
{
Serial.print("Going BACK\n");
// turn on motor A
digitalWrite(ava1, LOW);
digitalWrite(ind1, HIGH);
// set speed to 200 out of possible range 0~255
analogWrite(mot1, 250);
// turn on motor B
digitalWrite(ava2, HIGH);
digitalWrite(ind2, LOW);
// set speed to 200 out of possible range 0~255
analogWrite(mot2, 250);
delay(2000);
}
void moveServo(){
for (pos = 0; pos <= 180; pos += 1) {
servo_motor.write(pos);
delay(15);
}
for (pos = 180; pos >= 0; pos -= 1) {
servo_motor.write(pos);
delay(15);
}
}
void setup()
{
Serial.begin(9600);
servo_motor.attach(13); // why if i remove this both DC motor work and if i put only one DC motor work??
pinMode(mot2, OUTPUT);
pinMode(ava2, OUTPUT);
pinMode(ind2, OUTPUT);
pinMode(mot1, OUTPUT);
pinMode(ava1, OUTPUT);
pinMode(ind1, OUTPUT);
}
void loop()
{
moveServo();
delay(2000);
moveForward();
delay(2000);
moveBack();
}