Servos & Software Serial?

I have a Mega reading sensors and sending serial data to a Uno once every 3 Seconds over a serial link. I am using pin 18 (Tx2) on the Mega to send data to pin12 and Software Serial to receive the data with the Uno.

The Uno is being powered from an external 12V bench supply and a separate 6V regulator to power the RC servos, all on the same ground. There is extensive filtering on the 12V and 6V supplies and capacitive bypassing on the servo PWM lines (pins 9, 10, and 11).

The Mega is powered from the USB port on the PC but the ground is tied to the ground on the Uno.

Each burst of serial data (48 bytes @300 BAUD) cause the servos to jump considerably in a positive (longer pulse) direction, as if the pulses to the servo pins have been stretched. (I haven't connected the storage scope to the servos yet to confirm.)

Is there any chance that Software Serial is interfering with the servo pins? Maybe writing them to inputs or setting a different output state?

Do I need to declare my servo pins as outputs and my serial receive pin as an input to prevent such interference?

Any suggestions appreciated!

Try the ServoTimer2 library. The standard Servo library does not work with SoftwareSerial.

Note that the ServoTimer2 library expects values in microsecs rather than degrees.

...R

Thanks Robin!

I have one thing I want to try before I switch everything to ServoTimer2.

Tried using servo.detach and some delays to separate Serial from Servo - still glitched.

Maybe I should swap out the Uno board for a Mega? I seem to remember that the Mega doesn't have timer conflicts between Serial and Servo. Must research timers on Mega....

Because a Mega has 3 spare HardwareSerial ports you probably won't need to use SoftwareSerial at all. And compared to HardwareSerial SoftwareSerial is just a kludge.

...R

Robin2: Because a Mega has 3 spare HardwareSerial ports you probably won't need to use SoftwareSerial at all. And compared to HardwareSerial SoftwareSerial is just a kludge.

...R

This is on the Uno that the Mega is talking too I think.

SoftwareSerial uses timer1, Servo uses timer1 (on an Uno). This sort of thing you will notice if you carefully read the 'documentation' with a library (ie Servo.h and SoftwareSerial.h)

MarkT:
This is on the Uno that the Mega is talking too I think.

I was responding to the last sentence in Reply #2. Apologies if that was not clear.

…R

Well I swapped the Uno to the transmitter position and had no trouble converting the software.

The Mega was moved to the receive position and the following code added:

#include <Servo.h>
#include <MegaServo.h>


#define NBR_SERVOS 3  // the number of servos, up to 48 for Mega, 12 for other boards
#define FIRST_SERVO_PIN 2 

MegaServo Servos[NBR_SERVOS]


// define Servos & Servo trim

Servo hdgservo;  // create servo object to control a servo
Servo depthservo ; 
Servo pitchservo ;

But, when I compile, I get the following errors:

Arduino: 1.6.13 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\Dianne\Documents\Arduino\libraries\MegaServo\MegaServo.cpp:56:23: fatal error: WProgram.h: No such file or directory

 #include <WProgram.h> 

                       ^

compilation terminated.

exit status 1

I have read through a lot of posts about “If you get errors running Arduino sketches in Arduino 1.0. You MUST change WProgram.h to Arduino.h in the .h and .cpp libraries files”

They didn’t say WHAT .h and .cpp files so I found MegaServo.cpp and Megaservo.h in the libraries folder. Only Megaservo.cpp contained “#include WProgram.h” so I changed that to Arduino.h

After making that change, I get LOTS of errors but they seem to relate to defining the servo :

Arduino: 1.6.13 (Windows 7), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

SS_1_0_2:22: error: expected initializer before 'Servo'

 Servo hdgservo;  // create servo object to control a servo

 ^

SS_1_0_2:23: error: 'Servo' does not name a type

 Servo depthservo ; 

 ^

SS_1_0_2:24: error: 'Servo' does not name a type

 Servo pitchservo ;

 ^

C:\Users\Dianne\Documents\Arduino\SS_1_0_2\SS_1_0_2.ino: In function 'void setup()':

SS_1_0_2:67: error: 'hdgservo' was not declared in this scope

   hdgservo.attach(1);

   ^

SS_1_0_2:68: error: 'depthservo' was not declared in this scope

   depthservo.attach(3);

   ^

SS_1_0_2:69: error: 'pitchservo' was not declared in this scope

   pitchservo.attach(2) ;

   ^

C:\Users\Dianne\Documents\Arduino\SS_1_0_2\SS_1_0_2.ino: In function 'void updateServos()':

SS_1_0_2:258: error: 'hdgservo' was not declared in this scope

   hdgservo.attach(9); // connect servos

   ^

SS_1_0_2:259: error: 'depthservo' was not declared in this scope

   depthservo.attach(11);

   ^

SS_1_0_2:260: error: 'pitchservo' was not declared in this scope

   pitchservo.attach(10) ;

   ^

exit status 1
expected initializer before 'Servo'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

FRUSTRATED!!! I have no GD idea what is wrong now or even if I am going in the right direction! HELP!!!

I don't understand why you are not using the standard Servo library with the Mega? It works perfectly.

...R

Robin2: I don't understand why you are not using the standard Servo library with the Mega? It works perfectly.

I didn't know you could!

DianneB: I didn't know you could!

I presume that means "problem solved" ?

I can't imagine why you thought the standard Servo library would not work with a Mega. AFAIK it works with all Arduino boards.

...R

Robin2: I presume that means "problem solved" ?

I can't imagine why you thought the standard Servo library would not work with a Mega. AFAIK it works with all Arduino boards.

...R

YOU know that - I didn't. When I searched "Mega" and "servos" all I got was the MegaServo library and I couldn't solve all the error messages so, after your post, I went back to the standard servo library.

Swapping boards took HOURS! Mapping out all the connections to the Uno and figuring out where to put everything on the Mega. My Mega is from SainSmart Mega - VERY hard to match the pin number on the board to the connector holes on the top of the VERY tall headers so I screwed up the wiring a couple of times. Then I had to modify the software for the new pin locations (all of which seems to create Compiler errors LOL!).

It took awhile to figure out how to receives my serial, figuring out how to implement Serial2, and get that working again with no compiler errors.

I then spend quite a length of time getting the servos set up and working - it didn't help that there was a wiring error, a misalignment on the header, and that one of my servos crapped out!

Nearly 12 hours after deciding to swap boards, the Serial2 is working and I have all 3 servos working AGAIN.

NO MORE SERVO JUMPS!

There are still a few other I/O functions to test but I will leave that for tomorrow - I AM TIRED!

(I am nothing if not stubborn and persistent! LOL!)

DianneB: (I am nothing if not stubborn and persistent! LOL!)

Essential qualities IMHO.

Congrats on getting it all sorted.

YOU know that - I didn't. When I searched "Mega" and "servos"

Sometimes it pays to be stupid and NOT to ask :)

More seriously, I think all you needed was here all the time :) RTFM works !

...R