![adafruit – Adafruit_LEDBackpack library interfering with analog enter studying adafruit – Adafruit_LEDBackpack library interfering with analog enter studying](https://techtoguide.com/wp-content/uploads/2023/12/esp32-Arduino-GFX-Library-breaks-after-reinstalling.png)
[ad_1]
I am attempting to do one thing I feel can be easy. I would wish to learn the enter of a electret microphone board and replace the Adafruit 24-segment LED Bar Graph. I bought every particular person merchandise working completely: I can learn the enter of the microphone on the analog pin and I can replace the LED bar graph utilizing I2C.
The difficulty happens after I tried to mix the 2 ideas. When the bard graph will get up to date, the analog enter reads HIGH (1023) and by no means adjustments. I used my normal debug methodology and commented out code till the issue went away, so I’ve narrowed down the problem to a single line of code (proper on the finish). When the bar.writeDisplay known as the analog enter perpetually reads 1023.
If I remark out that one line of code and re-upload my sketch the readings (within the Serial Monitor) are as anticipated (decrease when its quiet and better when its loud).
Below the hood Adafruit is utilizing the Wire library.
I am no good at wiring diagrams, however this is what I’ve:
RoboDyn Mega 2560 Professional (Embed)
5V, Gnd, SDA (20), SCL (21) to the LED Backpack
5V, Gnd, Out (A15/69)
That mentioned, I am comparatively assured this isn’t a wiring downside as all the things works till the software program will get ahold of it.
#embrace <Wire.h>
#embrace <Adafruit_GFX.h>
#embrace "Adafruit_LEDBackpack.h"
Adafruit_24bargraph bar = Adafruit_24bargraph();
int pin = 69;
unsigned int sampleWindow = 50; // millis
unsigned int maxScale = 24;
void setup() {
Serial.start(9600);
bar.start(0x70); // cross within the handle
}
void loop() {
unsigned lengthy startMillis = millis(); // Begin of pattern window
unsigned int peakToPeak = 0; // peak-to-peak stage
unsigned int pattern;
unsigned int signalMax = 0;
unsigned int signalMin = 1024;
whereas (millis() - startMillis < sampleWindow) {
pattern = analogRead(pin);
if (pattern < 1024) { // toss out spurious readings
if (pattern > signalMax) {
signalMax = pattern; // save simply the max ranges
}
else if (pattern < signalMin) {
signalMin = pattern; // save simply the min ranges
}
}
}
peakToPeak = signalMax - signalMin;
int displayPeak = map(peakToPeak, 0, 1023, 0, maxScale); // map 1v p-p stage to the max scale of the show
Serial.print("mic: "); Serial.print(peakToPeak); Serial.print(" "); Serial.println(displayPeak);
for (uint8_t b = 0; b < 24; b++) {
if (b <= displayPeak) {
int c = LED_GREEN;
if (b >= 20) {
c = LED_RED;
}
else if (b >= 16) {
c = LED_YELLOW;
}
bar.setBar(23 - b, c);
}
else {
bar.setBar(23 - b, LED_OFF);
}
//bar.writeDisplay(); // <------- THIS LINE
}
}
UPDATE: It seems to be one thing with the way in which Adafruit makes use of the Wire library. I’ve an identical downside after I use the Adafruit 7-segment library, after I use their MCP23017 library.
However it’s wiring associated too. If I uncomment the commented out line above however disconnect the I2C system the analog enter begins working once more.
Yet another factor I’ve found: it depends upon what number of lights are lit on the I2C board. As soon as all of the lights are lit, the analog pin reads full. When solely a few lights are lit, the values are decrease. It’s just like the voltage going into the mic is greater when the lights are brighter.
For curiosity sake, I attempted the identical factor on a special Arduino (Adafruit Feather 32u4) with the identical consequence (so it is not my Arduino).
[ad_2]