Two servo pins doesn't work at the same time

I have found somekind of explanation to my problem in here, but I can't solve it.

If have realized that when I define two servo pins with pinMode()-function, my arduino does just blink once the ledlight, waits some time and blinks again.

Here's my code:

int ledPin = 13;

void roller(int servoPin, int pulseLen) {
  digitalWrite(servoPin, HIGH);
  delayMicroseconds(pulseLen);
  digitalWrite(servoPin, LOW);
  delay(20);
}

void setup() {
  Serial.begin(115200);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
}

void loop() {
   for (int i = 0; i <= 15; i++) {
   roller(2, 1800);
   roller(3, 1200);
   }
}

So if I define two pins with pinMode(), the servos doesn't work. Otherwise if I delete another of these defines, the arduino doesn't work. It doesn't matter wich one I delete. If I delete that one wich defines pin 2, the arduino works and servo in pin 3 works. If I delete define wich defines pin 3, the other servo works.

This is a little bit weird. Anyone have an answer or any questions?

Edit. I use Arduino Bluetooth ATMEGA 328

How are you powering the servos, if it is trough the usb cable you are pulling to much current and that pull the voltage so low that the arduino keeps being reset.

How are you powering the servos, if it is trough the usb cable you are pulling to much current and that pull the voltage so low that the arduino keeps being reset.

I have 4,5 volts battery and my servos which I'm using are SM-S4303R -models. (Pololu - SpringRC SM-S4303R Continuous Rotation Servo)

But problem doesn't depend on am I trying to run the servos. Even if I just define them the arduino doesn't work and led just blinks one again.

Maybe the servos don't like the 40ms+ frame length?

Any reason not to use the libraries?

I have 4,5 volts battery and my servos which I'm using are SM-S4303R -models

The link shows those to be ex-servos, ie they've been modified for continuous rotation.

Are you running the servos and the Arduino off the 4.5V battery?
The fact that the LED blinks suggests a brown-out, with the processor resetting.
Try running the servos off a battery, and the Arduino off the USB.

Maybe the servos don't like the 40ms+ frame length?

Maybe, but if I can't even define two servos with pinMode()-functions what's the point?

Are you running the servos and the Arduino off the 4.5V battery?
The fact that the LED blinks suggests a brown-out, with the processor resetting.
Try running the servos off a battery, and the Arduino off the USB.

Yes, I'm trying to run it with 4,5V battery.
I forget to say that arduino what I use is Arduino BT. So I can't use USB.

Maybe, but if I can't even define two servos with pinMode()-functions what's the point?

What's the point of doing it wrong? Not much I agree. As you've been told before, use the Servo library instead.

Korman

If I delete that one wich defines pin 2, the arduino works and servo in pin 3 works. If I delete define wich defines pin 3, the other servo works.

Your problem is either:-

  1. Your battery can't supply enough current for two servos.
  2. Your servos are generating so much noise on the power rail you are resetting the arduino.
    Possible Solutions:-
  3. Use a separate battery to power the motor and the arduino (but connect the grounds together)
  4. Add some large decoupling capacitors across the supply, 47uF or larger along with a 0.1uF ceramic in parallel.

Your problem is either:-

  1. Your battery can't supply enough current for two servos.
  2. Your servos are generating so much noise on the power rail you are resetting the arduino.
    Possible Solutions:-
  3. Use a separate battery to power the motor and the arduino (but connect the grounds together)
  4. Add some large decoupling capacitors across the supply, 47uF or larger along with a 0.1uF ceramic in parallel.

Ok. I have to test those options tomorrow. But I am amazed. Because can even defining of ports with pinMode()-functions took so much power?

Because can even defining of ports with pinMode()-functions took so much power?

Once a pin is defined as an output it is driving the pin at a zero or a one, so defining the pin to be an output not only makes it an output but also drives it to one level.

Note there is absolutely no need for the line:-
for (int i = 0; i <= 15; i++) {
because loop() repeats the code, all it will do is add a bit of jitter to the motion.

I did get for my servos another batteries and now they work kind of. I have lots of bugs to fix.

Once a pin is defined as an output it is driving the pin at a zero or a one, so defining the pin to be an output not only makes it an output but also drives it to one level.

Ok. I try to understand. My english isin't the best possible and thease things are so new and mystical for me.

Note there is absolutely no need for the line:-
for (int i = 0; i <= 15; i++) {
because loop() repeats the code, all it will do is add a bit of jitter to the motion.

That's true. I copypasted it from my bigger source and forget to remove that.