error: expected unqualified-id before 'volatile'

Hi everyone,

I had to switch my arduino from a Duemilanove to a Mega. Reasons being the number of I/O for my project.

My code stayed the same.

when i compile it under the Duemilanove, it completes compile with no errors.

but when i switch the board to the Mega it gives me the above error.

Does anyone have any advise? i have gone through my code with a fine comb, i could not find anything wrong with it.

Thanks

Generally there is a line number in front of the error message from the compiler...

There may be a semicolon missing at the end of the line before the reported error.

#include <SoftwareSerial.h>
#include <Servo.h>

 Servo servo1;  //tilt
 Servo servo2;  //pan

 
//******Pin assignments, Modify only with recpect to circuit wiring!!************
//Analog Pins
 int LDR    = 0;  
 
//Digital Pins 
 int piezo       = 22;
 int pan         = 23;
 int tilt        = 24;
 int RFwd        = 25;
 int RRvs        = 26;
 int pingPin     = 27;
 int LRvs        = 28;
 int LFwd        = 29;
 int sonarRight  = 30;
 int sonarLeft   = 31;
 int sonarCenterLeft = 32;
 int sonarCenterRight = 33;
//****************************************************************************** 


void setup() {
  Serial.begin(9600);  // initialize serial communication:
    bot_stop();  //stop robot
    beep();
    start();  //start system by touching the LDR
    delay(1000);
    servo_home();   //Assign  starting sevos
    delay(500);
    beep();  //ready
 }//end Void setup()

//***********************Function PING************************************
long PING(){
   // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches;
  

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(20);
  // The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
 // Serial.println(inches);
  return inches;
}// end  function "void PING"


//***********************Function left_sonar************************************
long left_sonar(){
    pinMode(sonarLeft, INPUT);
    long rangeLS = pulseIn(sonarLeft,HIGH);
    return(rangeLS/147);  
}// end function "left_sonar"


//***********************Function center_left_sonar************************************
long center_left_sonar(){
    pinMode(sonarCenterLeft, INPUT);
    long rangeCLS = pulseIn(sonarCenterLeft,HIGH);
    return(rangeCLS/147);  
}// end function "Center_left_sonar"


//***********************Function center_right_sonar************************************
long center_right_sonar(){
    pinMode(sonarCenterRight, INPUT);
    long rangeCRS = pulseIn(sonarCenterRight,HIGH);
    return(rangeCRS/147);  
}// end function "Center_right_sonar"



//***********************Function right_sonar************************************
long right_sonar(){
    pinMode(sonarRight, INPUT);
    long rangeRS = pulseIn(sonarRight,HIGH);
    return(rangeRS/147);  
}// end function "right_sonar"


//***********************Function start************************************
void start(){
   int val = analogRead(LDR);
   while(val>30){val=analogRead(LDR);
                 if(val<=30){break;}}//end if
}// end function "beep"

//***********************Function beep************************************
void beep(){
    pinMode(piezo, OUTPUT);
    digitalWrite(piezo, HIGH);
    delay(1000);
    digitalWrite(piezo, LOW);
}// end function "beep"



//***********************Function servo_home************************************
void servo_home(){
    servo1.attach(tilt);  
    servo2.attach(pan); 
    servo1.write(160);    //tilt          
    delay(15);
    servo2.write(90);         //pan   
    delay(15);
}// end function "servo_home"



//***********************Function bot_stop************************************
void bot_stop(){
  pinMode(LFwd, OUTPUT);
  pinMode(LRvs, OUTPUT);
  pinMode(RFwd, OUTPUT);
  pinMode(RRvs, OUTPUT);
  
  digitalWrite(LFwd, LOW);
  digitalWrite(LRvs, LOW);
  digitalWrite(RFwd, LOW);
  digitalWrite(RRvs, LOW);
}// end function "bot_stop"
  

//***********************Function bot_fwd************************************
void bot_fwd(){
    pinMode(LFwd, OUTPUT);
    pinMode(LRvs, OUTPUT);
    pinMode(RFwd, OUTPUT);
    pinMode(RRvs, OUTPUT);
    servo_home();
    digitalWrite(LFwd, HIGH);
    digitalWrite(LRvs, LOW);
    digitalWrite(RFwd, HIGH);
    digitalWrite(RRvs, LOW);
}// end function "bot_fwd"

//***********************Function bot_rvs************************************
void bot_rvs(){
    pinMode(LFwd, OUTPUT);
    pinMode(LRvs, OUTPUT);
    pinMode(RFwd, OUTPUT);
    pinMode(RRvs, OUTPUT);
    servo_home();  
    digitalWrite(LFwd, LOW);
    digitalWrite(LRvs, HIGH);
    digitalWrite(RFwd, LOW);
    digitalWrite(RRvs, HIGH);
}// end function "bot_RVS"


//***********************Function clear_path************************************
int clear_path(){
int pos2=0;
int pos3=0;
int dir=0;
    for(pos2=0; pos2<=180; pos2++)  {   //scan 180deg
                                    long inches = PING(); //Detect      
                                      if(dir<inches){dir=inches;
                                                      pos3=pos2;}
                                      if(dir>=inches){dir=dir;}
    servo2.write(pos2);  // scan 
    delay(40);         // waits 15ms for the servo to reach the position 
  } //end for(pos2=0; pos2<=180; pos2++)
 
    servo2.write(pos3);              // point to the clearest path
    delay(40);         // waits 15ms for the servo to reach the position    
    delay(2000);
    beep();
    beep(); 
    return dir;  //return the clearest path
}// end function "clear_path"


