Problem with the ISR(TIMER1_COMPA_vect) macro

Hi!
I want to make a Wavetable Synthesis class, and im having a problem / understanding question
for the mentioned macro.

I made a private function for the WavetableSynthesizer class:

void WavetableSynthesizer::InterruptHandler()
{
	
	ISR(TIMER1_COMPA_vect)
	{
		if (soundPWM)
		{
			tempphase = phase0 + frequencyCoef;
			sig0 = wavetables[currentVoice][phase0>>8];
			phase0 = tempphase;
			OCR2A = sig0; // output the sample
		} else { //square wave 
			flag ^= 1;
			digitalWrite(speakerPin, flag);
		}
	}
	
}

(the static variables are defined in the class also)

but I get this error:

sketchbook/libraries/WavetableSynthesizer/WavetableSynthesizer.cpp: In member function ‘void WavetableSynthesizer::InterruptHandler()’:
.../sketchbook/libraries/WavetableSynthesizer/WavetableSynthesizer.cpp:43: error: expected unqualified-id before string constant
.../sketchbook/libraries/WavetableSynthesizer/WavetableSynthesizer.cpp:44: error: a function-definition is not allowed here before ‘{’ token
.../sketchbook/libraries/WavetableSynthesizer/WavetableSynthesizer.cpp:136: error: expected `}' at end of input

What am I doing wrong here? Or should I put this method elsewhere (not as private member)?

(Sourcecode samples from this tutorial: http://learning.codasign.com/index.php?title=Wavetable_Synthesis)

Greetings,
widarr

What am I doing wrong here?

You are trying to define the function inside another function. That is not allowed. Defining the function and calling it are two separate activities.

The ISR will be called when some timer based action occurs. In the ISR, you might be able to call a static method of your class. But, the class method is not what gets called when the interrupt happens, so it can't call the ISR.

I don't know if you can nest the definition of one function inside the other, the way you have done. Maybe you can, but I don't think so.

Thank you for the quick reply!

Do you have an idea how I can pack this function in my Synthesizer lib so that I have not not deal with it in the sketch?

Do you have an idea how I can pack this function in my Synthesizer lib so that I have not not deal with it in the sketch?

When your code is compiled and linked, where the function comes from doesn't matter. So, you can put the function in the library source file.

you mean in the WavetableSynthesizer.cpp as a static function?

you mean in the WavetableSynthesizer.cpp as a static function?

static, in classes, really only (to me, at least) means something with regards to methods. The ISR is NOT going to be a class method. So, no, not a static function. Just a regular function implementation.

Ok thank you :slight_smile:
I took the SD library as example (the trick with the extern keyword at the end of the .h file)
and it compiled.

Tomorrow I'll solder together an amplifier for my scrap loudspeaker, connect it and listen to what my Arduino has to say
:slight_smile: