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?
#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
}
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.
The help is much appreciated but I think I'm still in the weeks!
I did this:
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
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.
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.
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.
#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
}