-
Notifications
You must be signed in to change notification settings - Fork 7.6k
tone was not declared in this scope #1720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think tone is part of the ledc functions in esp32. I don't have a speaker around to test, but try this:
|
yeah it works! thank you |
|
If this is working for you, please close the issue. |
thanks again! |
Just in case anybody finds this, you can make a quick swap like this: void tone(byte pin, int freq) {
ledcSetup(0, 2000, 8); // setup beeper
ledcAttachPin(pin, 0); // attach beeper
ledcWriteTone(0, freq); // play tone
} Don't know how to make it so it asynchronously stops playing after an amount of time, though. To stop it, you can do that again with 0 as the frequency, or do something like: int playing = 0;
void tone(byte pin, int freq) {
ledcSetup(0, 2000, 8); // setup beeper
ledcAttachPin(pin, 0); // attach beeper
ledcWriteTone(0, freq); // play tone
playing = pin; // store pin
}
void noTone() {
tone(playing, 0);
} |
or my library. https://github.com/lbernstone/Tone |
Why was this closed? A tone() abstraction still doesn't seem to exist in this core. |
Although this functionality is unavailable in Espressif's arduino-esp32 library, members of the community have found various work-arounds such as using the native LED Control functions to generate PWM signals. However, looking more closely at arduino-esp32 library, not only has Espressif provided a clean API for generating tones, they've provided an interface for generating specific PWM frequencies for specific notes on the chromatic scale in different octaves. double ledcWriteNote(uint8_t chan, note_t note, uint8_t octave){
const uint16_t noteFrequencyBase[12] = {
// C C# D Eb E F F# G G# A Bb B
4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902
};
if(octave > 8 || note >= NOTE_MAX){
return 0;
}
double noteFreq = (double)noteFrequencyBase[note] / (double)(1 << (8-octave));
return ledcWriteTone(chan, noteFreq);
} Although not directly compatible with Arduino's typedef enum {
NOTE_C, NOTE_Cs, NOTE_D, NOTE_Eb, NOTE_E, NOTE_F, NOTE_Fs, NOTE_G, NOTE_Gs, NOTE_A, NOTE_Bb, NOTE_B, NOTE_MAX
} note_t; Example UsageHere is an example of generating PWM frequencies at 50% duty cycle to play the C-major scale. void playScale(uint8_t pin, uint8_t channel) {
ledcAttachPin(pin, channel);
ledcWriteNote(pin, NOTE_C, 4);
delay(500);
ledcWriteNote(pin, NOTE_D, 4);
delay(500);
ledcWriteNote(pin, NOTE_E, 4);
delay(500);
ledcWriteNote(pin, NOTE_F, 4);
delay(500);
ledcWriteNote(pin, NOTE_G, 4);
delay(500);
ledcWriteNote(pin, NOTE_A, 4);
delay(500);
ledcWriteNote(pin, NOTE_B, 4);
delay(500);
ledcDetachPin(pin)
} |
Thanks for this uncovering this functionality @Thomascountz! We incorporated it into one of our teaching lessons for our Physical Computing course: https://makeabilitylab.github.io/physcomp/esp32/tone.html. Hope it's useful to others! |
Tone is now available from in the ESP32Servo library (which can be found in Arduino library manager). |
Oh, this is great. Thanks! |
Added info about third party ESP32 servo library as mentioned here: espressif/arduino-esp32#1720 (comment)
Verified working code and schematic. @Thomascountz I was following your comment and it took me quite some time to realize the void playScale(uint8_t pin, uint8_t channel)
{
ledcAttachPin(pin, channel);
ledcWriteNote(channel, NOTE_C, 4);
delay(100);
ledcWriteNote(channel, NOTE_D, 4);
delay(100);
ledcWriteNote(channel, NOTE_E, 4);
delay(100);
ledcWriteNote(channel, NOTE_F, 4);
delay(100);
ledcWriteNote(channel, NOTE_G, 4);
delay(100);
ledcWriteNote(channel, NOTE_A, 4);
delay(100);
ledcWriteNote(channel, NOTE_B, 4);
delay(100);
ledcDetachPin(pin);
} |
After adding the shared function to the code I wrote for esp32, the "notone" error persisted due to the ezbuzzer library I got in the ide interface. Therefore I removed the "ezbuzzer" library and the error was gone. I am hesitant as I am a novice coder. I just wanted to ask if I could have done wrong by removing this library. The process worked like this: My installed libraries in the first stage: #include <Wire.h> Here are the error codes I got for the tone(buzzer, 500) line: I disabled this line.(//), Then I added the following codes: void setup() { I thought that the issue might have something to do with the "ezBuzzer" library and I disabled the library(//) What I want to ask is: Now that I have removed the "ezbuzzer" library, should I add another library for the "ledcWriteTone" code written for esp32? |
|
Hardware:
Board: ESP32 Dev Module
Core Installation/update date: 11/jul/2017
IDE name: Arduino IDE
Flash Frequency: 80Mhz
Upload Speed: 115200
Description:
when i use tone function the arduino console says:
"tone was not declared in this scope."
if the esp32 core doesn't have tone function please add it.
thanks.
Sketch:
The text was updated successfully, but these errors were encountered: