Pages: [1] 2   Go Down
Author Topic: 'Func-def not allowed...' error on IR MP3 controller  (Read 1820 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all
-
I'm mashing Bill Porter's MP3 shield player:
http://www.billporter.info/sparkfun-mp3-shield-arduino-library/comment-page-6/#comments

with Sparkfun's IR controller:
http://www.sparkfun.com/tutorials/291

I'll post the code below.  I need help because at int getIRKey()  { 
I get:
a function-definition is not allowed here before '{' taken

sketch_oct02a.cpp: In function 'void loop()':
sketch_oct02a:39: error: a function-definition is not allowed here before '{' token
sketch_oct02a:63: error: expected `}' at end of input
sketch_oct02a:63: error: expected `}' at end of input
sketch_oct02a:63: error: expected `}' at end of input

I've used this exact piece of code in another IR controller and not had this problem.  In fact, I just copied it over.  I can't find a bracket solution at all so I'm looking for your help, please. 
I can't tell where I haven't closed a bracket, perhaps?   Any thoughts on what I'm doing wrong?

Code:
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;

//new ir code
int irPin = A0; //Sensor pin 1 wired to Arduino's pin 2
int statLED = 13; //Toggle the status LED every time Power is pressed
int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)

void setup() {

   pinMode(irPin, INPUT);
       // turn on the MP3 chip
    MP3player.begin();
}


void loop() {
{
  int key = getIRKey(); 
  if (key !=0) // bingo! bring on the noise! bring on the funk!
  {
   
        // start playing the sound
  switch(key)

{
  case 144: MP3player.playTrack(1); break;
  case 145: MP3player.playTrack(2); break;
  case 146: MP3player.playTrack(3); break;
  case 147: MP3player.playTrack(4); break;

}

int getIRKey() {
  int data[12];
  int i;

  while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
 
  for(i = 0 ; i < 11 ; i++)
    data[i] = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses
 
  for(i = 0 ; i < 11 ; i++) //Parse them
  {    
    if(data[i] > bin_1) //is it a 1?
      data[i] = 1;
    else if(data[i] > bin_0) //is it a 0?
      data[i] = 0;
    else
      return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
  }

  int result = 0;
  for(i = 0 ; i < 11 ; i++) //Convert data bits to integer
    if(data[i] == 1) result |= (1<<i);

  return result; //Return key number
}
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at "loop" and count the closing braces.

Consistent indentation helps prevent this kind of thing
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1836
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You need another close curly brace after the end of the switch statement, just before the 'int getIRKey() {' line.  I believe the IDE has an option to indent code, and if you use it, it can help identify where you are missing closing brackets.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Make that two closing braces
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ayer, Massachusetts, USA
Offline Offline
Edison Member
*
Karma: 53
Posts: 1836
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Make that two closing braces
Yes you are right.  I missed the one on the if statement.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The help is much appreciated but I think I'm still in the weeks!
I did this:
Code:
void loop() {
{
  int key = getIRKey(); 
  if (key !=0) // bingo! bring on the noise! bring on the funk!
    } 
    // start playing the sound
switch(key)
{
  case 144: MP3player.playTrack(1); break;
  case 145: MP3player.playTrack(2); break;
  case 146: MP3player.playTrack(3); break;
  case 147: MP3player.playTrack(4); break;

  }
)
  int getIRKey() {
  int data[12];
  int i;

And I get:
expected primary-expression before '}' token

sketch_oct02a.cpp: In function 'void loop()':
sketch_oct02a:26: error: expected primary-expression before '}' token
sketch_oct02a:26: error: expected `;' before '}' token
sketch_oct02a:28: error: 'key' was not declared in this scope
sketch_oct02a:36: error: expected primary-expression before ')' token
sketch_oct02a:36: error: expected `;' before ')' token
sketch_oct02a:61: error: expected `}' at end of input
Logged

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The help is much appreciated but I think I'm still in the weeks!

Do you not understand how to match { with }?

