Many "expected `)' before ';' token" errors

Hello guys.

As the title suggests, I have many "expected `)' before ';' token" errors. The issue is, I have this stuff in. So now I am confused, why am I getting this if the things are there.

Here is the code:

#include <Servo.h>

Servo servo;

#define trigPin 3; // Trig Pin (Digital)
#define echoPin 2; // Echo Pin (Digital)

int alertRange = 30; // alert rainge in cm
// #define maximumRange 10000;
// #define minimumRange 0;
long duration, cmFront, cmLeft, cmRight; // Duration used to calculate distance

void setup()
{
  servo.attach(9);
  // Test the servo before begining
  servoTest();
  Serial.begin(9600);
}

void loop()
{
  long duration, cmFront, cmLeft, cmRight; // Duration used to calculate distance
  int frontFace = 85; // The Servo's front facing angle
  int rightFace = 40; // The Servo's right facing angle
  int leftFace = 130; // The Servo's left facing angle

  cmFront = cycle(frontFace);
  cmLeft = cycle(leftFace);
  cmRight = cycle(rightFace);

  Serial.print("Front: ");
  Serial.print(cmFront);
  Serial.print("Left: ");
  Serial.print(cmLeft);
  Serial.print("Right: ");
  Serial.println(cmRight);
}

void servoTest()
{
  servo.write(frontFace);
  delay(1000);
  servo.write(rightFace);
  delay(1000);
  servo.write(frontFace);
  delay(1000);
  servo.write(leftFace);
  delay(1000);
  servo.write(frontFace);
  delay(1000);
  servo.write(0);
  delay(1000);
  servo.write(180);
  delay(1000);
}

long cycle(int face)
{
  servo.write(face);
  delay(100);
  // Trig/Echo cycle
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  return microsecondsToCentimeters(duration);
}


long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

And here are the errors:

AutoBot:58: error: expected `)' before ';' token
AutoBot:58: error: expected primary-expression before ',' token
AutoBot:58: error: expected `;' before ')' token
AutoBot:61: error: expected `)' before ';' token
AutoBot:61: error: expected primary-expression before ',' token
AutoBot:61: error: expected `;' before ')' token
AutoBot:64: error: expected `)' before ';' token
AutoBot:64: error: expected primary-expression before ',' token
AutoBot:64: error: expected `;' before ')' token
AutoBot:65: error: expected `)' before ';' token
AutoBot:65: error: expected primary-expression before ',' token
AutoBot:65: error: expected `;' before ')' token
AutoBot.ino: In function 'void cycleLeft()':
AutoBot:72: error: expected `)' before ';' token
AutoBot:72: error: expected primary-expression before ',' token
AutoBot:72: error: expected `;' before ')' token
AutoBot:75: error: expected `)' before ';' token
AutoBot:75: error: expected primary-expression before ',' token
AutoBot:75: error: expected `;' before ')' token
AutoBot:78: error: expected `)' before ';' token
AutoBot:78: error: expected primary-expression before ',' token
AutoBot:78: error: expected `;' before ')' token
AutoBot:79: error: expected `)' before ';' token
AutoBot:79: error: expected primary-expression before ',' token
AutoBot:79: error: expected `;' before ')' token
AutoBot.ino: In function 'void cycleRight()':
AutoBot:86: error: expected `)' before ';' token
AutoBot:86: error: expected primary-expression before ',' token
AutoBot:86: error: expected `;' before ')' token
AutoBot:89: error: expected `)' before ';' token
AutoBot:89: error: expected primary-expression before ',' token
AutoBot:89: error: expected `;' before ')' token
AutoBot:92: error: expected `)' before ';' token
AutoBot:92: error: expected primary-expression before ',' token
AutoBot:92: error: expected `;' before ')' token
AutoBot:93: error: expected `)' before ';' token
AutoBot:93: error: expected primary-expression before ',' token
AutoBot:93: error: expected `;' before ')' token

Does anyone understand how this could happen?

Thanks for replying.

your #defines should not have semicolons.

Thanks that fixed that issue, but I have another, the system keeps coming up with outrageous numbers as each code sample.

the system keeps coming up with outrageous numbers as each code sample.

That's terrible.
You should give it a stern talking-to.

AWOL:
That's terrible.
You should give it a stern talking-to.

?

Oh, sorry meant the arduino keeps coming up with outrageous numbers as each of the cm variables.

I can't see any significant difference between the three "cycle" functions.
Can you explain, please?

Its missing the servo rotations, which when added gibe the difference

afroraydude:
Its missing the servo rotations

No, I mean "why have you got three virtually identical functions?"

They aren't identical.

frontCycle has the servo at a 85 degree angle
leftCycle has the servo at a 130 degree angle
rightCycle has the servo at a 40 degree angle

What are you measuring distance with?

The functions are virtually identical - the only difference is the global variable they write to.
If a single non-void function were written in their stead, you'd save yourself debug effort.

ChilliTronix:
What are you measuring distance with?

The ultrasonic distance sensor.

