From 6ec3ebccde3d3774e98944b8dfd98bc48b914c3f Mon Sep 17 00:00:00 2001 From: Z Date: Sat, 28 Dec 2024 20:25:30 +0800 Subject: [PATCH] Update Simple_tone.ino Fixed I2S tone issue. --- libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino index 935aa4bc50f..60819bb322d 100644 --- a/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino +++ b/libraries/ESP_I2S/examples/Simple_tone/Simple_tone.ino @@ -24,6 +24,8 @@ 2nd September 2021 Lucas Saavedra Vaz (lucasssvaz) 22nd December 2023 + anon + 28nd December 2024 */ #include @@ -36,10 +38,10 @@ i2s_data_bit_width_t bps = I2S_DATA_BIT_WIDTH_16BIT; i2s_mode_t mode = I2S_MODE_STD; i2s_slot_mode_t slot = I2S_SLOT_MODE_STEREO; -const int halfWavelength = (sampleRate / frequency); // half wavelength of square wave +const int halfWavelength = (sampleRate / frequency / 2); // half wavelength of square wave int32_t sample = amplitude; // current sample value -int count = 0; +unsigned int count = 0; I2SClass i2s; @@ -47,6 +49,8 @@ void setup() { Serial.begin(115200); Serial.println("I2S simple tone"); + i2s.setPins(5, 25, 26); + // start I2S at the sample rate with 16-bits per sample if (!i2s.begin(mode, sampleRate, bps, slot)) { Serial.println("Failed to initialize I2S!"); @@ -61,7 +65,9 @@ void loop() { } i2s.write(sample); // Right channel + i2s.write(sample >> 8); i2s.write(sample); // Left channel + i2s.write(sample >> 8); // increment the counter for the next sample count++;