Go Down

Topic: only one servo spins (Read 529 times) previous topic - next topic

jessabu

Hello. I'm doing a project including servos, DC motors, and sensors. Almost everything else works, except the servos. Only one spins.

this is the code I was using before I modified part of it.
Code: [Select]

#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

void setup() {
  servoLeft.attach(10);  // Set left servo to digital pin 10
  servoRight.attach(9);  // Set right servo to digital pin 9
}

void loop() {            // Loop through motion tests
  forward();             // Example: move forward
  delay(2000);           // Wait 2000 milliseconds (2 seconds)
  reverse();
  delay(2000);
  turnRight();
  delay(2000);
  turnLeft();
  delay(2000);
  stopRobot();
  delay(2000);
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoLeft.write(0);
  servoRight.write(180);
}

void reverse() {
  servoLeft.write(180);
  servoRight.write(0);
}

void turnRight() {
  servoLeft.write(180);
  servoRight.write(180);
}
void turnLeft() {
  servoLeft.write(0);
  servoRight.write(0);
}

void stopRobot() {
  servoLeft.write(90);
  servoRight.write(90);
}


since the servos I'm using aren't continuous rotation servos, I modified the code so it would be like this:

Code: [Select]

#include <Servo.h>

Servo servoLeft;          // Define left servo
Servo servoRight;         // Define right servo

int pos = 0;

void setup() {
  servoLeft.attach(1);  // Set left servo to digital pin 10
  servoRight.attach(2);  // Set right servo to digital pin 9
}

void loop() {            // Loop through motion tests
  forward();             // Example: move forward
  delay(2000);           // Wait 2000 milliseconds (2 seconds)
  reverse();
  delay(2000);
  stopRobot();
  delay(2000);
}

// Motion routines for forward, reverse, turns, and stop
void forward() {
  servoLeft.write(0);
  servoRight.write(180);
}

void reverse() {
  servoLeft.write(180);
  servoRight.write(0);
}

void stopRobot() {
  servoLeft.write(90);
  servoRight.write(90);
}


the second code compiled and uploaded, but only one servo spun.
the schematic for the two servos is at this link: http://www.robotoid.com/appnotes/arduino-operating-two-servos.html

thank you for all your help.

TolpuddleSartre

Different pins between the two sketches.
Significant?
(Personally, I wouldn't use a serial pin)

outsider

Pin 1 is one of the serial comm pins (TX), what you had (9 & 10) should have worked. Are you sure both servos can move through the entire 0 ... 180 degree range? You're not trying to power 2 servos from the Arduino's 5V pin, are you?

jremington

#3
May 02, 2018, 02:41 am Last Edit: May 02, 2018, 02:42 am by jremington
Be sure to use a separate power supply for motors and servos, and don't forget to connect all the grounds.

vinceherman

Have you tried unplugging the working servo and plugging in the non-working servo where the working servo was plugged?

bos1714

Hi there!

Usually the pin that the servo uses needs to be a PWM pin. Pins 1 and 2 are usually not PWM pins, which would indicate the issue.

There are some boards like the Arduino Mega that have PWM capabilities on these pins. Can you tell me what board you are using?

As others have said, it is usually not the greatest idea to use the Tx and Rx pins (1 and 0) as signals unless necessary, as it can add problems.

That being said, try returning to pins 9 and 10 for the two servo signal pins with your modified code and see what happens!

Good luck  :)
Time line? Time isn't made out of lines. It is made out of circles. That is why clocks are round.

slipstick

Usually the pin that the servo uses needs to be a PWM pin. Pins 1 and 2 are usually not PWM pins, which would indicate the issue.
I don't know where you got your ideas from but the Servo library does not need PWM pins. It works on any digital pin. That is at least in part because servos are NOT driven with standard PWM.

Steve

MarkT

#7
May 03, 2018, 12:02 am Last Edit: May 03, 2018, 12:04 am by MarkT
Usually the pin that the servo uses needs to be a PWM pin. Pins 1 and 2 are usually not PWM pins, which would indicate the issue.
You are confused, the Servo library works with any pin at all, analog(*) or digital (at least on the ATmega
Arduinos).  Its an interrupt driven library, and is documented if you have a look...

Pin 1 is serial TX, it may be clashing due to this.

(*) For those analog pins that are also mapped to a I/O port.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Tofer

Please show your schematics, cuz sketch is ok.

jessabu

I made sure both grounds were connected, and it worked! Thank you for all the help  :)

jessabu

I also changed the pins I was using for the servos from 1 and 2 to 9 and 10.

Go Up