This part being the code to change it to cm:

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

I use the following:

 //Calculate the distance (in cm) based on the speed of sound.
 distance = duration/58.2;

See here:

ChilliTronix:
I use the following:

 //Calculate the distance (in cm) based on the speed of sound.

distance = duration/58.2;




See[ here](http://www.chillitronix.co.uk/index.php?page=hc-sr04-rangefinder):

and 58 / 2 = ?

ChilliTronix:
I use the following:

 //Calculate the distance (in cm) based on the speed of sound.

distance = duration/58.2;




See[ here](http://www.chillitronix.co.uk/index.php?page=hc-sr04-rangefinder):

how? if sound travels at 29 34 cm per millisecond then it should be different

Well, I can't remember where I got it from, but it does check out with a tape measure.

However, obviously the distance sound travels is a function of time taken times the speed of sound. So if t was 1 second and speed was 340.29 then multiply speed times time to get ...

So I can see your scepticism. Try it though, it works. We have this which references sources. (Or in other words now I can remember.)

The thing is, my code worked at first. It was accurate. But the distance sensor has to be broken, when testing, I was about a foot away from the distance sensor and it said I was 1000 cm away. Then, as I was in the same place, it said that I was 200 cm away. Weird. If you tried my code, well at least the proximity sensor part, you would see that it does work code-wise. And I know this because it came from the code on the Playground here.

When converting time to distance, don't forget that the sound travels twice as far: from the sensor to the object, and then from the object back to the sensor. So the conversion factor will be twice as big as expected (if you're dividing by the factor, half the size if you're multiplying.)

AWOL:
The functions are virtually identical - the only difference is the global variable they write to.
If a single non-void function were written in their stead, you'd save yourself debug effort.

This is a good point. If you decide you need to make a change to one function, you will have to make sure you make the same change to ALL functions. If you don't, things will get very confusing.

Besides the global variable being assigned (which could be a return value) the other difference is the the parameter to servo.write() which could be a parameter to the function. You can get rid of the three functions, and replace it with:

long cycle(int face)
{
 servo.write(face);
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 return microsecondsToCentimeters(duration);
}

Then the sequence where the three functions are called at the top of loop() becomes:

  cmFront = cycle(frontFace);
  cmLeft = cycle(leftFace);
  cmRight = cycle(rightFace);

This has several advantages:

  • It's more understandable and maintainable since there is less code duplication
  • It takes less code memory space, and less time to compile, and less time to upload. (Not a huge savings in this small sketch, but can make a difference in a big sketch that has a lot of similar functions.)
  • it's now more clear how the cmFront, cmLeft, and cmRight variables are set
  • cmFront, cmLeft, and cmRight can be made local to loop(), thereby limiting their exposure to accidental changes, and also showing that they are not used outside of the loop
  • duration can be made local to cycle(), again limiting the exposure to accidental changes, and also showing that it is not used outside of the function
  • overall it's just a cleaner design

ShapeShifter:
When converting time to distance, don't forget that the sound travels twice as far: from the sensor to the object, and then from the object back to the sensor. So the conversion factor will be twice as big as expected (if you're dividing by the factor, half the size if you're multiplying.)
This is a good point. If you decide you need to make a change to one function, you will have to make sure you make the same change to ALL functions. If you don't, things will get very confusing.

Besides the global variable being assigned (which could be a return value) the other difference is the the parameter to servo.write() which could be a parameter to the function. You can get rid of the three functions, and replace it with:

long cycle(int face)

{
servo.write(face);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
return microsecondsToCentimeters(duration);
}




Then the sequence where the three functions are called at the top of loop() becomes:


cmFront = cycle(frontFace);
  cmLeft = cycle(leftFace);
  cmRight = cycle(rightFace);



This has several advantages:

- It's more understandable and maintainable since there is less code duplication
- It takes less code memory space, and less time to compile, and less time to upload. (Not a huge savings in this small sketch, but can make a difference in a big sketch that has a lot of similar functions.)
- it's now more clear how the cmFront, cmLeft, and cmRight variables are set
- cmFront, cmLeft, and cmRight can be made local to loop(), thereby limiting their exposure to accidental changes, and also showing that they are not used outside of the loop
- duration can be made local to cycle(), again limiting the exposure to accidental changes, and also showing that it is not used outside of the function
- overall it's just a cleaner design

Still trying to understand the code, so basically

long cycle(int face) // Int face is replaced by the face
{
 servo.write(face); // Servo writes to the angle of said face
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2); 

 digitalWrite(trigPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH); 
 return microsecondsToCentimeters(duration); // This is what loop() will receive
}
  // Inside loop()
  cmFront = cycle(frontFace); // This recieves what front face is returning
  cmLeft = cycle(leftFace); // This recieves what left face is returning
  cmRight = cycle(rightFace); // This recieves what right face is returning

The thing is, my mind keeps telling me that, according to my understanding of this, if the int face has already been defined(as angles in the beginning of the code), then wouldn't it just cause an error?