Servo Trouble

Hi all,

I'm new to using servos and have written a script, however I keep getting "'Servo' does not name a type" and I have no idea what this means as a previous script(that I hadn't written) I tried worked fine and I had used this as a template. Any help you guys could give me would be awesome as I'm still very new to C programming and I'm really enjoying it!

Thanks in advance,

Jack

Are you using the servo library?

#include<Servo.h>

Rule #1 is to post the code that is not worling for you if you want more than just guesses as to the problem.

Hi all,

HazardsMind:
Are you using the servo library?

#include<Servo.h>

Yeah, that what the script I based if on said to do.

zoomkat:
Rule #1 is to post the code that is not worling for you if you want more than just guesses as to the problem.

Sorry, slipped my mind! Here's the code:

#inlcude <Servo.h>
Servo servoTest1;

int servoPin = 9;
int distPin = 0;
int distPin2 = 1;

void setup()
{
servoTest1.attach(servoPin);

}

void loop()
{
int dist = analogRead(distPin);
int dist2 = analogRead(distPin2);

//int pos = map(dist, 0, 1023, 0, 180);

// servoTest1.write(pos);
int pos

if (dist - dist2 = 0) //are both distances equal
{dist = dist2} //if so, they are both eqaul
else // otherwise
{int pos2 = dist - dist2; // calculate their distance
int pos = pos2 * -1;} // invert it, and make it the servo's new possition

int servoPossition = map(pos, 0, 1023, 0, 180);

}

Basically I'm trying to create a script that measures 2 distances, calculates if they're equal, then adjusts the servo position until the sensors on it are equal distances from the object thereby following the object as it moves.

Thanks again!

Jack

First thing I noticed on a quick review:

if (dist - dist2 = 0) //are both distances equal

should be:

if ((dist - dist2) == 0) //are both distances equal

The single equal sign is the assignment operator
The double equal sign (e.g. if (x == 10) ), which is the comparison operator.

Lefty

I modified your code so you can use the serial monitor to see if you are getting the expected values.

#include <Servo.h>;
Servo servoTest1;

int servoPin = 9;
int distPin = 0;
int distPin2 = 1;

void setup()
{
  servoTest1.attach(servoPin);
  Serial.begin(9600);
  Serial.println("start test");
}

void loop()
{
  int dist = analogRead(distPin);
  int dist2 = analogRead(distPin2);
  Serial.print("dist ");
  Serial.println(dist); 
  Serial.print("dist2 ");
  Serial.println(dist2); 
  Serial.println(); 
  //int pos = map(dist, 0, 1023, 0, 180);

  //  servoTest1.write(pos);
  //int pos;

  if (dist != dist2 ) //are both distances equal
    //{dist = dist2} //if so, they are both eqaul
    //else // otherwise
  {
    int pos2 = (dist - dist2); // calculate their distance
    Serial.print("pos2 ");
    Serial.println(pos2); 
  
    int pos = pos2 * -1;
    // invert it, and make it the servo's new possition
    Serial.print("pos ");
    Serial.println(pos); 
    Serial.println();    
    
    int servoPossition = map(pos, 0, 1023, 0, 180);
    Serial.print("servoPossition ");
    Serial.println(servoPossition); 
    Serial.println("done! ========================="); 
    Serial.println();  

  }  
  delay(5000); 
}

You spelled include wrong, "#inlcude <Servo.h>"

Thankfully Zoomkat's code has it corrected. Try it now

Hi All,

Thanks for all your posts, I really appreciate it. Looks like my dyslexia has got the better of me again lol :sweat_smile: , all is working now!
It has been driving me mad for the past couple of weeks now so I'm glad that I know where I went wrong :slight_smile:

Just one last question, reading through the modified code I'm not understanding what '!=' does. Could someone explain as google isn't bringing anything up.

Thanks again!

Jack

"!=" means "not equal"

Pseudo code Example,

X=5;
Y=2;

If (Y != X) {
Display, Y does not equal X
Y++; //increment Y by 1 and will keep going until Y = X
}

Else {Display, Y equals X}

I'm not understanding what '!=' does.

Local is usually better

Hi All,

HazardsMind:
"!=" means "not equal"

Pseudo code Example,

X=5;
Y=2;

If (Y != X) {
Display, Y does not equal X
Y++; //increment Y by 1 and will keep going until Y = X
}

Else {Display, Y equals X}

Right, I understand now as I was wondering what happened to the True condition line where a = b. That make sense now.

AWOL:

I'm not understanding what '!=' does.

Local is usually better

What is local? I understand the different operators on thi page but can't find anything about 'local'

Thanks again,

Jack

The below page has some good info.

What is local?

Local to this forum.
Why go off to the WWW, when the information is right here?

Hi All,

zoomkat:
The below page has some good info.

Arduino - Home

That it awesome! I've been looking for a site like this, thankyou!!!!

AWOL:

What is local?

Local to this forum.
Why go off to the WWW, when the information is right here?

Agreed, I hate posting rookie questions, but I always think for the hours it would take me looking through site (or trying to find them) I could ask someone on here and get a concise answer.

Once again, can't thank you guys enough for the help!

Happy New year (ish)!!

Jack

Checkout the Arduino tutorials here: http://arduino.cc/en/Tutorial/HomePage
There are plenty of examples for basic functions and projects that can point you in the right direction. Enjoy your project.