arduino a function-definition is not allowed here FULL Error Message

Hello i need help with my code i dont know what i did wrong but for some reason i am getting an error message. Saying a “function definition is not allowed here before ’ ’ token” this code is suppose to be for an ultrasonic parking sensor. Please can anyone help ive tried everything.

Below is the full error message and the code that i am using.

Arduino: 1.6.9 (Windows 10), Board: “Arduino/Genuino Uno”

D:\Arduino\Sketches\Ultrasonic_Sensor_Sketch\Ultrasonic_Sensor_Sketch.ino: In function ‘void setup()’:

Ultrasonic_Sensor_Sketch:25: error: a function-definition is not allowed here before ‘{’ token

void setup () {


Ultrasonic_Sensor_Sketch:36: error: a function-definition is not allowed here before ‘{’ token

void loop () {


Ultrasonic_Sensor_Sketch:82: error: expected ‘}’ at end of input



exit status 1
a function-definition is not allowed here before ‘{’ token

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

int tonePin = 4;            // Tone (Buzzer pin)
int trigPin = 9;            // Trig (Ultrasonic pin)
int echoPin = 10;           // Echo (Ultrasonic pin)
int clockPin = 11;          // Clock (74HC595 IC pin)
int latchPin = 12;          // Latch (74HC595 IC pin)
int dataPin = 13;           // Data (74HC595 IC pin)

byte possible_patterns [9] = {

int proximity = 0;
int duration;
int distance;

void setup () {
  Serial.begin (9600);
  pinMode (trigPin, OUTPUT);
  pinMode (echoPin, INPUT);
  pinMode (clockPin, OUTPUT);
  pinMode (latchPin, OUTPUT);
  pinMode (dataPin, OUTPUT);
  pinMode (tonePin, OUTPUT);

void loop () {
  digitalWrite (latchPin, LOW);
  digitalWrite (trigPin, HIGH);
  delayMicroseconds (1000);
  digitalWrite (trigPin, LOW);
  duration = pulseIn (echoPin, HIGH);
  distance = (duration/2)/29.1;
  if (distance >= 45 || distance <= 0) {
    Serial.println ("Out of range");
  else {
    Serial.print (distance);
    Serial.println (" cm");
  proximity = map (distance, 0, 45, 8, 0);
  Serial.println (proximity);
  if (proximity <= 0) {
    proximity = 0;
  else if (proximity >= 3 && proximity <= 4) {
    tone (tonePin, 200000, 200);
  else if (proximity >= 5 && proximity <= 6) {
    tone (tonePin, 5000, 200);
  else if (proximity >= 7 && proximity <= 8) {
    tone (tonePin, 1000, 200);
  shiftOut (dataPin, clockPin, MSBFIRST, possible_patterns [proximity]);
  digitalWrite (latchPin, HIGH);
  delay (600);
  noTone (tonePin);


I copied the entire code and it compiled without error.

I also recommend providing the entire error message.

FWIW I was able to get the posted code to compile under 1.6.3 without errors (I did get some odd error messages using 1.6.6 but I suspect my install has issues).

Perhaps I missed something due to lack of code tags?




It compiles fine for me, except for one warning:

/Users/john/Documents/Arduino/sketch_aug02d/sketch_aug02d.ino: In function 'void loop()':
/Users/john/Documents/Arduino/sketch_aug02d/sketch_aug02d.ino:58:31: warning: large integer implicitly truncated to unsigned type [-Woverflow]
     tone (tonePin, 200000, 200);

I don't think tone() can reach 200 kHz anyway.

I see nothing that would keep that code from compiling. There is something different between what you're trying to compile, and what we get when we copy/paste your code. Could your code be polluted with bogus non-printing characters? Try copy/pasting it into a serious text editor (notepad++, sublime, etc) and set it to show non-printing characters?

Depends on how smart the implementation of Tone() is - I think if you put it on a pin that can output hardware PWM from the same timer that Tone uses (ie, have Tone output on one of the PWM pins it breaks) it will use the hardware output compare module, so it can get way up there.

I certainly remember fixing that behavior on my attiny core for the 't85, which has a crazy timer.

@Cd2110, do not cross-post. Other thread removed.

I have updated the thread and added the full error message which someone asked me to do could someone please explain to me in detail why i am receiving this error message when i try and upload my code thanks .


Maybe the last comma in your possible_patterns array definition?

Builds without error.

@Cd2110, copy-then-paste the sketch from your original post. I suspect you have an errant character in your version that gets (has gotten) stripped away by the time it reaches a browser.

Hello thanks for your help @CodingBadly and very one else who's helped me out I still can't figure out why this is giving me an error message even once I've altered the code I still get it. I have to write a report on this and wasn't sure what I could put it down to the code not working anyone got any ideas for what I could put.

Cheers Callum

Post the verbose output from the compiler. Please use
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>