Go Down

Topic: toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc. (Read 80298 times) previous topic - next topic

teckel

Hi, sorry for replying in an old thread, but this is exactly what I need, but it won't compile on the new nano Every (ATMega4809).  Is it easily fixable or is there an alternative?

Thank you,
Probably easily fixable but not so much without the hardware in-hand (which I don't have). You could try fixing the library for your specific microcontroller and then submit the fix.
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

dprovencher

Probably easily fixable but not so much without the hardware in-hand (which I don't have). You could try fixing the library for your specific microcontroller and then submit the fix.
I've been digging around at http://ww1.microchip.com/downloads/en/DeviceDoc/megaAVR-0-series-Family-Data-Sheet-40002015C.pdf and the Arduino files and this is too complicated for my brain.  From what I understand it seems all the registers and every constant need to be changed.  All I did up to now in my life is solder wires, resistors, leds, etc. nothing fancy like microcontrollers and such.  I wanted to try and do an alarm thing with light patterns and sound for when my homemade baby gate was open instead of just a steady lit led.  I got everything working but wanted to use more of my 5vrms speakers so the sound would be louder.

Is there a way to do this with normal components like diodes and transistors?

dprovencher

In the end this is what I did.  Seems to work, I can tell the difference, but I don't think it's twice as loud.  Works for my purpose though.

Code: [Select]
// duration : ms
void toneAlmostAC(byte pinA, byte pinB, unsigned int frequency, unsigned int duration)
{
  // Compared to tone(), frequency is a bit off...
  // Matches found :
  // 800 1%
  // 2000 3%
  frequency = (int)((float)frequency * 1.01); //1%
 
  for (unsigned long i = 1; i <= (frequency * duration / 1000); i++)
  {
    digitalWrite(pinA, LOW);
    digitalWrite(pinB, HIGH);
    delayMicroseconds(1000000 / frequency / 2);
    digitalWrite(pinB, LOW);
    digitalWrite(pinA, HIGH);
    delayMicroseconds(1000000 / frequency / 2);
  }
  digitalWrite(pinA, LOW);
  digitalWrite(pinB, LOW);
}

teckel

In the end this is what I did.  Seems to work, I can tell the difference, but I don't think it's twice as loud.  Works for my purpose though.

Code: [Select]
// duration : ms
void toneAlmostAC(byte pinA, byte pinB, unsigned int frequency, unsigned int duration)
{
  // Compared to tone(), frequency is a bit off...
  // Matches found :
  // 800 1%
  // 2000 3%
  frequency = (int)((float)frequency * 1.01); //1%
 
  for (unsigned long i = 1; i <= (frequency * duration / 1000); i++)
  {
    digitalWrite(pinA, LOW);
    digitalWrite(pinB, HIGH);
    delayMicroseconds(1000000 / frequency / 2);
    digitalWrite(pinB, LOW);
    digitalWrite(pinA, HIGH);
    delayMicroseconds(1000000 / frequency / 2);
  }
  digitalWrite(pinA, LOW);
  digitalWrite(pinB, LOW);
}

You can just use my ToneAC2 library instead.

https://bitbucket.org/teckel12/arduino-toneac2/wiki/Home

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

dprovencher

You can just use my ToneAC2 library instead.

https://bitbucket.org/teckel12/arduino-toneac2/wiki/Home

Tim
I run into the same problems I did with toneAC :

Code: [Select]
/tmp/143048879/build/sketch/toneAC2.cpp:32:3: error: 'OCR2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:33:7: error: 'TCNT2' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:34:3: error: 'TCCR2B' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:34:17: error: 'WGM22' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:35:3: error: 'TCCR2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:35:17: error: 'WGM20' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:35:30: error: 'WGM21' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:36:3: error: 'TIMSK2' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:36:18: error: 'OCIE2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:45:3: error: 'TIMSK2' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:45:18: error: 'OCIE2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:46:3: error: 'TCCR2B' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:46:17: error: 'CS22' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:47:17: error: 'WGM20' was not declared in this scope


I need to change all the registers and constants and I simply don't really know what to look for in the 4809 PDF this is way above my current skill level.

I'm gonna try and play with bitWrite to see if I can get the frequency right.  PORTD.OUT first and second bits are the ports I use (A0 et A1).

teckel

I run into the same problems I did with toneAC :

Code: [Select]
/tmp/143048879/build/sketch/toneAC2.cpp:32:3: error: 'OCR2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:33:7: error: 'TCNT2' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:34:3: error: 'TCCR2B' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:34:17: error: 'WGM22' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:35:3: error: 'TCCR2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:35:17: error: 'WGM20' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:35:30: error: 'WGM21' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:36:3: error: 'TIMSK2' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:36:18: error: 'OCIE2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:45:3: error: 'TIMSK2' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:45:18: error: 'OCIE2A' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:46:3: error: 'TCCR2B' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:46:17: error: 'CS22' was not declared in this scope
/tmp/143048879/build/sketch/toneAC2.cpp:47:17: error: 'WGM20' was not declared in this scope


I need to change all the registers and constants and I simply don't really know what to look for in the 4809 PDF this is way above my current skill level.

I'm gonna try and play with bitWrite to see if I can get the frequency right.  PORTD.OUT first and second bits are the ports I use (A0 et A1).
You really need to use the timers to do this, doesn't work with how your doing it or even using bitwise operations. That's why it's not louder for you..

I would just use an amp instead. That will work for sure as doing it at the microcontroller level won't work if it's changed a bunch.

Or, just get a classic arduino.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

dprovencher

You really need to use the timers to do this, doesn't work with how your doing it or even using bitwise operations. That's why it's not louder for you..

I would just use an amp instead. That will work for sure as doing it at the microcontroller level won't work if it's changed a bunch.

Or, just get a classic arduino.

Tim
Thanks for the suggestion, but I got something working before I read your message.  I don't know if I get the same volume I would get with toneAC but now I can tell it's way louder and I can get frequencies I couldn't before with tone().  Sound is way clearer too and no wierd noises.  The only thing bothering me is I can't do anything else while the loop is running, but as my other components are only LEDs I can work around it.

Code: [Select]
// frequency : hertz
// duration : milliseconds
void toneAlmostAC(unsigned int frequency, unsigned int duration)
{
  // A1 and A2 hardcoded (PD1, PD2)
 
  PORTD.OUT &= 0b11111001; // Clear A1 and A2
  PORTD.OUT |= 0b00000010; // Set A1
  unsigned long delay = 1000000 / frequency / 2;
  unsigned long end = millis() + duration;
  while(millis() < end)
  {
    PORTD.OUT ^= 0b00000110; // Toggle A1 and A2
    delayMicroseconds(delay);
  }
  PORTD.OUT &= 0b11111001; // Clear A1 and A2
}

teckel

Thanks for the suggestion, but I got something working before I read your message.  I don't know if I get the same volume I would get with toneAC but now I can tell it's way louder and I can get frequencies I couldn't before with tone().  Sound is way clearer too and no wierd noises.  The only thing bothering me is I can't do anything else while the loop is running, but as my other components are only LEDs I can work around it.

Code: [Select]
// frequency : hertz
// duration : milliseconds
void toneAlmostAC(unsigned int frequency, unsigned int duration)
{
  // A1 and A2 hardcoded (PD1, PD2)
 
  PORTD.OUT &= 0b11111001; // Clear A1 and A2
  PORTD.OUT |= 0b00000010; // Set A1
  unsigned long delay = 1000000 / frequency / 2;
  unsigned long end = millis() + duration;
  while(millis() < end)
  {
    PORTD.OUT ^= 0b00000110; // Toggle A1 and A2
    delayMicroseconds(delay);
  }
  PORTD.OUT &= 0b11111001; // Clear A1 and A2
}

You'll need to use timers to do multiple things as once, which is the reason for the ToneAC library. What you're doing is blocking mode, where one thing runs at a time.

I'd look into using timers on your microcontroller, or using an amp, or use a classic Arduino that works with all the libraries out there.

Tim
My platforms Arduino, Teensy 3.2, Arduino Pro Mini, ATmega328
My libraries: NewPing, LCDBitmap, toneAC, toneAC2, NewTone, TimerFreeTone
My projects: https://dogblocker.com & https://baconorbeer.com
My beer: Great Lakes Brewing Co. Lake Erie Monster

Go Up