Another 'expected unqualified-id...'

Hi folks :slight_smile:

Having a compiling issue, with the seemingly standard 'expected unqualified-id' error upon compiling.

If anyone could please have a squiz at my code and see where the (quite likely very simple) problem is I'd be very grateful.

I've Googled until my vision went blurry and still cannot see the problem :frowning:

/*----- Code for Drew's proximity sensor -----*/

//Define pins
const int pingPin = 7;                  
const int echoPin = 6;                  
const int RedLEDpin = 12;               
const int GrnLEDpin = 13;               

//Start of setup code
void setup() {
  Serial.begin(9600);              
}

//Start of loop code
void loop() {
  long duration, inches, cm;                
  String BangOn = "BANG ON THE CLIP!";      
  pinMode(pingPin, OUTPUT);                 
  digitalWrite(pingPin, LOW);               
  delayMicroseconds(2);                     
  digitalWrite(pingPin, HIGH);              
  delayMicroseconds(10);                    
  digitalWrite(pingPin, LOW);               
  pinMode(echoPin, INPUT);                  
  duration = pulseIn(echoPin, HIGH);        
  inches = microsecondsToInches(duration);  
  cm = microsecondsToCentimeters(duration); 
  Serial.print(inches);                     
  Serial.print("in, ");                     
  Serial.print(cm);                         
  Serial.print("cm");                       
  Serial.println();
  delay(100);                               
}

if (inches < 2)                       
{
  digitalWrite(GrnLEDpin, HIGH);       
  digitalWrite(RedLEDpin, LOW);        
  Serial.print(BangOn);                
  delay(1000);                         
  digitalWrite(GrnLEDpin, LOW);        
}

long microsecondsToInches(long microseconds) {       
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) 
  return microseconds / 29 / 2;
}

Probably an errant curly bracket or summat.

Millions of thank in advance,

Drew :slight_smile:

did you look at the braces?

One missing near the end.

besides that issue, your microsecondsToInches() and microsecondsToCentimeters() are doing integer math so you may not be getting what you expect.

If you mean the opening one here...

long microsecondsToCentimeters(long microseconds) {  
  return microseconds / 29 / 2;
}

Then that's just a stupid mistake in removing the comments to get it under the character limit for the code tags, it is actually there when trying to compile.

My bad :frowning:

check your functions (they are in red in the listing on this page)

blh64 -

That has almost entirely lost me.

I was assuming math = math, apparently not.

Do you have a link to something that will explain what you mean (I'm not one for asking for help until my brain is totally melted) by integer math and what other math there is that I may need to be learning about?

This code isn't in a function:

if (inches < 2)

Looks like this..

if (inches < 2)                       
{
  digitalWrite(GrnLEDpin, HIGH);       
  digitalWrite(RedLEDpin, LOW);        
  Serial.print(BangOn);                
  delay(1000);                         
  digitalWrite(GrnLEDpin, LOW);        
}

Is floating on its own between functions.

And..

long microsecondsToCentimeters(long microseconds) 
  return microseconds / 29 / 2;
}

Whoops! Missing bracket at start.

-jim lee

AWESOME.

Got it to compile, just had to put the 'if' into the loop function and away it went.

Thanks a million people, probably the most helpful forum I have ever experienced, be proud.

Absolutely bouncing now lol

Again, thanks folks :slight_smile:

As an aside, is it OK to use 'float' instead of 'long' for my math here?

My Google-Fu suggests that would be the way to go.

I use float all the time. Depends on what you are doing and how you are using the stuff you are calculating. You designing an autopilot for the new spaceX orbiter? Then maybe you should look into some better math.

-jim lee

Yeah... I'll just use 'float'.

An orbiter is still a 'little bit' beyond me :smiley: :smiley: :smiley:

Thanks :+1:

Math for humans != math for microcontrollers. Humans almost always deal with real numbers (float) but microcontrollers do not.

Humans: 3/2 = 1.5
Computer: 3/2 = 1 (integer division so no floating point allowed)

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.