A function-definition is not allowed here before '{' token

Hey guys, Im having trouble with my code for an ultrasonic sensor, but I keep getting this

./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/227253356/build -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -libraries /tmp/227253356/pinned -libraries /tmp/227253356/custom -fqbn arduino:avr:uno -build-cache /tmp -logger humantags -verbose=false /tmp/227253356/sketch_mar24b

/tmp/227253356/sketch_mar24b/sketch_mar24b.ino: In function 'long int microsecondsToInches(long int)':

/tmp/227253356/sketch_mar24b/sketch_mar24b.ino:43:51: error: a function-definition is not allowed here before '{' token

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; }

^

/tmp/227253356/sketch_mar24b/sketch_mar24b.ino:43:51: error: expected '}' at end of input

exit status 1

Spider 888--

Did you read this old thread before you decided to revive it and post on to the end?

The thread is all about missing and matching brackets in the code. Did you use the auto format tool as suggested and look for that?

You have not posted any code, but my crystal ball tells me that you have got the same problem.

My bad. This is the code:

const int pingPin = 2;

int sensingRangeUnit = 11; int buzzerLimit = 100; int buzzerFrequency;

void setup() {

pinMode(3, OUTPUT); // sets the digital pin 3 as output for a buzzer pinMode(4, OUTPUT); // sets the digital pin 4 as output for an LED pinMode(5, OUTPUT); // sets the digital pin 5 as output for an LED pinMode(6, OUTPUT); // sets the digital pin 6 as output for an LED pinMode(7, OUTPUT); // sets the digital pin 7 as output for an LED pinMode(8, OUTPUT); // sets the digital pin 8 as output for an LED pinMode(9, OUTPUT); // sets the digital pin 9 as output for an LED pinMode(10, OUTPUT); // sets the digital pin 10 as output for an LED pinMode(11, OUTPUT); // sets the digital pin 11 as output for an LED pinMode(12, OUTPUT); // sets the digital pin 12 as output for an LED pinMode(13, OUTPUT); // sets the digital pin 13 as output for an LED Serial.begin(9600);

}

void loop() { long duration, inches, cm;

pinMode(pingPin, OUTPUT); digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(5); digitalWrite(pingPin, LOW);

pinMode(pingPin, INPUT); duration = pulseIn(pingPin, HIGH);

// convert the time into a distance inches = microsecondsToInches(duration); cm = microsecondsToCentimeters(duration); Serial.print(inches); Serial.print("in, "); Serial.print(cm); Serial.print("cm"); Serial.println();

if (inches < (sensingRangeUnit*1)) { digitalWrite(4, HIGH); } else { digitalWrite(4, LOW); }

if (inches < (sensingRangeUnit*2)) { digitalWrite(5, HIGH); } else { digitalWrite(5, LOW); }

if (inches < (sensingRangeUnit*3)) { digitalWrite(6, HIGH); } else { digitalWrite(6, LOW); }

if (inches < (sensingRangeUnit*4)) { digitalWrite(7, HIGH); } else { digitalWrite(7, LOW); }

if (inches < (sensingRangeUnit*5)) { digitalWrite(8, HIGH); } else { digitalWrite(8, LOW); }

if (inches < (sensingRangeUnit*6)) { digitalWrite(9, HIGH); } else { digitalWrite(9, LOW); }

if (inches < (sensingRangeUnit*7)) { digitalWrite(10, HIGH); } else { digitalWrite(10, LOW); }

if (inches < (sensingRangeUnit*8)) { digitalWrite(11, HIGH); } else { digitalWrite(11, LOW); }

if (inches < (sensingRangeUnit*9)) { digitalWrite(12, HIGH); } else { digitalWrite(12, LOW); }

if (inches < (sensingRangeUnit*10)) { digitalWrite(13, HIGH); } else { digitalWrite(13, LOW); }

if (inches < buzzerLimit) { buzzerFrequency = (((buzzerLimit - inches)*255)/buzzerLimit); analogWrite(3, buzzerFrequency); } else { analogWrite(3, 0); } delay(100); }

long microsecondsToInches(long microseconds) { // According to Parallax's datasheet))), 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. return microseconds / 74 / 2; }

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; }

It was the last line of code that I had difficulty with

My bad. This is the code:

Please modify your post to use the code tags.

Use auto format on the code Ctrl +T or find it under tools in your sketch. See if you can see the brackets issue.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Your code is a mess. This might be the result of posting it here without code tags though I have my doubts; maybe copy/paste errors.

e.g.

void setup()
{

  pinMode(3, OUTPUT); // sets the digital pin 3 as output for a buzzer pinMode(4, OUTPUT); // sets the digital pin 4 as output for an LED pinMode(5, OUTPUT); // sets the digital pin 5 as output for an LED pinMode(6, OUTPUT); // sets the digital pin 6 as output for an LED pinMode(7, OUTPUT); // sets the digital pin 7 as output for an LED pinMode(8, OUTPUT); // sets the digital pin 8 as output for an LED pinMode(9, OUTPUT); // sets the digital pin 9 as output for an LED pinMode(10, OUTPUT); // sets the digital pin 10 as output for an LED pinMode(11, OUTPUT); // sets the digital pin 11 as output for an LED pinMode(12, OUTPUT); // sets the digital pin 12 as output for an LED pinMode(13, OUTPUT); // sets the digital pin 13 as output for an LED Serial.begin(9600);

}

