Conflict between servo and Sparkfun open segment serial display

I am trying to do something extremely simple but there must be an error I am not seeing. Please help me out I would really appreciate it.

I have an arduino uno and for testing I just used the simple sweep servo example. I am trying to just display the servo position on the open segment display as the servo sweeps around. Here is a link to the display I have. https://www.sparkfun.com/products/11647

What I have found is that as soon as I put the code line in to print to the display it causes the servo to jump around and appear to be having noise interference. Comment that print line out and everything works smoothly.

Here is the sketch (it’s also attached):

#include <Servo.h>; 
#include <SoftwareSerial.h>

const int softwareTx = 12; // These are the Arduino pins required to create a software serial
const int softwareRx = 13; //  instance. We'll actually only use the TX pin.
const int servoPin = 8; // the digital pin used for the servo
char tempString[10];  // Will be used with sprintf to create strings
int pos = 0;

SoftwareSerial s7s(softwareRx, softwareTx); //initializes serial communication on the seven segment display with the name "s7s"
Servo myservo;

void setup() {
 
  Serial.begin(9600); //sets baud rate
      // Must begin s7s software serial at the correct baud rate.
 //  The default of the s7s is 9600.
 s7s.begin(9600);

 // Clear the display, and then turn on all segments and decimals
 clearDisplay();  // Clears display, resets cursor
 s7s.print("----");  // Displays ---- on all digits

 setBrightness(250);  //
// Set the displays brightness. Should receive byte with the value
//  to set the brightness to
//  dimmest------------->brightest
//     0--------127--------255
 delay(1500);

 // Clear the display before jumping into loop
 clearDisplay();

myservo.attach(servoPin);  // attaches the servo on servo pin value to the servo object
}

void loop() {
   for (pos = 10; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees
   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
   sprintf(tempString, "%4d", pos);
     s7s.print(tempString);  //this line is causing the servo to jump around very badly
 }
 
 for (pos = 160; pos >= 10; pos -= 1) { // goes from 180 degrees to 0 degrees
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15); 

   sprintf(tempString, "%4d", pos);
     s7s.print(tempString);  //this line is causing the servo to jump around very badly
     }

}


// Send the clear display command (0x76)
//  This will clear the display and reset the cursor
void clearDisplay()
{
 s7s.write(0x76);  // Clear display command
}

//-------------------------------------------------------------------------------------------------------------------------------------

// Set the displays brightness. Should receive byte with the value
//  to set the brightness to
//  dimmest------------->brightest
//     0--------127--------255
void setBrightness(byte value)
{
 s7s.write(0x7A);  // Set brightness command byte
 s7s.write(value);  // brightness data byte
}

test_servo_sketch_with_display.ino (2.51 KB)

Try the ServoTimer2 library. It may eliminate the conflict.

...R

I just downloaded the servotimer2 library but I am also interested in using the VarSpeedServo library later on which allows you to change the speed of the servo movement.

Is there any way to fix the problem without switching to the servotimer2 library?

here is the link to the VarSpeedServo library

https://github.com/netlabtoolkit/VarSpeedServo

Is there any way to fix the problem without switching to the servotimer2 library?

Why don't you see if that fixes the issue, first?

good point. I was just trying to say that since I already had the intention of using the variable speed library that I would still have to implement a solution that didn't involve switching to a new library.

I'll give it a go and see if it fixes it.

I included the library ServoTimer2 but the library doesn't seem to be compatible with the current arduino software. It errors out immediately during compiling and it doesn't recognize it as a valid library.

Here is the error output by the compiler

Arduino: 1.6.8 (Windows 7), Board: "Arduino/Genuino Uno"

ServoTimer2.h:76:17: error: conflicting declaration 'typedef uint8_t boolean'

typedef uint8_t boolean;

^

In file included from sketch\test_servo_sketch_with_display_servotimer2.ino.cpp:1:0:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:117:14: error: 'boolean' has a previous declaration as 'typedef bool boolean'

typedef bool boolean;

^

exit status 1 Error compiling for board Arduino/Genuino Uno.

That error has come up in the Forum before. Unfortunately I can’t remember the fix - which is simple. I’m still using IDE 1.5.6

You can have several different versions of the IDE on your PC.

…R

I can’t remember the fix - which is simple.

Sure is. Delete the typedef statement from ServoTimer2.h.

Thank you for the information regarding the error with servotimer2 library.
I deleted the typedef shown in the error and the sketch compiled but unfortunately it did not solve the problem. The servo still jumps around badly while the print to display command is included.

This is not the right way to do what you want to achieve

   for (pos = 10; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees
   // in steps of 1 degree
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);                       // waits 15ms for the servo to reach the position
   sprintf(tempString, "%4d", pos);
     s7s.print(tempString);  //this line is causing the servo to jump around very badly
 }

In effect the printing stuff is adding to the delay.

You need to manage timing using millis() as illustrated in Several Things at a Time

Why do you need to print every iteration of the servo?
Printing every 15 millisecs is too fast - no human reader can keep up with that. Just print 2 or 3 times per second. Alternatively slow the servo to a move every (say) 300 msecs.

…R

Thanks for the information. I read through the several things at a time sketch specifically looking at the servo portion and while it's new to me I think I get it.

when you are using millis to keep note of the time what do you do to interject a display command somewhere "out of phase" with the servo command?

It seems like all of the separate functions you have don't really reference one another to stay out of eachothers way because they don't need to but in my case the servo really doesn't like it if the display and the servo are getting a command at the same time.

I have been using the variable speed servo library because my ultimate goal is to use a momentary switch that when pressed makes the servo move and the rate of movement of the servo varies depending on it's position around the arc. For example if the servo moves from 0° to 180° the rate of movement of the servo is faster near 0° and slows down as it gets toward 180°. It certainly seems like the time keeping can do this same job instead of using the variable speed servo library so I just need to figure out where to interject the display command like you said I don't need to see it update every 15 millisec 2 or 3 times per second is fine I just don't know how to keep it out of phase with the servo movement.

WWould you be able to help me figure out where to place a display command. With just taking your code for the several things at a time sketch would you place the display command inside the servo function or would you make it a separate function that is called at the beginning

wjcarman: It seems like all of the separate functions you have don't really reference one another to stay out of eachothers way because they don't need to but in my case the servo really doesn't like it if the display and the servo are getting a command at the same time.

In an Arduino no two things can "get a command at the same time". The structure of the code in Several Things at a Time is designed to manage the different activities so that they all work smoothly.

This may be explained more clearly in Planning and Implementing a Program

Put your servo code in one function and your display code in another function and call both functions from loop().

If you try this and it does not work post the latest version of your code.

...R

Thanks I will do that.

One other immediate question I had regarding the several things at a time sketch is why don't you have to check millis() inside of each function at the beginning of each one? It only gets checked once before all of the functions and I just feel confused why each sub function doesn't have to check it within it's routine.

and I just feel confused why each sub function doesn’t have to check it within it’s routine.

Because most of the time, the function is going to determine that is has nothing to do, and return. The clock will not have ticked over another millisecond while a dozen functions are called, so than can all look at the one value, rather than calling millis() a dozen times.

wjcarman: One other immediate question I had regarding the several things at a time sketch is why don't you have to check millis() inside of each function at the beginning of each one? It only gets checked once before all of the functions and I just feel confused why each sub function doesn't have to check it within it's routine.

I am assuming that loop() will repeat fast enough that millis() won't change in one iteration. There are also circumstances where it is useful to have all the decisions based on the same time value.

In fact I would prefer all my decisions to use the same time value unless there is a specific reason why they should not. It makes debugging much easier.

...R