Skip to content
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

Implemented tone and noTone; fixes #980 #6402

Merged
merged 4 commits into from
Mar 10, 2022
Merged

Implemented tone and noTone; fixes #980 #6402

merged 4 commits into from
Mar 10, 2022

Conversation

PilnyTomas
Copy link
Contributor

@PilnyTomas PilnyTomas commented Mar 9, 2022

Issue #980 requested tone to be implemented.
tone in Arduino documentation
noTone in Arduino documentation

Summary

This PR provides new implementation of functions tone and noTone to be used with esp32.

Impact

New functions are written with almost the same API as Arduino original functions. This implementation adds the option to change duty (default is 50% the same as in Arduino).

Closes #980

@SuGlider
Copy link
Collaborator

SuGlider commented Mar 9, 2022

@PilnyTomas - will you also implement the Queue mode to this PR, in order to allow tone() to run in background in a separated task, as @me-no-dev suggested?

@PilnyTomas
Copy link
Contributor Author

Yeah, I can do that...

Copy link
Member

@P-R-O-C-H-Y P-R-O-C-H-Y left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL

cores/esp32/Arduino.h Outdated Show resolved Hide resolved
cores/esp32/Tone.cpp Outdated Show resolved Hide resolved
cores/esp32/Tone.cpp Outdated Show resolved Hide resolved
@VojtechBartoska VojtechBartoska added the Type: Feature request Feature request for Arduino ESP32 label Mar 9, 2022
cores/esp32/Tone.cpp Outdated Show resolved Hide resolved
@me-no-dev me-no-dev merged commit 68daea4 into espressif:master Mar 10, 2022
@luc-github
Copy link
Contributor

luc-github commented Mar 11, 2022

Sorry to ask, may be I misunderstood something - I see channel must be done manually before calling tone(...) using void setToneChannel(uint8_t channel = 0); as by default channel is 0 static uint8_t _channel = 0; and do not change unless void setToneChannel(uint8_t channel = 0); what ever the pin is used.

Any reason not to set channel according pin number when calling tone(...) as it is defined here:
https://github.com/espressif/arduino-esp32/blob/master/tools/sdk/esp32/include/driver/include/driver/adc.h#L23

Sorry if it is stupid question

@PilnyTomas
Copy link
Contributor Author

Hi, the channel can be ignored altogether- it is preset by default to 0, but you can, if you want, change it with the setToneChannel. Calling the function without a parameter will set the channel to default zero.
setToneChannel(); // will set to 0
The tone is not using ADC, it is using PWM (see LED Control in IDF and LED Control in Arduino-ESP32).
The PWM channel is independent of a pin so you can really create any permutation of pin x channel.
This implementation simply provides same API as Arduino tone() and noTone(). The intention is to be compatible with Arduino standard functions. The tone is unfortunately limited to one channel at a time.
If you want really want to take advantage of ESP32 I suggest taking look at the mentioned LED Control in Arduino-ESP32 - you will be able to use all the channels in parallel.

@luc-github
Copy link
Contributor

ok understood - thank you for the clarification ^_^

@TS6000
Copy link

TS6000 commented Mar 12, 2022

Hello
I have big problems on UART2.
Since the change from 1.xx to 2.02 data is lost.
I work with Arduino Studio.

I use UART2 with the pins (GPIO) 12 and 13

Torsten

@SuGlider
Copy link
Collaborator

Hello
I have big problems on UART2.
Since the change from 1.xx to 2.02 data is lost.
I work with Arduino Studio.

I use UART2 with the pins (GPIO) 12 and 13

Torsten

Please open a new issue, with more details and a sketch that demonstrate the issue.

@SrGalindo
Copy link

Is there any example on how to use this new Tone library? I can't make it work.
Thanks.

@SuGlider
Copy link
Collaborator

@SrGalindo - https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/ has a lot of examples linked at the end of the web page.

This example is nice: https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody

@SrGalindo
Copy link

@SrGalindo - https://www.arduino.cc/reference/en/language/functions/advanced-io/tone/ has a lot of examples linked at the end of the web page.

This example is nice: https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody

@SuGlider I've started with that example in my esp8266 without problem, although using it in my ESP32 it doesn't work, that's why I was looking for some example of this tone library for ESP32. If I'm not wrong I think is not as easy as tone(pin,frequenzy,duration).
Thanks

@SuGlider
Copy link
Collaborator

Yes, it is as simples as tone(pin, freq, duration);
I tested the Melody example directly from Arduino IDE -> Menu -> File -> Examples -> 02 Digital -> toneMelody
It worked fine, just had to setup the pin.

What exactly do you see as issue?

@SuGlider
Copy link
Collaborator

Working Example:

Connection:
ESP32 PIN 4 ---> (+) Buzzer ---> GND

/*
  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/toneMelody
*/

#include "pitches.h"

#define TONE_PIN 4

// notes in the melody:
int melody[] = {
  NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
  4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {
  // iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 8; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(TONE_PIN, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(TONE_PIN);
  }
}

void loop() {
  // no need to repeat the melody.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature request Feature request for Arduino ESP32
Projects
None yet
Development

Successfully merging this pull request may close these issues.

esp32 tone
8 participants