expected intializer before 'const'??

Once again hello :sweat_smile: . I am stuck once more and i need help.
Here is the code. Second line of the code is the problem.

#include <Servo.h>


//Setup the variables for the HC-SR04

// initializer supposed to be before

const int trigPin = 6;

// thats the problem
const int echoPin = 7;

// create servo object to control a servo

// a maximum of eight servo objects can be created

Servo myservo;



// variable to store the servo position

int pos = 0;


{

  // initialize serial communication:

  Serial.begin(9600);



  // attaches the servo on pin 9 to the servo object

  myservo.attach(9);



}

void loop()

{

  // establish variables for duration of the ping,

  // and the distance result in inches and centimeters:

  long duration, inches, cm;



  // The sensor is triggered by a HIGH pulse of 10 or
  // more microseconds.

  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  pinMode(trigPin, OUTPUT);

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);



  // Read the signal from the sensor: a HIGH pulse whose

  // duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off of an object.

  pinMode(echoPin, INPUT);

  duration = pulseIn(echoPin, HIGH);



  // convert the time into a distance

  inches = microsecondsToInches(duration);

  cm = microsecondsToCentimeters(duration);



  //Tell the Arduino to print the measurement in the serial console

  Serial.print(inches);

  Serial.print("in, ");

  Serial.print(cm);

  Serial.print("cm");

  Serial.println();



  // This if-else statement tells the Arduino at what distance

  // it should trigger the servo, what the servo should do,

  // and what it should do if the distance is too far away.

  if (inches <= 24) {
    sweep(3);



  }

  else if (inches >= 24) {
    myservo.write(pos);

  }



  // Tell the Arduino to wait 0.10 seconds before pinging the

  // Ultrasonic Sensor again.

  delay(100);

}

// Converts the microseconds reading to Inches

long microsecondsToInches(long microseconds)

{

  // According to Parallax's datasheet for the PING))), there are

  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

  // second). This gives the distance travelled by the ping, outbound

  // and return, so we divide by 2 to get the distance of the obstacle.

  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

  return microseconds / 74 / 2;

}

//Converts the Microseconds Reading to Centimeters

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;

}

//This sub-routein is what dictates the movement of the servo

void sweep(int NUM_OF_CYCLES)



