Pages: [1]   Go Down
Author Topic: "function-definition is not allowed" error  (Read 2325 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

[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).

« Last Edit: August 16, 2009, 12:17:49 am by jafadmin » Logged

0
Offline Offline
Full Member
***
Karma: 0
Posts: 163
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 13
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: August 16, 2009, 08:33:43 am by RoyK » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Roy, Arduino is intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. (that is a direct quote from the arduino home page)

Things like auto-prototyping are there to insulate the user from programming details.  There are plenty of C environments that are available for formal learning  What makes the Arduino different  is how easy it is to create physical computing projects with minimal programming knowledge.

I agree that the Arduino environment hides many technical aspects that someone interested in learning software engineering should learn (for example, most aspects of object oriented programming are hidden). But that is a good part of what makes it so popular.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I reviewed the brackets, placed one and the sketch is working now, but there is another poroblem that came up when the program was uploaded.
the pseaker only plays the first note over and over again.
« Last Edit: August 16, 2009, 02:23:18 pm by 79317139 » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

you are missing the ++ at the end of your for loop, change:
  for (int i; i < length; i) {
to
  for (int i; i < length; i++) {
Logged

London, England
Offline Offline
Edison Member
*
Karma: 4
Posts: 1026
Go! Go! Arduinoooo !!!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

When posting code, please do not just paste it into the forum. Use the CODE button above the text box when entering text and paste it in between the commands that appear. Thanks.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK I'll do that with the code, and I changed the i to i++. Now it works.
Logged

Pages: [1]   Go Up
Jump to: