Bonjour à tous,
J'essaie de modifier un sketch de Web Radio à base d'ESP32 + Max 98357A en lui incorporant une Rotary encoder pour régler le volume et, à terme, le choix des stations.
Pour ce faire, j'ai utilisé ce sketch de radio tout simple : ESP32 I2S Internet Radio. Full Tutorial & explanation of I2S. For PCM5102 & MAX98357A I2S Decoders. - XTronical
auquel j'ai ajouté ce sketch de rotary encoder : GitHub - RalphBacon/226-Better-Rotary-Encoder---no-switch-bounce: An improved sketch to cater for switch bounce, courtesy of Marko Pinteric
Si les deux sketchs fonctionnent correctement indépendamment, ce n'est plus le cas après compilation des deux .
En effet, l'incrémentation ou décrémentation du volume n'intervient qu'après plusieurs rotations du Rotary encoder. C'est assez aléatoire comme le montre cet extrait de la console série :
Setup completed
digitalRead
digitalRead
lrsum = 0
digitalRead
lrsum = 0
digitalRead
lrsum = 0
digitalRead
lrsum =+4
Volume = 7
digitalRead
lrsum = 0
digitalRead
lrsum = 0
digitalRead
lrsum =+4
Volume = 8
digitalRead
lrsum = 0
digitalRead
lrsum = 0
digitalRead
digitalRead
lrsum = 0
digitalRead
lrsum = 0
digitalRead
lrsum = 0
Je butte sur ce problème depuis plusieurs jours et j'espère que vous pourrez m'aider.
Ci dessous mon code :
#include "Arduino.h"
#include "WiFi.h"
#include "Audio.h"
// Digital I/O used
#define I2S_DOUT 26 // DIN connection
#define I2S_BCLK 27 // Bit clock
#define I2S_LRC 25 // Left Right Clock
//uint8_t volume = 5; // range is 0 to 21
Audio audio;
//Rotary_encoder
// Rotary encoder pins
#define PIN_A 34
#define PIN_B 35
#define PUSH_BTN 32
// A turn counter for the rotary encoder (negative = anti-clockwise)
int rotationCounter = 200;
int volume = 6;
// Flag from interrupt routine (moved=true)
volatile bool rotaryEncoder = false;
// Interrupt routine just sets a flag when rotation is detected
void IRAM_ATTR rotary()
{
rotaryEncoder = true;
}
// Rotary encoder has moved (interrupt tells us) but what happened?
// See https://www.pinteric.com/rotary.html
int8_t checkRotaryEncoder()
{
// Reset the flag that brought us here (from ISR)
rotaryEncoder = false;
static uint8_t lrmem = 3;
static int lrsum = 0;
static int8_t TRANS[] = {0, -1, 1, 14, 1, 0, 14, -1, -1, 14, 0, 1, 14, 1, -1, 0};
// Read BOTH pin states to deterimine validity of rotation (ie not just switch bounce)
int8_t l = digitalRead(PIN_A);
int8_t r = digitalRead(PIN_B);
Serial.println("digitalRead");
// Move previous value 2 bits to the left and add in our new values
lrmem = ((lrmem & 0x03) << 2) + 2 * l + r;
// Convert the bit pattern to a movement indicator (14 = impossible, ie switch bounce)
lrsum += TRANS[lrmem];
/* encoder not in the neutral (detent) state */
if (lrsum % 4 != 0)
{
Serial.println("lrsum = 0");
return 0;
}
/* encoder in the neutral state - clockwise rotation*/
if (lrsum == 4)
{
Serial.println("lrsum =+4");
lrsum = 0;
return 1;
}
/* encoder in the neutral state - anti-clockwise rotation*/
if (lrsum == -4)
{
Serial.println("lrsum =-4");
lrsum = 0;
return -1;
}
Serial.println("fin digital read");
// An impossible rotation has been detected - ignore the movement
lrsum = 0;
return 0;
}
//Fin Rotary_encoder
String ssid = "*************";
String password = "**********";
void setup() {
Serial.begin(115200);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED) delay(1500);
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
//audio.setVolume(6); // 0...21
audio.setVolume(volume);
//***************Rotary_encoder********************
// The module already has pullup resistors on board
pinMode(PIN_A, INPUT);
pinMode(PIN_B, INPUT);
// But not for the push switch
pinMode(PUSH_BTN, INPUT_PULLUP);
// We need to monitor both pins, rising and falling for all states
//attachInterrupt(digitalPinToInterrupt(PIN_A), rotary, CHANGE);
//attachInterrupt(digitalPinToInterrupt(PIN_B), rotary, CHANGE);
attachInterrupt (PIN_A, rotary, CHANGE);
attachInterrupt (PIN_B, rotary, CHANGE);
Serial.println("Setup completed");
//***************Fin rotary_encoder*****************
// audio.connecttohost("http://www.wdr.de/wdrlive/media/einslive.m3u");
// audio.connecttohost("http://macslons-irish-pub-radio.com/media.asx");
// audio.connecttohost("http://mp3.ffh.de/radioffh/hqlivestream.aac"); // 128k aac
// audio.connecttohost("http://mp3.ffh.de/radioffh/hqlivestream.mp3"); // 128k mp3
audio.connecttohost("http://vis.media-ice.musicradio.com/CapitalMP3"); // 128k mp3
// audio.connecttospeech("Wenn die Hunde schlafen, kann der Wolf gut Schafe stehlen.", "de");
// audio.connecttohost("http://media.ndr.de/download/podcasts/podcast4161/AU-20190404-0844-1700.mp3"); // podcast
}
void loop()
{
// Has rotary encoder moved?
if (rotaryEncoder)
{
// Get the movement (if valid)
int8_t rotationValue = checkRotaryEncoder();
// If valid movement, do something
if (rotationValue != 0)
{
if (rotationValue < 1) (volume = volume - 1);
else (volume = volume + 1);
Serial.print("Volume = "), Serial.println (volume);
}
}
if (digitalRead(PUSH_BTN) == LOW)
{
volume = 0;
Serial.print("X");
Serial.println(volume);
// Wait until button released (demo only! Blocking call!)
while (digitalRead(PUSH_BTN) == LOW)
{
delay(100);
}
}
audio.loop();
}
/*
// optional
void audio_info(const char *info) {
Serial.print("info "); Serial.println(info);
}
void audio_id3data(const char *info) { //id3 metadata
Serial.print("id3data "); Serial.println(info);
}
void audio_eof_mp3(const char *info) { //end of file
Serial.print("eof_mp3 "); Serial.println(info);
}
void audio_showstation(const char *info) {
Serial.print("station "); Serial.println(info);
}
void audio_showstreaminfo(const char *info) {
Serial.print("streaminfo "); Serial.println(info);
}
void audio_showstreamtitle(const char *info) {
Serial.print("streamtitle "); Serial.println(info);
}
void audio_bitrate(const char *info) {
Serial.print("bitrate "); Serial.println(info);
}
void audio_commercial(const char *info) { //duration in sec
Serial.print("commercial "); Serial.println(info);
}
void audio_icyurl(const char *info) { //homepage
Serial.print("icyurl "); Serial.println(info);
}
void audio_lasthost(const char *info) { //stream URL played
Serial.print("lasthost "); Serial.println(info);
}
void audio_eof_speech(const char *info) {
Serial.print("eof_speech "); Serial.println(info);
}
*/
J'ai mis des "mouchards" à plusieurs endroits pour voir où ça coince!
Merci à vous.