{

  // Tells the Arduino to start this loop at 0 and incriment it by 1

  // each time the loop completes. This is how the Arduino knows to

  // stop the loop when a specific number of the
  // Sweep routein has been ran.

  for (int j = 0; j < NUM_OF_CYCLES; j++)
    // goes from 0 degrees to 180 degrees
    for (pos = 0; pos < 180; pos += 1)
    { // in steps of 1 degree
      // tell servo to go to position in variable 'pos'
      myservo.write(pos);
      // waits 15ms for the servo to reach the position delay(10); }
      // goes from 180 degrees to 0 degrees
      for (pos = 180; pos >= 1; pos -= 1) {
        // tell servo to go to position in variable 'pos'
        myservo.write(pos);
        // waits 15ms for the servo to reach the position
        delay(10);
      }
    }

And now here is the error message

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

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV1.ino:1:19: warning: extra tokens at end of #include directive

 #include <Servo.h>;

                   ^

ServoV2:7: error: expected initializer before 'const'

 const int trigPin = 6;

 ^

SmartBin_CodeV2:2: error: expected unqualified-id before numeric constant

 #define echoPin 6

                 ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino:9:11: note: in expansion of macro 'echoPin'

 const int echoPin = 7;

           ^

ServoV2:24: error: expected unqualified-id before '{' token

 {

 ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void loop()':

ServoV2:40: error: redefinition of 'void loop()'

 void loop()

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino:22:6: note: 'void loop()' previously defined here

 void loop() {

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void sweep(int)':

ServoV2:198: error: expected '}' at end of input

     }

     ^

exit status 1
expected initializer before 'const'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

This line from your code

#include <Servo.h>

and this line from the error message

 #include <Servo.h>;

aren’t the same.

Pete

Don’t use; at the end of #include <servo.h>!

It still says the same error message even if i put the ; and verify.

Error message:

Arduino: 1.8.5 (Windows 7), Board: ‚ÄúArduino/Genuino Uno‚ÄĚ

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV1.ino:1:19: warning: extra tokens at end of #include directive

#include <Servo.h>;

^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino:1:19: warning: extra tokens at end of #include directive

#include <Servo.h>;

^

ServoV2:8: error: expected initializer before ‚Äėconst‚Äô

const int trigPin = 6;

^

SmartBin_CodeV2:2: error: expected unqualified-id before numeric constant

#define echoPin 6

^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino:11:11: note: in expansion of macro ‚ÄėechoPin‚Äô

const int echoPin = 7;

^

ServoV2:26: error: expected unqualified-id before ‚Äė{‚Äô token

{

^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function ‚Äėvoid loop()‚Äô:

ServoV2:42: error: redefinition of ‚Äėvoid loop()‚Äô

void loop()

^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino:22:6: note: ‚Äėvoid loop()‚Äô previously defined here

void loop() {

^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function ‚Äėvoid sweep(int)‚Äô:

ServoV2:200: error: expected ‚Äė}‚Äô at end of input

}

^

exit status 1
expected initializer before ‚Äėconst‚Äô

This report would have more information with
‚ÄúShow verbose output during compilation‚ÄĚ
option enabled in File ‚Üí Preferences.

Programming is full of traps!

Read Railroader’s reply #2 again.

Or read the error again in your opening post; it does not complain about the const, it complains about the semicolon at the end of the #include

Might need a void setup() somewhere in there.

sterretje:
Read Railroader's reply #2 again.

Or read the error again in your opening post; it does not complain about the const, it complains about the semicolon at the end of the #include

I did add a semi colon at the end of the #include command after i read that post but again same error

I did add a semi colon at the end of the #include command

Well, don't do that.

evanmars:
Might need a void setup() somewhere in there.

I even tried that, it still says you need an initializer before it.

Please anybody who understands how to fix this, please tell me quick as this is a project i have to finish in less than a week.
Thank you for your help

Here's the deal:. Post all the code, post all the error messages.

Otherwise, you're just a time-waster, yours and ours.

This is the code that i changed when you guys told me to.

#include <Servo.h>;



//Setup the variables for the HC-SR04

// initializer supposed to be before
const int trigPin = 6;
//^^^^^^^^^^^^
// thats the problem
const int echoPin = 7;

// create servo object to control a servo

// a maximum of eight servo objects can be created

Servo myservo;



// variable to store the servo position

int pos = 0;


void setup (){

  // initialize serial communication:

  Serial.begin(9600);



  // attaches the servo on pin 9 to the servo object

  myservo.attach(9);



}

void loop()

{

  // establish variables for duration of the ping,

  // and the distance result in inches and centimeters:

  long duration, inches, cm;



  // The sensor is triggered by a HIGH pulse of 10 or
  // more microseconds.

  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  pinMode(trigPin, OUTPUT);

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);



  // Read the signal from the sensor: a HIGH pulse whose

  // duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off of an object.

  pinMode(echoPin, INPUT);

  duration = pulseIn(echoPin, HIGH);



  // convert the time into a distance

  inches = microsecondsToInches(duration);

  cm = microsecondsToCentimeters(duration);



  //Tell the Arduino to print the measurement in the serial console

  Serial.print(inches);

  Serial.print("in, ");

  Serial.print(cm);

  Serial.print("cm");

  Serial.println();



  // This if-else statement tells the Arduino at what distance

  // it should trigger the servo, what the servo should do,

  // and what it should do if the distance is too far away.

  if (inches <= 24) {
    sweep(3);



  }

  else if (inches >= 24) {
    myservo.write(pos);

  }



  // Tell the Arduino to wait 0.10 seconds before pinging the

  // Ultrasonic Sensor again.

  delay(100);

}

// Converts the microseconds reading to Inches

long microsecondsToInches(long microseconds)

{

  // According to Parallax's datasheet for the PING))), there are

  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

  // second). This gives the distance travelled by the ping, outbound

  // and return, so we divide by 2 to get the distance of the obstacle.

  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

  return microseconds / 74 / 2;

}

//Converts the Microseconds Reading to Centimeters

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;

}

//This sub-routein is what dictates the movement of the servo

void sweep(int NUM_OF_CYCLES)



{

  // Tells the Arduino to start this loop at 0 and incriment it by 1

  // each time the loop completes. This is how the Arduino knows to

  // stop the loop when a specific number of the
  // Sweep routein has been ran.

  for (int j = 0; j < NUM_OF_CYCLES; j++)
    // goes from 0 degrees to 180 degrees
    for (pos = 0; pos < 180; pos += 1)
    { // in steps of 1 degree
      // tell servo to go to position in variable 'pos'
      myservo.write(pos);
      // waits 15ms for the servo to reach the position delay(10); }
      // goes from 180 degrees to 0 degrees
      for (pos = 180; pos >= 1; pos -= 1) {
        // tell servo to go to position in variable 'pos'
        myservo.write(pos);
        // waits 15ms for the servo to reach the position
        delay(10);
      }
    }

Here is the error message after i put the void setup and the ; after the #include command that you guys told me to put.

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

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV1.ino:1:19: warning: extra tokens at end of #include directive

 #include <Servo.h>;

                   ^

ServoV2:8: error: expected initializer before 'const'

 const int trigPin = 6;

 ^

SmartBin_CodeV2:2: error: expected unqualified-id before numeric constant

 #define echoPin 6

                 ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino:11:11: note: in expansion of macro 'echoPin'

 const int echoPin = 7;

           ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void setup()':

ServoV2:26: error: redefinition of 'void setup()'

 void setup (){

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino:11:6: note: 'void setup()' previously defined here

 void setup() {

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void loop()':

ServoV2:42: error: redefinition of 'void loop()'

 void loop()

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino:22:6: note: 'void loop()' previously defined here

 void loop() {

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void sweep(int)':

ServoV2:200: error: expected '}' at end of input

     }

     ^

exit status 1
expected initializer before 'const'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

And also GrooveFlotilla have you seen the initial comment i put? There you can see the code i started with and the error message i got before i came to you guys.
Thank you for your time.

#include <Servo.h>;

You still haven’t fixed this.

Pete

el_supremo:

#include <Servo.h>;

You still haven’t fixed this.

Pete

So here is the error message without the ‚Äė;‚Äô at the end of #include <Servo.h>

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

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV1.ino:1:19: warning: extra tokens at end of #include directive

 #include <Servo.h>;

                   ^

ServoV2:8: error: expected initializer before 'const'

 const int trigPin = 6;

 ^

SmartBin_CodeV2:2: error: expected unqualified-id before numeric constant

 #define echoPin 6

                 ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino:11:11: note: in expansion of macro 'echoPin'

 const int echoPin = 7;

           ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void setup()':

ServoV2:26: error: redefinition of 'void setup()'

 void setup (){

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino:11:6: note: 'void setup()' previously defined here

 void setup() {

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void loop()':

ServoV2:42: error: redefinition of 'void loop()'

 void loop()

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino:22:6: note: 'void loop()' previously defined here

 void loop() {

      ^

C:\Users\Home\Documents\Arduino\SmartBin_CodeV2\ServoV2.ino: In function 'void sweep(int)':

ServoV2:200: error: expected '}' at end of input

     }

     ^

exit status 1
expected initializer before 'const'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

And here is the code to prove it

#include <Servo.h>



//Setup the variables for the HC-SR04

// initializer supposed to be before
const int trigPin = 6;
//^^^^^^^^^^^^
// thats the problem
const int echoPin = 7;

// create servo object to control a servo

// a maximum of eight servo objects can be created

Servo myservo;



// variable to store the servo position

int pos = 0;


void setup (){

  // initialize serial communication:

  Serial.begin(9600);



  // attaches the servo on pin 9 to the servo object

  myservo.attach(9);



}

void loop()

{

  // establish variables for duration of the ping,

  // and the distance result in inches and centimeters:

  long duration, inches, cm;



  // The sensor is triggered by a HIGH pulse of 10 or
  // more microseconds.

  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  pinMode(trigPin, OUTPUT);

  digitalWrite(trigPin, LOW);

  delayMicroseconds(2);

  digitalWrite(trigPin, HIGH);

  delayMicroseconds(10);

  digitalWrite(trigPin, LOW);



  // Read the signal from the sensor: a HIGH pulse whose

  // duration is the time (in microseconds) from the sending

  // of the ping to the reception of its echo off of an object.

  pinMode(echoPin, INPUT);

  duration = pulseIn(echoPin, HIGH);



  // convert the time into a distance

  inches = microsecondsToInches(duration);

  cm = microsecondsToCentimeters(duration);



  //Tell the Arduino to print the measurement in the serial console

  Serial.print(inches);

  Serial.print("in, ");

  Serial.print(cm);

  Serial.print("cm");

  Serial.println();



  // This if-else statement tells the Arduino at what distance

  // it should trigger the servo, what the servo should do,

  // and what it should do if the distance is too far away.

  if (inches <= 24) {
    sweep(3);



  }

  else if (inches >= 24) {
    myservo.write(pos);

  }



  // Tell the Arduino to wait 0.10 seconds before pinging the

  // Ultrasonic Sensor again.

  delay(100);

}

// Converts the microseconds reading to Inches

long microsecondsToInches(long microseconds)

{

  // According to Parallax's datasheet for the PING))), there are

  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per

  // second). This gives the distance travelled by the ping, outbound

  // and return, so we divide by 2 to get the distance of the obstacle.

  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf

  return microseconds / 74 / 2;

}

//Converts the Microseconds Reading to Centimeters

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;

}

//This sub-routein is what dictates the movement of the servo

void sweep(int NUM_OF_CYCLES)



{

  // Tells the Arduino to start this loop at 0 and incriment it by 1

  // each time the loop completes. This is how the Arduino knows to

  // stop the loop when a specific number of the
  // Sweep routein has been ran.

  for (int j = 0; j < NUM_OF_CYCLES; j++)
    // goes from 0 degrees to 180 degrees
    for (pos = 0; pos < 180; pos += 1)
    { // in steps of 1 degree
      // tell servo to go to position in variable 'pos'
      myservo.write(pos);
      // waits 15ms for the servo to reach the position delay(10); }
      // goes from 180 degrees to 0 degrees
      for (pos = 180; pos >= 1; pos -= 1) {
        // tell servo to go to position in variable 'pos'
        myservo.write(pos);
        // waits 15ms for the servo to reach the position
        delay(10);
      }
    }

There's a closing curly bracket missing at the end of your code. Adding that, I get this if I try to compile it:

Applications/Arduino-3.app/Contents/Java/libraries/Servo/src/avr/Servo.cpp:184:39: warning: unused parameter 'timer' [-Wunused-parameter]
 static void finISR(timer16_Sequence_t timer)
                                       ^

Sketch uses 3,886 bytes (12%) of program storage space. Maximum is 32,256 bytes.
Global variables use 241 bytes (11%) of dynamic memory, leaving 1,807 bytes for local variables. Maximum is 2,048 bytes.

You have TWO .ino files in the one project.

Arduino\SmartBin_CodeV2\ServoV1.ino
and
Arduino\SmartBin_CodeV2\ServoV2.ino

ServoV1 contains this error:

#include <Servo.h>;

That’s what is causing many of the other error messages including why it is complaining about you having two setup functions because it is trying to compile what should be two separate sketches as if they were one.

Pete

el_supremo:
You have TWO .ino files in the one project.

Three, there's also the main .ino
Arduino\SmartBin_CodeV2\SmartBin_CodeV2.ino

Geez. No wonder the compiler's barfin :slight_smile:

Pete

Your code post #11 is compiling with this error - exit status 1
expected '}' at end of input

I add }
and have no errors