Go Down

Topic: problem with SoftwareSerial and servo libraries (Read 2785 times) previous topic - next topic


I am using Arduino UNo with  GPS data going into pin 3
and a servo connected to pin 9 - nothing else.

I am using 3 libraries

but my problem seems to be between SoftwareSerial and Servo

If I install the demo Servo code everything works fine.

I have simplified my code as much as pos - to show the problem
(No code at all in the loop, and no TinyGPS library)

If i include the line (see code below)

then i get random erratic movement from the servo on pin 9 every second - even though the
myservo.write(pos); is commented out.

(I need "ss.begin(9600)"  as I am using TinyGPS.)

If I comment out this code then I can get the servo to behave (but I cant then use TinyGPS which needs this line.)
ANy ideas anyone - I am completely stuck
SoftwareSerial seems to be sending something down pin 9??


Code: [Select]
#include <Servo.h>
#include <SoftwareSerial.h>

SoftwareSerial ss(3, 4);

Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created
  int pos = 0;    // variable to store the servo position

void setup()
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

//next line cases the problems

void loop()
    //pos = random(5);




many thanks for link - It confirms there is a problem but doesn't seem to come up with any workaround.
ANy ideas anyone?

Thanks all



The current SoftwareSerial library doesn't use timer 2 (it doesn't use any timer) and Servo uses timer 1 (and not 2), so the solution presented in the adafruit forum won't work. Do you use the IDE 1.0.1?

You say the single line of "ss.begin(9600);" is enough for the Servo to go wild? Is this only if the GPS is attached or also without the GPS? The ss.begin() method installs a pin change interrupt for the reception pin you specified. This interrupt routine is called every time an edge is detected on that pin. The routine is controlling the processor for the whole time a byte needs to be transferred. So if you have a busy serial line attached your processor is doing almost nothing else than receiving bits on that line. Try to change to the hardware serial because there the hardware is handling all that stuff interrupting the processor only for a very short time to store the received byte in the buffer.

Go Up