teckel:
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.
// 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
}