Strange effect when attaching servo

Hi. I'm having a problem with my arduino nano trying to run a motor and a steer a servo with a joystick. When I use the line servo.attach(servoPin); the motor only spin when the joystick is at full throttle. It's the same even if the servo isn't physically connected to the arduino. It seems to be only that line of code that mess it up because when i comment it out everything works fine and the motor can spin at different speeds.

The motor is connected to a separate power source and I'm controlling it through a L293 controller connected to pin 9. When the servo is connected i use the 5V output on the nano and pin 6 to control it but as i said, the servo doesn't even need to be connected for it to act weird.

Any ideas?

(deleted)

Thank you very much! It's working now. I had no clue that a library could disable pins like that.

lefeh:
Thank you very much! It's working now. I had no clue that a library could disable pins like that.

It's not that it disables the pins, but that the two functions would be trying to use the same hardware resource for the same thing at the same time, which causes problems.

In this case, the resource is one of the Timer peripherals. The Timers can be used for several different things, like generating periodic interrupts or creating waveforms like PWM on an output pin. The Arduino core by default sets up all the Timers for PWM. That's why analogWrite works on all of them right out of the box. There's 3 Timers, and each of them has 2 pins that can be used for waveform generation. That's why there's 6 PWM pins (on an Uno or Uno derivative), and why they are locked onto specific pins.

The Servo library takes one of those Timers (usually Timer2) and reconfigures it to generate a servo control signal. Reconfiguring it means that it will no longer work properly for PWM generation unless you reset it back to the way the Arduino core had originally configured it. And if you reconfigure it for PWM, it won't work properly for generating the servo signal.

If I drive my car to the grocery store, my brother can't use it to drive to the movie theater until I'm done with it. It's the same basic concept.

Oh i see, that's some great info. Thanks for taking the time to explain!