Go Down

Topic: "function-definition is not allowed" error (Read 2 times) previous topic - next topic

daxter4090

I am trying to run this code:

int pingPin = 7;
int speakerPin = 9;


int length = 100; // the number of notes
char notes[] = "CgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCgCg"; // a space represents a rest
int beats[] = {  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,};
int tempo = 300;

void playTone(int tone, int duration) {
 for (long i = 0; i < duration * 1000L; i += tone * 2) {
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(tone);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone);
 }
}
void playNote(char note, int duration) {
 char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
 int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
 
 // play the tone corresponding to the note name
 for (int i = 0; i < 8; i++) {
   if (names == note) {
     playTone(tones, duration);
   }
 }
}
void setup(){
 pinMode(speakerPin, OUTPUT);
}

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

 // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
 // We give a short LOW pulse beforehand to ensure a clean HIGH pulse.
 pinMode(pingPin, OUTPUT);
 digitalWrite(pingPin, LOW);
 delayMicroseconds(2);
 digitalWrite(pingPin, HIGH);
 delayMicroseconds(5);
 digitalWrite(pingPin, LOW);

 // 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);
 if (inches <= 10) i == 1



   ;for (int i; i < length; i) {
   if (notes == ' ') {
     delay(beats * tempo); // rest
   } else {
     playNote(notes, beats * tempo);
   }
   
   // pause between notes
   delay(tempo / 10);







}


long microsecondsToInches (long microseconds)
[glow]{[/glow]

 // 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/2801 [glow][/glow]PINGv1.3.pdf
 return microseconds / 74 / 2;
 }

and this error:

In function 'void loop()':
error: a function-definition is not allowed here before '{' token

comes up.
Can someone please tell me why?

jafadmin

#1
Aug 16, 2009, 05:08 am Last Edit: Aug 16, 2009, 07:17 am by jafadmin Reason: 1
[edit] First see Fjornir's comments below. He is correct.

either put the microsecondsToInches ()  function up above the setup() function or put a function prototype for it up there.

The compiler has to know what the function is that your using before you call it (just like a data variable).


Fjornir

I believe you will find that jafadmin is mistaken. The arduino IDE will automagically generate prototypes for the functions you use.

In your case it looks like you have unbalanced {}s. Run over your code again looking for a {   where you don't have a } at the appropriate following spot. At a glance I found a missing }.


jafadmin

You are correct, Fjornir. Thanks for pointing out that difference from formal C.


RoyK

#4
Aug 16, 2009, 11:13 am Last Edit: Aug 16, 2009, 03:33 pm by RoyK Reason: 1
I understand that the Arduino IDE will generate prototypes automatically but I'm not altogether sure that is a good thing. It does indeed make life simpler for a beginner/hobbyist however I'd argue that it teaches one learning C a bad habit that will come back to bIte her if she starts working in another environment that expects the C protocols to be adhered to.

Perhaps generating the prototypes automatically along with a warning message (explanatory - as a teaching tool) would be better.

Go Up