Use Servo and SoftwareSerial together on UNO

First of all sorry for the delay, and thanks a lot for all of the reactions! Replaying to everyone:

Robin2:
I always thought that SoftwareSerial would work alongside ServoTimer2 - which is NOT to rule out other combinations.

The problem with the SoftwareSerial and the standard servo library is that they both use the same hardware Timer.

You need to post the program you tried and describe what it actually does and what you want it to do that is different.

...R

It really doesn't. For example this code causes the servo to twitch whenever SoftwareSerial communication is happening, while the servo works fine if I either just use hardware serial.

-dev:
...or you could try a different approach. Use Serial for the serial device. You have to disconnect pin 0 to upload new sketches over USB. Or get an ISP and leave it connected.

You didn't identify the serial device, so I can't tell whether you could still do debug prints. Some devices ignore data that isn't in its required format (e.g., GPS devices).

Even if you can't do debug prints to Serial, you might be able to use AltSoftSerial for debug prints. Connect pins 8 & 9 to a TTL Serial-to-USB adapter (aka FTDI). AltSoftSerial is the best software serial library, but it disables PWM on pin 10 (on an UNO).

Need more information, including the code. Loop structure is very important.

I can't use hardware serial because I need both usb serial output and serial communication over bluetooth with a HC-05 module, so yes it's a general serial device. I tried AltSoftSerial as well, but I think that library occasionally dropped serial communication bytes when used together with a servo.

For code see above, and no loop structure to speak of.

pylon:
No, SoftwareSerial does not use a timer. But it's programmed so badly that it blocks the complete CPU during send out and reception of complete bytes. As the timer interrupts are not handled during this time, the signaling for the servos get out of sync.

You might get it to work if you let the hardware PWM do all the servos stuff and use one of the interrupt serial emulations but the best solution is definitely as -dev suggested: use the hardware serial interface. If you need more than one serial interface, change the hardware platform (Leonardo has one additional hardware serial, Mega2560 has 3 additional hardware serial interfaces).

Thanks, didn't know the issue wans't a timer! I'll consider changing platform if it turns out this approach really doesn't work.

slipstick:
The couple of times I've tried SoftwareSerial and ServoTimer2 have worked together fine.

The only thing you need to be aware of is that ServoTimer2.write() takes a pulse length in microseconds not a 0-180 angle, so it is equivalent to Servo.writeMicroseconds() not Servo.write(). If you try doing write(90) nothing will happen because 90 microseconds is below the minimum.

Steve

Didn't know that, thanks! I tried a couple of small and big values for the write function, but the servo always twitches while serial communication is happening.