How to intergrate code for a servo with NeoGPS code

Hi,

A coupe of years ago, I had post a question regarding my 0-40 MPH old school speedometer for a bike that uses a 1575R-A gps module to calculate speed and a servo with an orange needle to show the speed. The speedometer design has been tweaked a little bit, the scale now goes to 50, but it's still the same concept. Someone replied with saying that first I need to get the gps to calculate speed, then implement the code for the servo so it will point to the correct number once hit that speed. My question is, how? I looked at YouTube videos and other forums, but it's not making sense to me. Also, when I do find information on Arduino speedometers, they are using a LCD to display speed, but not my project as I mentioned earlier.

Here's what I have so far

#include <NMEAGPS.h>
#include <NeoSWSerial.h>
NeoSWSerial gpsSerial(0,1);
NMEAGPS gps;

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  gpsSerial.begin(9600);
}

void loop() {
  for (pos = 0; pos <= 180; 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(17);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(17);                       // waits 15ms for the servo to reach the position
  }
}


void displayGPS( gps_fix & fix )
{
  int mph = 0;

  // Print speed
 
  if (fix.valid.speed && (fix.spd.whole > 5)) {
    mph = (fix.spd.whole * 185) / 100;
  }

  if (mph > 30)
    digitalWrite( WARNING_LED, HIGH );
  else
    digitalWrite( WARNING_LED, LOW );

  // Print time
  if (fix.valid.time) {
    printTime( fix.dateTime );
  }
} // displayGPS

void loop()
{
  while (gps.available( gpsSerial )) {  // if there is data coming from the GPS shield
    gps_fix fix = gps.read();           // get the complete fix structure
    displayGPS( fix );                  // Show pieces of the fix on the LCD
  }
}

Anything is much appreciated

First of all, you can't have two loop() functions in your code. It will not compile. The second loop() version is probably more like what you need. You will have to figure out what servo position corresponds to what speed such that, once you calculate the speed (mph), you can determine which position the servo needs to be in and move it there. You will also have to keep track of the current servo position so you know which way to move it if the speed goes up or down.

So should I use "If" statements, saying that if reach 5 mph, servo turns 18 degrees? Also, how can I track current servo position?

gotta_go_fast:
So should I use "If" statements, saying that if reach 5 mph, servo turns 18 degrees?

It really depends on how linear the response is vs. your servo. Is 0 degrees = 0 mph and 180 degrees = 50 mph? And everything in between in linear? (90 degrees = 25 mph). If it is linear, you can just write an equation or use the map() function and/or constrain() functions

Also, how can I track current servo position?

The normal way to do this would be to create a global variable (maybe 'currentPostion') and use that. When you get a new position, you move from the 'currentPostion' to the 'newPosition' and then set currentPosition equal to the newPosition.

gotta_go_fast:
how can I track current servo position?

If by that you mean to know where it actually is, as opposed to where it's supposed to be, you need a 4th wire like this.

If you just want to know where you sent it, that is the last position commanded, use myservo.read().