Servo moves when not prompted to

Hi I'm building a reverse goecaching box. I have an lcd and gps that work fine together but when i include a servo it moves back and forth throughout the sketch even when I don't have any commands for it.

/*
 The circuit:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
 
 http://www.arduino.cc/en/Tutorial/LiquidCrystal
 */

// include the library code:
#include <LiquidCrystal.h>
#include <math.h>

#include <Servo.h> 

#include <SoftwareSerial.h>
#include <TinyGPS.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
#define RXPIN 2
#define TXPIN 3
#define GPSBAUD 4800
TinyGPS gps;
SoftwareSerial uart_gps(RXPIN, TXPIN);
void getgps(TinyGPS &gps);

const float pi = 3.14;
float targetlat = rad(39.52);
float targetlong = rad(-104.92139);

Servo myservo;

void setup() {
  myservo.attach(8);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2);
  Serial.begin(115200);
  uart_gps.begin(GPSBAUD);
  // Print a message to the LCD.
  lcd.print("waiting for lock...");

}

void loop() {

  if(uart_gps.available())     // While there is data on the RX pin...
  {

    int c = uart_gps.read();    // load the data into a variable...
    if(gps.encode(c))      // if there is a new valid sentence...
    {
      lcd.clear();
      getgps(gps);         // then grab the data.
      delay(500);
    }

  }
}

void getgps(TinyGPS &gps)
{
  // To get all of the data into varialbes that you can use in your code, 
  // all you need to do is define variables and query the object for the 
  // data. To see the complete list of functions see keywords.txt file in 
  // the TinyGPS and NewSoftSerial libs.

  // Define the variables that will be used
  float latitude, longitude;
  // Then call this function
  gps.f_get_position(&latitude, &longitude);

  float lat = rad(latitude);
  float lon = rad(longitude);
  float x = (lon-targetlong)*cos(targetlat);
  float y = lat-targetlat;
  float d = 3959*sqrt(sq(x)+sq(y));
  if(d <= .05)
  {
    lcd.clear();
    lcd.print("Congrats!");
    lcd.setCursor(0,1);
    lcd.print("target reached");
    while(1)
    {
    }
  }
  // You can now print variables latitude and longitude
  lcd.print("Lat: "); 
  lcd.print(latitude,5); 
  lcd.setCursor(0, 1);
  lcd.print("long: "); 
  lcd.print(longitude,5);

  // Here you can print statistics on the sentences.
  unsigned long chars;
  unsigned short sentences, failed_checksum;
  gps.stats(&chars, &sentences, &failed_checksum);
  //Serial.print("Failed Checksums: ");Serial.print(failed_checksum);
  //Serial.println(); Serial.println();
  delay(2500);

  lcd.clear();
  lcd.print(d);
  lcd.setCursor(0, 1);
  lcd.print("Miles to target");
  delay(2500);



}

float rad(float n)
{
  float r = n*(pi/180);
  return r;
}

Can any one tell me why this is happening and provide a fix?

How is the servo powered?

HazardsMind:
How is the servo powered?

Standard reply when SSS is exhibited..... SpasticServoSyndrome..... 8)

powered from the 5v pin on the arduino

As you and countless other people have discovered, that won't work. Power the servo separately and connect the two power supply grounds together.

jremington:
Power the servo separately and connect the two power supply grounds together.

As shown in zoomkat's renowned pic....

That said, in this case, I'm wondering if power is the problem, since it seems to be merely attached not commanded. But, it's got to be re-wired, and that might fix the problem.

servo-wire.jpg

I took a 6 volt battery supply and put the positive end to the servo and the ground to a ground on the arduino but that didn't solve the problem.

hadjisra:
I took a 6 volt battery supply and put the positive end to the servo and the ground to a ground on the arduino but that didn't solve the problem.

Is the Arduino ground connected to the servo ground, and did you remove the 5v from Arduino to servo that was powering it before?

yes I removed the 5v power supply from the servo replaced it with a separate battery pack and linked the servo, arduino, and new power supply grounds together.

The servo makes periodic movements through out the sketch and are far from random so it leads me to believe there is something wrong with my code.

Perhaps one of the other libraries does something to pin 8.... try attach to another pin?

I tried all the other pins that I had available for use and rearranged some of the lcds pins but still nothing... Is it possible to us a analog out to run a servo?

you attached the servo but didn't give it a starting point. Not sure if this makes a difference, but try:

Servo myservo;
myservo.write (90);

hadjisra:
I tried all the other pins that I had available for use and rearranged some of the lcds pins but still nothing... Is it possible to us a analog out to run a servo?

  1. No
  2. There is no analog out on an Arduino. An analogRead() generates a PWM signal.

cwhummel:
you attached the servo but didn't give it a starting point. Not sure if this makes a difference, but try:

Servo myservo;

myservo.write (90);

With SSS, anything's worth a try.

I tried that and it didn't work. What if I built a separate controller circuit that moves the servo from 0 to 180 with the input of a 2 second high pulse from the arduino? or something like that because I do not need specific control of my servo

I tried that but still nothing. All I need is for the servo to move from 0 to 180 and back is there a simple controller circuit that might do this?

Have you tried the servo by itself using just say the sweep code from the ide, and nothing else connectged.... no LCD etc. That would at least tell us the servo and its connections & power are good.

Then, assuming they are good, there would presumably be some conflict with the other parts of your actual sketch. Troubleshoot by commenting out parts of that until the problem goes away.

The sweep code with just the servo works fine and if I write a simple code that just attaches the servo and does nothing with it is fine. Ill start commenting out sections.

hadjisra:
The sweep code with just the servo works fine and if I write a simple code that just attaches the servo and does nothing with it is fine. Ill start commenting out sections.

I really can't think of anything else to try, other than chop it down and try to identify the code (or hardware.... like the LCD) that's causing the problem.

But, at least, you do know the servo itself, and its power and connection is good.

Hang in there.....