//***********************Function object_detected************************************
int object_detect(int safe_zone){  
  int detected = 0;
    
  long  inches_ping = PING(); //Detect
  long  inches_left_sonar = left_sonar();  //Detect
  long  inches_right_sonar = right_sonar();  //Detect
  long  inches_CRS = center_right_sonar();  //Detect
  long  inches_CLS = center_left_sonar();  //Detect
  
    if(inches_ping<=safe_zone || inches_left_sonar<=safe_zone || inches_right_sonar<=safe_zone || inches_CRS<=safe_zone || inches_CLS<=safe_zone){  //object detected within safe zone?  
    bot_stop();  //stop robot
    detected = 1; //set flag
    delay(1000); // Pause for a second  
          }//end if(inches_ping<=safe_zone || inches_left_sonar<=safe_zone || inches_right_sonar<=safe_zone)  
        
    return detected;
}//end object_detect()



//***********************Function good_clear_path()************************************
void good_clear_path(int safe_zone,int path){
  
    while(path<=safe_zone){ 
      servo_home();
      delay(3000); // Pause for 3 secondS
      bot_rvs();
      delay(3000); // reverse for 3 secondS
      bot_stop();  //stop robot 
      int path = clear_path();  //keep checking for clear path and point to it
      
      if(path>safe_zone){break;} //found clear path, get out of loop            
                           }//while(path<=safe_zone)                  
} // end function good_clear_path()
    

 //***********************Function microsecondsToInches()************************************
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;
}  //end function long microsecondsToInches()



/*
//***********************Function compas()************************************
int compas()
{
//CODE!!!!!!!!!!!!!!!!!!!!!!!!!
}  //end function int compas()

//***********************Function logdata()************************************
int logdata()
{
//CODE!!!!!!!!!!!!!!!!!!!!!!!!!
}  //end function int logdata()


//***********************Function getdata()************************************
int getdata()
{
//CODE!!!!!!!!!!!!!!!!!!!!!!!!!
}  //end function int getdata()
*/



//***********************Main Code Start******************************
//********************************************************************
//********************************************************************
//********************************************************************
void loop()
{
    int safe_zone =7;
        bot_fwd();  //Keep going forward
//    int obstacal = object_detect(safe_zone); //returns detected=1 , not detected=0, if =1 stops robot
    
  //  if(obstacal=1){
    //int path = clear_path();  //check for clear path and points to it and beeps
   //     good_clear_path(safe_zone,path);  //checks if the pointed path has any obstacals. if not good, robot reverses and checks for a clearer path
        //CODE----turn heading to the clearest path
        //CODE----if heading at new path, obstacal=0;
    }//end if(obstacal=1)
    



  }//end void loop     
//***********************Main Code End********************************
//********************************************************************
//********************************************************************
//********************************************************************

Hi,

Thank you for your replies.

I have posted my code above.

The code compiles when i switch the board to the duemilinove, but does not compile with the mega.

I went through the code again, but could not find anything missing.

I need help...

and as deSilva already said: on which line do you get the compile error?

The line number I got when I compiled it was -13. Not very helpful.

You have mismatches curly brackets in your loop function.
An extra }

And PING give me problems when I compiled it. I renamed that function to ZING (and all the references) and it compiles.

Is PING a reserved word?

void setup(){
}

void loop(){
}

long PING(){
return 7;
}

does not compile
error: expected unqualified-id before 'volatile'

void setup(){
}

void loop(){
}

long ZING(){
return 7;
}

Does compile

Is PING a reserved word?

Ahhh...mystery solved.

The identifier PING is not a C or C++ reserved word. However, it is #defined in the file <avr/iomxx_01.h>, which is included when you compile for an ATmega1280. The compiler includes this automatically for the '1280, but it is not included when you compile for a '328p.

By using this as a function name, you have unknowingly (almost innocently) created code with a non-obvious syntax error. Not obvious to us mere humans, that is.

A convention used by many (most?) experienced programmers is:
For your own variable names and function names, it is recommended that you do not use all upper-case letters. Use all upper-case identifiers for #defined macros only.

This won't always prevent conflicts with things used by compiler and library writers, of course, but it definitely would have saved us from getting bogged down with this little head-scratcher.

On the other hand, when people hear seemingly arbitrary maxims like, "Don't use all upper-case variable and function names," it (sometimes) slides like water off of a duck's back if they haven't run into situations like this that don't violate any rules of the language but lead to hard-to-locate (but easy-to-prevent) problems like this.

I mean, as far as C and C++ language standards are concerned, it's perfectly legal to use PING() as a function name, and people may reject seemingly arbitrary "rules of thumb" or "generally accepted conventions" until they run into situations like this. (Or, maybe no one ever tried to clue them in.)

Now we know.

Regards,

Dave

1 Like

Thank you very much.

Never in a million years i would have figured this out on my own.

kind regards,
Keith

I've been guilty of dispensing the advice to follow standards, and not use all uppercase names for functions. While I wouldn't have been bit by this (not using all upper case names for anything but #define names), I'm surprised by this discrepancy between the 328 and the Mega.

I'm glad that you were able to find a solution to your problem, though.

so, what is the purpose of this "PING" on the 1280? The compiler obviously uses it for something, but what?

@UltraMagnus

purpose of this "PING" on the 1280

It's the address of input port G in terminology supported by avr-gcc. It is used in the definition of the port_to_input_PGM[] array in the '1280 section of the Arduino core file pins_arduino.c

The '328 doesn't have port G, so its avr-gcc io file doesn't define PING, and it's not used by avr-gcc or Arduino when compiling for a '328.

Regards,

Dave