Non-blocking tone on ATtiny84

Hello,

quick question: is there a way that the ATtiny84 can play a non-blocking tone?

For an upcoming project, I would like to be able to give off a signal tone through a subminiature speaker while at the same time handling a few other things like pin change interrupts and detecting if input pins are high or low.

I'm aware that ISRs work outside the realm of blocking code, but of course I want my sketch to do various things with the results of the ISRs right as they happen.

At the moment, the ATtiny84A looks like the best microcontroller to use, as the code itself will be relatively short and simple, but I will need more pins than the ATtiny85 provides, and using an ATmega328 at this point seems a bit much.

  • carguy

a square wave tone simply needs the output to toggle.

#define Pin 10

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);

    pinMode (Pin, OUTPUT);
}

// -----------------------------------------------------------------------------
void loop (void)
{
    static unsigned long usecLst = 0;
           unsigned long usec    = micros ();

#define TonePeriod 1000     // 1msec == 1kHz

    if (usec - usecLst > (TonePeriod/2))  {
        usecLst = usec;
        digitalWrite (Pin, ! digitalRead (Pin));
    }

    // do other stuff
}

Thanks, I will try that then.