Go Down

Topic: 'Func-def not allowed...' error on IR MP3 controller (Read 2077 times) previous topic - next topic

drwhofan

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: [Select]

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


AWOL

Look at "loop" and count the closing braces.

Consistent indentation helps prevent this kind of thing
"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.

MichaelMeissner

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.

AWOL

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

MichaelMeissner


Make that two closing braces

Yes you are right.  I missed the one on the if statement.

drwhofan

The help is much appreciated but I think I'm still in the weeks!
I did this:
Code: [Select]
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

Arrch


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.

drwhofan

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

Jimmy60

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.


tuxduino

#9
Oct 03, 2012, 01:12 am Last Edit: Oct 03, 2012, 08:23 am by tuxduino Reason: 1

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: [Select]
if ( a== 2
don't you ? Likewise, you should see something missing here too:
Code: [Select]
while (value < 3) {  k++;

I hope this clarifies the subject.

drwhofan

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.

AWOL

#11
Oct 03, 2012, 07:29 am Last Edit: Oct 03, 2012, 08:41 am by AWOL Reason: 1
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.
"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.

tuxduino

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

drwhofan

Hint taken.   :D
Code: [Select]

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

tuxduino

You probably meant:

Code: [Select]

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

Go Up