toneAC v1.2 - Twice the volume, higher quality, higher frequency, etc.

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.

// 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