On an Arduino board with an ATmega328, the tone() function can be called in two different ways:
tone(pin, frequency) ;
// or
tone(pin, frequency, duration);
Frequency in Hz, duration is milliseconds.
It grabs hold of Timer/counter 2, an 8 bit timer, and runs it in CTC mode (Clear Timer on Compare match) and uses the timer’s Compare Match A interrupt to do the work of toggling the pin.
Do not connect a pin directly to a sound card or amplifier. The pin is switching between 0 and 5v, which is way above “line” level. Also, connecting directly to a speaker needs a resistor to limit current flow and avoids letting the magic blue smoke out of the AVR (or whatever microcontroller is in use).
Only one pin can be generating a tone at any time. If a tone is already playing on a pin, a call to tone() with a different pin number will have no effect unless noTone() was called first.
If the call is made for the same pin as the one currently playing, this will set the tone’s frequency to that specified in the most recent call.
Your code above appears to be using two different pins. I assume you are using your Espressif board?
What have you got attached to the two pins named BZR01 and BZR02? Some buzzers require just a voltage while others require a pulse - like the tone() command generates.
I’m using the little piezo guys, just a + and a -.
So no chords on the Uno, but the WeMos should work.
I’m migrating this from Arduino. It’s the first thing I wrote by my very own self. It generates a random number between 1 and 1M, and then does a bunch of if tests.
Some of the random numbers are going to pass more than one test, so I want the LEDs and tones to go together. They act in series now.
It’s fun, running in the background. You get this odd buzz at different tones, durations.
Can you create a small sketch which recreates the problem, and post it please. tone() is an Arduino built in, so including “Arduino.h” should make it compile.
Failing that, it’s possible that there isn’t a tone() function for your microcontroller.
Butit still won’t upload because it refuses to see the library.
Gotta say, PIO library function sucks too often.
I’ve spent at least half the time I’ve spent with PIO losing my mind over these stupid libraries.
And this one was installed by the PIO library tool. And it’s in the project’s /lib folder.
Fails too often. It’s insanely frustrating. And these are Adafruit libraries. No reason they wouldn’t be just known.
Seriously sucks. I took the tones out of that simple script that just lights stinking LEDs, and it’s forced me to add BusIO to the /lib. and now this?
I hate it. Where did all of this new junk come from? None of that was a problem the first three thousand times I got a library to work.
Processing esp32dev (platform: espressif32; board: esp32dev; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/esp32dev.html
PLATFORM: Espressif 32 1.12.4 > Espressif ESP32 Dev Module
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-arduinoespressif32 3.10004.200129 (1.0.4)
- tool-esptoolpy 1.20600.0 (2.6.0)
- tool-mkspiffs 2.230.0 (2.30)
- toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 32 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <Adafruit GFX Library> 1.10.0
| |-- <Adafruit BusIO> 1.4.1
| | |-- <Wire> 1.0.1
| | |-- <SPI> 1.0
| |-- <SPI> 1.0
| |-- <Wire> 1.0.1
|-- <Adafruit SSD1306> 2.3.1
| |-- <Adafruit GFX Library> 1.10.0
| | |-- <Adafruit BusIO> 1.4.1
| | | |-- <Wire> 1.0.1
| | | |-- <SPI> 1.0
| | |-- <SPI> 1.0
| | |-- <Wire> 1.0.1
| |-- <SPI> 1.0
| |-- <Wire> 1.0.1
|-- <timelib>
|-- <Tone> 0.0.1
|-- <Wire> 1.0.1
Building in release mode
Compiling .pio\build\esp32dev\src\main.cpp.o
Generating partitions .pio\build\esp32dev\partitions.bin
Compiling .pio\build\esp32dev\libba3\Wire\Wire.cpp.o
Compiling .pio\build\esp32dev\lib5a1\SPI\SPI.cpp.o
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_BusIO_Register.cpp.o
Archiving .pio\build\esp32dev\lib5a1\libSPI.a
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_I2CDevice.cpp.o
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_I2CRegister.cpp.o
Archiving .pio\build\esp32dev\libba3\libWire.a
Compiling .pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_SPIDevice.cpp.o
Compiling .pio\build\esp32dev\libf23\Adafruit GFX Library\Adafruit_GFX.cpp.o
Compiling .pio\build\esp32dev\libf23\Adafruit GFX Library\Adafruit_GrayOLED.cpp.o
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:3:145: error: ISO C++ forbids declaration of 'Adafruit_I2CRegister' with no type [-fpermissive]
Adafruit_I2CRegister::Adafruit_I2CRegister(Adafruit_I2CDevice *device, uint16_t reg_addr, uint8_t width, uint8_t bitorder, uint8_t address_width) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:3:145: error: no 'int Adafruit_BusIO_Register::Adafruit_I2CRegister(Adafruit_I2CDevice*, uint16_t, uint8_t, uint8_t, uint8_t)' member function declared in class 'Adafruit_BusIO_Register'
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::write(uint8_t*, uint8_t)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:14:9: error: '_device' was not declared in this scope
if (! _device->write(buffer, len, true, addrbuffer, _addrwidth)) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::write(uint32_t, uint8_t)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:29:9: error: '_bitorder' was not declared in this scope
if (_bitorder == LSBFIRST) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'uint32_t Adafruit_BusIO_Register::read()':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:49:10: error: '_bitorder' was not declared in this scope
if (_bitorder == LSBFIRST) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::read(uint8_t*, uint8_t)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:62:9: error: '_device' was not declared in this scope
if (! _device->write_then_read(_buffer, 1, buffer, len)) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: In member function 'bool Adafruit_BusIO_Register::read(uint16_t*)':
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:73:7: error: '_bitorder' was not declared in this scope
if (_bitorder == LSBFIRST) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp: At global scope:
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:105:106: error: ISO C++ forbids declaration of 'Adafruit_I2CRegisterBits' with no type [-fpermissive]
Adafruit_I2CRegisterBits::Adafruit_I2CRegisterBits(Adafruit_I2CRegister *reg, uint8_t bits, uint8_t shift) {
^
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:105:106: error: no 'int Adafruit_BusIO_RegisterBits::Adafruit_I2CRegisterBits(Adafruit_I2CRegister*, uint8_t, uint8_t)' member function declared in class 'Adafruit_BusIO_RegisterBits'
lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:117:6: error: prototype for 'void Adafruit_BusIO_RegisterBits::write(uint32_t)' does not match any in class 'Adafruit_BusIO_RegisterBits'
void Adafruit_I2CRegisterBits::write(uint32_t data) {
^
In file included from lib\Adafruit BusIO/Adafruit_I2CRegister.h:1:0,
from lib\Adafruit BusIO\Adafruit_I2CRegister.cpp:1:
lib\Adafruit BusIO/Adafruit_BusIO_Register.h:66:8: error: candidate is: bool Adafruit_BusIO_RegisterBits::write(uint32_t)
bool write(uint32_t value);
^
*** [.pio\build\esp32dev\lib6d3\Adafruit BusIO\Adafruit_I2CRegister.cpp.o] Error 1
==================================================================================================== [FAILED]
In src/testAVRusart.cpp I have #include "AVRusart.h".
In lib/cBuffer/cBuffer.cpp I have #include "cBuffer.h". That header defines a circular buffer class.
In lib/AVRusart/AVRusart.cpp I have #include "AVRusart.h". That header defines my USART class.
Finally, in lib/AVRusart/AVRusart.h I have #include "cBuffer.h". Because the USART class has a pair of cBuffer class instances internally.
pio run compiles everything in src. When it sees the #include with a header file name in double quotes, it looks in:
The current directory;
The include directory;
All sub-directories in lib.
The compilation works. I have nothing in platformio.ini that mentions lib_deps etc. It just works.
I do not have to restart VSCodium. I do not have to restart Linux.
In another project, migrated from the Arduino IDE, I use the “servo” library in the IDE. So, I have the option to:
Install it globally - not a good idea as I’m using AVR for this project, but I also have some STM32 projects, it could lead to problems. Best avoided.
Install it locally, just for this project. pio lib install servo
Install it globally (no!) Or locally, using the pio home page in VSCodium.
Get the compiler to install it for me, locally. lib_deps = servo.
I chose to use lib_deps.
Once again, no need to restart, pio run will find the library, or download it as required.
I do not have a “global” folder where I keep lots of useful libraries. All mine are self written so live in a subdirectory of lib, or live wherever pio run puts them if I have a lib_deps in the ini file.
PlatformIO version 5.0.0 - it updated itself yesterday.
VSCodium (not the latest) 64 bit.
Linux Mint 19.3 64 bit.
Dell Vostro 17" laptop, 8G RAM, Core duo (Yes, it’s over 10 years old!)
With the number if problems you are having, I think there is either:
Something wrong with your operating system;
Something wrong with your PlatformIO installation;
Something wrong in how or where you installed the libraries;
Something wrong with the framework.
My advice.
Start simple. Try out a simple project with no libraries - blink. Get it working. So far so good!.
Try another project, like servo, that uses a single library. Use lib_deps = servo. Compile. Did it work? Yes, move on, No, get in touch again. Post the output from the compiler. Post your ini file. Post the structure of your project folder. Post how you installed the library etc.
Please, post everything in one post. If you need to, you can edit a post using the pencil icon at the bottom. (I have to edit my posts all the time, spelling!)
I don’t know what the cause of your latest error is - but since it didn’t happen the “first three thousand times”… and you were just making changes to the code (removing tone)… it’s probably something you changed that you shouldn’t have!
tone()/noTone() does work on the AVR and ESP8266 platforms without issue - it is only missing for the ESP32 platform, with the workaround being to use the functions mentioned in that github issue, or a library.