Take a look at a few examples to see how they are supposed to be used together.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought I had a clue about it but I guess not, because otherwise I wouldn't be asking for help.
Logged

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 19
Posts: 363
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One method I use to keep the braces matched is I always type both if I type one. Then I put the code inside them.

I also use ctrl-t (format) in the IDE. It will give an error if the brackets don't match. The IDE will also highlight the matching bracket if you put your cursor on one.

Once you figure out the brackets look at the if statement around the switch. You should be able to eliminate that and have the switch handle any value of key.

Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought I had a clue about it but I guess not, because otherwise I wouldn't be asking for help.

It's dead simple. For each { there must be a matching }. In the end, the number of { must be equal to the number of }.

Each { } pair identifies a block of code. Just like a pair of ( ) encloses an expression.
You immediately see something missing here
Code:
if ( a== 2
don't you ? Likewise, you should see something missing here too:
Code:
while (value < 3) {  k++;

I hope this clarifies the subject.
« Last Edit: October 03, 2012, 01:23:51 am by tuxduino » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK - the example is appreciated - now all my brackets seem to be closed, according to the IDE.
Now, when I compile, I get this:

expected primary-expression before '}' token

sparkfun_porter_mashup_2_100212.cpp: In function 'void loop()':
sparkfun_porter_mashup_2_100212:30: error: expected primary-expression before '}' token
sparkfun_porter_mashup_2_100212:30: error: expected `;' before '}' token
sparkfun_porter_mashup_2_100212.cpp: At global scope:
sparkfun_porter_mashup_2_100212:31: error: expected unqualified-id before '{' token

Is that still an indication of a bracket problem?  I googled the error and I get a lot of info about C++ programming.

Thanks.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25874
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, that is an indication of a problem, but without code, we can't tell what it is.
Consistent indentation helps greatly identifying problems like this.
« Last Edit: October 03, 2012, 01:41:49 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
error: expected `;' before '}' token

You probably just need to add the required ; at the end of line 30.

BTW, it's quite hard to debug other people's code just by looking at compilation errors and some source snippet (hint smiley-wink )
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hint taken.   smiley-grin
Code:
#include <SPI.h>
#include <SdFat.h>
#include <SdFatUtil.h>
#include <SFEMP3Shield.h>

SFEMP3Shield MP3player;

//new ir code
int irPin = A0; //Sensor pin 1 wired to Arduino's pin 2
int statLED = 13; //Toggle the status LED every time Power is pressed
int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 = 1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)

void setup() {

   pinMode(irPin, INPUT);
       // turn on the MP3 chip
    MP3player.begin();
}


void loop() {

  int key = getIRKey(); 
  if (key !=0) // bingo! bring on the noise! bring on the funk!

   
        // start playing the sound
  switch(key)
    }
{
  case 144: MP3player.playTrack(1); break;
  case 145: MP3player.playTrack(2); break;
  case 146: MP3player.playTrack(3); break;
  case 147: MP3player.playTrack(4); break;

}

int getIRKey() {
  int data[12];
  int i;

  while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
 
  for(i = 0 ; i < 11 ; i++)
    data[i] = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses
 
  for(i = 0 ; i < 11 ; i++) //Parse them
  {    
    if(data[i] > bin_1) //is it a 1?
      data[i] = 1;
    else if(data[i] > bin_0) //is it a 0?
      data[i] = 0;
    else
      return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
  }

  int result = 0;
  for(i = 0 ; i < 11 ; i++) //Convert data bits to integer
    if(data[i] == 1) result |= (1<<i);

  return result; //Return key number
}
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You probably meant:

Code:
void loop() {
  int key = getIRKey(); 
  if (key !=0) {
    switch(key) {
      case 144: MP3player.playTrack(1); break;
      case 145: MP3player.playTrack(2); break;
      case 146: MP3player.playTrack(3); break;
      case 147: MP3player.playTrack(4); break;
    }
  }
}
Logged

Pages: [1] 2   Go Up
Jump to: