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

While not written to do this, toneAC works well controlling a two-pin dual LED. With this and a potentiometer, the sketch below will flash the LED alternating between the two LED colors. This is happening in the background with control returning to your sketch to do other things as noted in the sketch. Every half second it will look at the pot and change the LED flashing speed. This sketch is designed to work with toneAC v1.1 that will be released once I get home

// ---------------------------------------------------------------------------
// Connect a two-pin dual LED to the following pins with inline 220 ohm resistor.
//   Pins  9 & 10 - ATmega328, ATmega128, ATmega8, Uno, Leonardo, etc.
//   Pins 11 & 12 - ATmega2560, ATmega1280, Mega
//   Pins 14 & 15 - Teensy 2.0
//   Pins 25 & 26 - Teensy++ 2.0
// Connect the center lead of a potentiometer to analog pin A0 and the other two leads to +5V and ground.
// ---------------------------------------------------------------------------

#include <toneAC.h>

unsigned long timestamp = 0; // Stores when the next time the routine is set to run.

void setup() {}

void loop() {
  if (millis() > timestamp) { // Is it time yet?
    timestamp += 500;         // Set the next time routine will run. 500 ms because the lowest frequency is 2 Hz, which is a half second.
    int pot = analogRead(A0);            // Read the potentiometer connected to analog pin A0.
    int freq = map(pot, 0, 1023, 2, 40); // Convert pot analog values to a range from 2 to 40 Hz.
    toneAC(freq, 10, 0, true);           // Set the frequency and have it run forever in the background (next event should take over in 500 ms).
  }
  /* Do a bunch of other stuff here, it won't affect toneAC doing its thing. */
}

Because toneAC is designed to be used for audio, duty has been purposed for linear volume control instead of fading the LED from one color to the other evenly. Basically, duty is linear for audio but not for lighting an LED. If there's a desire for linear LED duty control, making a few tweaks to the toneAC library or making a different library designed just for controlling a two color LED would be in order. The above sketch is a teaser that may help someone with a project.

Tim