Thank you Val42 !! Your simple tone() function resolves it before they fix it. Very simple and straight to the point! Fantastic!
Val42:
While searching for a better solution for my original problem, I came across ESP32 Arduino: Controlling a buzzer with PWM. In summary, even though the ESP-32 libraries don't have a tone(), they do have other functions that will let you do the equivalent.
#define BUZZER_PIN 13
#ifdef ARDUINO_ARCH_ESP32
#define SOUND_PWM_CHANNEL 0
#define SOUND_RESOLUTION 8 // 8 bit resolution
#define SOUND_ON (1<<(SOUND_RESOLUTION-1)) // 50% duty cycle
#define SOUND_OFF 0 // 0% duty cycle
void tone(int pin, int frequency, int duration)
{
ledcSetup(SOUND_PWM_CHANNEL, frequency, SOUND_RESOLUTION); // Set up PWM channel
ledcAttachPin(pin, SOUND_PWM_CHANNEL); // Attach channel to pin
ledcWrite(SOUND_PWM_CHANNEL, SOUND_ON);
delay(duration);
ledcWrite(SOUND_PWM_CHANNEL, SOUND_OFF);
}
#endif
This code is in an example program that I wrote. I found what is defined when the IDE compiles for ESP-32. "Show verbose output during: [ ] compilation" will show you too. This allows me to use the same code for ESP-32 or Arduino UNO.
Since the PWM channel can be 0-15, I tried running three piezo buzzers at the same time at different frequencies. I got only one frequency out though. I'm thinking that the PWM can only run at one frequency at a time, but I'm still looking into this.
int buzzer_pin[] = {13, 12, 14};
void tones(int freq1, int freq2, int freq3, int duration)
{
// Set up pins
ledcSetup(SOUND_PWM_CHANNEL+0, freq1, SOUND_RESOLUTION); // Set up PWM channel
ledcAttachPin(buzzer_pin[0], SOUND_PWM_CHANNEL+0); // Attach channel to pin
ledcSetup(SOUND_PWM_CHANNEL+1, freq2, SOUND_RESOLUTION); // Set up PWM channel
ledcAttachPin(buzzer_pin[1], SOUND_PWM_CHANNEL+1); // Attach channel to pin
ledcSetup(SOUND_PWM_CHANNEL+2, freq3, SOUND_RESOLUTION); // Set up PWM channel
ledcAttachPin(buzzer_pin[2], SOUND_PWM_CHANNEL+2); // Attach channel to pin
// Sound buzzers
ledcWrite(SOUND_PWM_CHANNEL+0, SOUND_ON);
ledcWrite(SOUND_PWM_CHANNEL+1, SOUND_ON);
ledcWrite(SOUND_PWM_CHANNEL+2, SOUND_ON);
delay(duration);
ledcWrite(SOUND_PWM_CHANNEL+0, SOUND_OFF);
ledcWrite(SOUND_PWM_CHANNEL+1, SOUND_OFF);
ledcWrite(SOUND_PWM_CHANNEL+2, SOUND_OFF);
}
I could have made a mistake in this code (for multiple tones), but I don't see it.