should have been something like

void setup()
{

  pinMode(3, OUTPUT); // sets the digital pin 3 as output for a buzzer
  pinMode(4, OUTPUT); // sets the digital pin 4 as output for an LED 
  pinMode(5, OUTPUT); // sets the digital pin 5 as output for an LED 
  pinMode(6, OUTPUT); // sets the digital pin 6 as output for an LED 
  pinMode(7, OUTPUT); // sets the digital pin 7 as output for an LED 
  pinMode(8, OUTPUT); // sets the digital pin 8 as output for an LED
  pinMode(9, OUTPUT); // sets the digital pin 9 as output for an LED
  pinMode(10, OUTPUT); // sets the digital pin 10 as output for an LED
  pinMode(11, OUTPUT); // sets the digital pin 11 as output for an LED
  pinMode(12, OUTPUT); // sets the digital pin 12 as output for an LED
  pinMode(13, OUTPUT); // sets the digital pin 13 as output for an LED
  Serial.begin(9600);
}

In the above, it's not a disaster (except for the fact that you only set one pin to output :slight_smile: ).

But for these two lines it results in compiler errors

long microsecondsToInches(long microseconds) { // According to Parallax's datasheet))), 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. return microseconds / 74 / 2; }

  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; }

Hi,
Your code should be like this;

const int pingPin = 2;

int sensingRangeUnit = 11; int buzzerLimit = 100; int buzzerFrequency;

void setup() {

  pinMode(3, OUTPUT); // sets the digital pin 3 as output for a buzzer
  pinMode(4, OUTPUT); // sets the digital pin 4 as output for an LED
  pinMode(5, OUTPUT); // sets the digital pin 5 as output for an LED
  pinMode(6, OUTPUT); // sets the digital pin 6 as output for an LED
  pinMode(7, OUTPUT); // sets the digital pin 7 as output for an LED
  pinMode(8, OUTPUT); // sets the digital pin 8 as output for an LED
  pinMode(9, OUTPUT); // sets the digital pin 9 as output for an LED
  pinMode(10, OUTPUT); // sets the digital pin 10 as output for an LED
  pinMode(11, OUTPUT); // sets the digital pin 11 as output for an LED
  pinMode(12, OUTPUT); // sets the digital pin 12 as output for an LED
  pinMode(13, OUTPUT); // sets the digital pin 13 as output for an LED
  Serial.begin(9600);

}

void loop() {
  long duration, inches, cm;

  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  if (inches < (sensingRangeUnit * 1))
  {
    digitalWrite(4, HIGH);
  }
  else
  {
    digitalWrite(4, LOW);
  }

  if (inches < (sensingRangeUnit * 2))
  {
    digitalWrite(5, HIGH);
  }
  else
  {
    digitalWrite(5, LOW);
  }

  if (inches < (sensingRangeUnit * 3))
  {
    digitalWrite(6, HIGH);
  }
  else
  {
    digitalWrite(6, LOW);
  }

  if (inches < (sensingRangeUnit * 4))
  {
    digitalWrite(7, HIGH);
  }
  else
  {
    digitalWrite(7, LOW);
  }

  if (inches < (sensingRangeUnit * 5))
  {
    digitalWrite(8, HIGH);
  }
  else
  {
    digitalWrite(8, LOW);
  }

  if (inches < (sensingRangeUnit * 6))
  {
    digitalWrite(9, HIGH);
  }
  else
  {
    digitalWrite(9, LOW);
  }

  if (inches < (sensingRangeUnit * 7))
  {
    digitalWrite(10, HIGH);
  }
  else
  {
    digitalWrite(10, LOW);
  }

  if (inches < (sensingRangeUnit * 8))
  {
    digitalWrite(11, HIGH);
  }
  else
  {
    digitalWrite(11, LOW);
  }

  if (inches < (sensingRangeUnit * 9))
  {
    digitalWrite(12, HIGH);
  }
  else
  {
    digitalWrite(12, LOW);
  }

  if (inches < (sensingRangeUnit * 10))
  {
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }

  if (inches < buzzerLimit)
  {
    buzzerFrequency = (((buzzerLimit - inches) * 255) / buzzerLimit);
    analogWrite(3, buzzerFrequency);
  }
  else
  {
    analogWrite(3, 0);
  }
  delay(100);
}

Forget about putting as much on a line as possible, you have to be a able to read and follow your code.
Putting it in long line format will not compile or run it any faster.

Your code does not compile due to the two conversion statements

inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

Have you got ALL the code?
Where did you get it?

Thanks... Tom... :slight_smile: