Go Down

Topic: Tone Function will not work with FreqCount (Read 575 times) previous topic - next topic

Sanoom

Jul 30, 2019, 07:55 pm Last Edit: Jul 30, 2019, 08:06 pm by Sanoom
Hello, I'm trying to design an audio down converter for a project. I'm getting an error message that it will not compile for the board (compile, not upload). I read that FreqCount can affect other PWM pins, but I wasn't expecting them to be unusable and I don't know how to trouble shoot this. The line that's giving my problems is the tone function. I think it's trying to say that there's an error with the timer and the tone function interrupting it.

Code: [Select]
#include <FreqCount.h>


void setup() {
  Serial.begin(57600);

}

void loop() {
  // put your main code here, to run repeatedly:
  FreqCount.begin(1000);
  long freq = 0.0;
  if(FreqCount.available())
  {
    long freq = FreqCount.read();
    Serial.print("Frequency: ");
    Serial.println(freq);
    FreqCount.end();

  }
  FreqCount.end();
  long pitch = map(freq, 10000, 20000, 100, 1000);
  Serial.print("New Pitch: ");
  Serial.println(pitch);
  tone(1, pitch, 10);

}


Here is the error message:

Tone.cpp.o (symbol from plugin): In function `timer0_pin_port':
(.text+0x0): multiple definition of `__vector_7'
libraries/FreqCount-1.3.0/FreqCount.cpp.o (symbol from plugin): (.text+0x0): first defined here
collect2: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Uno.

DVDdoug

Yes, it looks like there is a conflict between the tone() library and FreqCount library.  You might be able to modify one of the libraries but there may be more than one conflict, so if you were to fix that one another might 'pop-up'.  Or, there could be a hardware conflict (both using the same timers/registers, etc.).

You might have to write your own tone or frequency function(s) or write your own libraries.  That would probably involve using the chip's internal registers/timers, etc., and it would probably require assembly language (depending on the speed/frequency).  

It's hard to know if it will can work at all since both things are timing-critical and everything has to be done by a shared processor...  Doing two or more timing-critical things at once can get tricky.  

There are frequency divider chips/circuits that don't require programming software, if that kind of thing will work in your application.


cattledog

Congratulations on the use of code tags with your first post.

Quote
multiple definition of `__vector_7'
You are encountering a conflict between tone() and freqCount.h. which are both trying to use the Timer2_COMPA interrupt ISR vector.

Code: [Select]
#define TIMER2_COMPA_vect _VECTOR(7)   /* Timer/Counter2 Compare Match A */

Tone uses Timer2 for its output, and freqCount.h uses Timer2 to establish the time window for counting. Neither library can be easily changed.

I see a couple of possible solutions.

You can try the TimerFreeTone library. https://bitbucket.org/teckel12/arduino-timer-free-tone/wiki/Home

If you want to write your own code, you can adopt the Timer1 external clock source counting methods of freqCount.h but gate the count with a software timer, and then Timer2 will be free for tone().

You can try migrate to a Leonardo which uses the 32U4 instead of the AT328. Tone() uses Timer3, and freqCount uses Timer1 and Timer4.



Sanoom

Thank you so much! I used the timer free tone and it solved the entire problem.

Go Up