Hello, my first post here....
Thanks for your work on this libary
I have a Project on an Feather M0 BLE board and have change the 1.5 Libary: The Piezo is connectet to two pins and the Status for high / low is rversed on the pins to get a louder level. Thie first impression was fine. I removed also unused code. What do you think about that?
A Question: Does this libary play the sound in background? I mean, does the code works on while a Sound is playing?
Thanks, Peter
// ---------------------------------------------------------------------------
// Created by Tim Eckel - teckel@leethost.com
// Copyright 2016 License: GNU GPL v3 http://www.gnu.org/licenses/gpl-3.0.html
//
// See "TimerFreeTone.h" for purpose, syntax, version history, links, and more.
// ---------------------------------------------------------------------------
#include "TimerFreeTone.h"
uint8_t _tft_volume[] = { 255, 200, 150, 125, 100, 87, 50, 33, 22, 2 }; // Duty for linear volume control.
void TimerFreeTone(uint8_t pin1, uint8_t pin2, unsigned long frequency, unsigned int duration, uint8_t volume) {
if (frequency == 0 || volume == 0) { // If frequency or volume are zero, just wait duration and exit.
delay(duration);
return;
}
frequency = 1000000 / frequency; // Calculate the square wave length (in microseconds).
uint32_t duty = frequency / _tft_volume[min(volume, 10) - 1]; // Calculate the duty cycle (volume).
pinMode(pin1, OUTPUT); // Set pin to output mode.
pinMode(pin2, OUTPUT);
uint32_t startTime = millis(); // Starting time of note.
while(millis() - startTime < duration) { // Loop for the duration.
digitalWrite(pin1,HIGH); // Set pin high.
digitalWrite(pin2, LOW);
delayMicroseconds(duty); // Square wave duration (how long to leave pin high).
digitalWrite(pin1,LOW); // Set pin low.
digitalWrite(pin2, HIGH); // Set pin low.
delayMicroseconds(frequency - duty); // Square wave duration (how long to leave pin low).
}
}