Hello, first timer here!
I am using my arduino uno to drive a servo and 2 DC motors. I am using the seeed studio motor shield. They 2 DC motors and the servo work seperately, but together they are not working.
The electronics should all be working, and I am driving the whole thing with a big battery.
When I program to move the DC motors, everything works. My setup function uses pins 8,11,12,13 as digital outputs and pins 9 and 10 as analog output for the pwm. When i enter my function forward(), the DC motors both rotate forward.
The servo motor is attached to pin 6. I used servo.attach(6). When i put servo.write(90) or any angle, it goes to that angle successfully. So the Servo part is working.
Now, I take the code of the 2 DC motors, a code that is perfectly working, and I add to it at the top of the program
#include <Servo.h>
Servo servoMain;
and in the setup I add servoMain.attach(6);
I compile and upload, but now the DC motors stop turning. The DC motors don't use pin 6 at all, so what could be the problem??
I would appreciate any help! Thanks!
Christopher414:
#include <Servo.h>
Servo servoMain; // Define our Servo
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int speedpinA=9;//enable motor A
int pinI3=12;//define I3 interface
int pinI4=13;//define I4 interface
int speedpinB=10;//enable motor B
int spead =180;//define the spead of motor
void setup()
{
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(speedpinA,OUTPUT);
pinMode(pinI3,OUTPUT);
pinMode(pinI4,OUTPUT);
pinMode(speedpinB,OUTPUT);
servoMain.attach(6); //If i just remove this line, the loop works and wheels rotate, if this is here, then the wheels don't move.
}
void forward() //GO FORWARD
{
digitalWrite(pinI4,HIGH);
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);
}
void backward() //GO BACK
{
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,HIGH);
digitalWrite(pinI1,LOW);
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);
}
void left() //TURN LEFT
{
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);
digitalWrite(pinI4,HIGH);
digitalWrite(pinI3,LOW);
digitalWrite(pinI2,HIGH);
digitalWrite(pinI1,LOW);
}
void right() //TURN RIGHT
{
digitalWrite(pinI4,LOW);
digitalWrite(pinI3,HIGH);
digitalWrite(pinI2,LOW);
digitalWrite(pinI1,HIGH);
analogWrite(speedpinA,spead);
analogWrite(speedpinB,spead);
}
void carstop() //STOP CAR
{
digitalWrite(speedpinA,LOW);
digitalWrite(speedpinB,LOW);
delay(1000);
}
void loop() //GO FORWARD A BIT THEN STOP.
{
forward();
delay(200);
while(1);
}