Moin,
den Code habe ich an meine Bedürfnisse angepasst, damit wie im ersten Post die Balken halt hin und her springen nach Randomzahlen. Der Code stamm von diesem Beispiel:
Youtube
Änderung auf mein Setup:
#define _LCDML_cfg_use_ram
// include libs
#include <WiFi.h> // For WiFi
#include <LCDMenuLib2.h>
#include "time.h" // For time things
#include "RTClib.h" // For DS3231 RTC Module
// U8g2lib
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#define SDA_PIN 21
#define SCL_PIN 22
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/2, /* dc=*/U8X8_PIN_NONE, /* reset=*/15);
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2_2(U8G2_R0, /* cs=*/19, /* dc=*/U8X8_PIN_NONE, /* reset=*/U8X8_PIN_NONE);
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2_3(U8G2_R0, /* cs=*/4, /* dc=*/U8X8_PIN_NONE, /* reset=*/U8X8_PIN_NONE);
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2_4(U8G2_R0, /* cs=*/5, /* dc=*/U8X8_PIN_NONE, /* reset=*/U8X8_PIN_NONE);
byte audio_bar_height[7]; // sizes for the individual bars
byte audio_bar_peak[7]; // positions for the individual peaks (lines over the bars)
void setup() {
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(33, OUTPUT);
Serial2.begin(9600);
Wire.begin();
u8g2.begin();
u8g2_2.begin();
u8g2_3.begin();
u8g2_4.begin();
}
void loop() { // main loop
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
digitalWrite(33, HIGH);
u8g2.clearBuffer(); // clear buffer for storing display content in RAM
u8g2_2.clearBuffer(); // clear buffer for storing display content in RAM
u8g2_3.clearBuffer(); // clear buffer for storing display content in RAM
u8g2_4.clearBuffer(); // clear buffer for storing display content in RAM
for (int i=0; i<16; i++) { // loop for every fraquency (63Hz, 160Hz, 400Hz, 1kHz, 2.5kHz, 6.25kHz and 16kHz)
int random_value = random(1024); // calculate random value between 0-1024
audio_bar_height[i] = audio_bar_height[i] + ((map(random_value, 0, 1024, 0, 48) - audio_bar_height[i]) / 4.0); // update the bar with a new value (slowly)
// calculate the peak position
if (audio_bar_peak[i] < audio_bar_height[i]) { // if the peak is below the current bar size
audio_bar_peak[i] = audio_bar_height[i]; // move peak to the new max. position (i.e. size of the bar)
} else if (audio_bar_peak[i] > audio_bar_height[i]) { // if the bar is lower than the peak
audio_bar_peak[i]--; // slowly move the peak down, one pixel every frame
}
u8g2.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
u8g2_2.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2_2.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
u8g2_3.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2_3.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
u8g2_4.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2_4.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
}
u8g2.sendBuffer(); // send buffer from RAM to display controller
u8g2_2.sendBuffer(); // send buffer from RAM to display controller
u8g2_3.sendBuffer(); // send buffer from RAM to display controller
u8g2_4.sendBuffer(); // send buffer from RAM to display controller
}
Meine erste Versuche da die Line anzubinden waren folgende:
#define _LCDML_cfg_use_ram
// include libs
#include <WiFi.h> // For WiFi
#include <LCDMenuLib2.h>
#include "time.h" // For time things
#include "RTClib.h" // For DS3231 RTC Module
// U8g2lib
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#define SDA_PIN 21
#define SCL_PIN 22
#define DEBUG 0
#define L_IN 36 // Audio input A0 Arduino
#define R_IN 39 // Audio input A1 Arduino
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/2, /* dc=*/U8X8_PIN_NONE, /* reset=*/15);
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2_2(U8G2_R0, /* cs=*/19, /* dc=*/U8X8_PIN_NONE, /* reset=*/U8X8_PIN_NONE);
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2_3(U8G2_R0, /* cs=*/4, /* dc=*/U8X8_PIN_NONE, /* reset=*/U8X8_PIN_NONE);
U8G2_GP1294AI_256X48_F_4W_HW_SPI u8g2_4(U8G2_R0, /* cs=*/5, /* dc=*/U8X8_PIN_NONE, /* reset=*/U8X8_PIN_NONE);
const int Yres = 8;
const int gain = 3;
float peaks[64];
char im[64], data[64];
char Rim[64], Rdata[64];
char data_avgs[64];
int debugLoop;
int i;
int load;
byte audio_bar_height[7]; // sizes for the individual bars
byte audio_bar_peak[7]; // positions for the individual peaks (lines over the bars)
void setup() {
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);
pinMode(33, OUTPUT);
Serial2.begin(9600);
Wire.begin();
u8g2.begin();
u8g2_2.begin();
u8g2_3.begin();
u8g2_4.begin();
}
void loop() { // main loop
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
digitalWrite(33, HIGH);
u8g2.clearBuffer(); // clear buffer for storing display content in RAM
u8g2_2.clearBuffer(); // clear buffer for storing display content in RAM
u8g2_3.clearBuffer(); // clear buffer for storing display content in RAM
u8g2_4.clearBuffer(); // clear buffer for storing display content in RAM
for (int i = 0; i < 64; i++) { // 64 bins = 32 bins of usable spectrum data
data[i] = ((analogRead(L_IN) / 8 ) - 256); // chose how to interpret the data from analog in
im[i] = 0; // imaginary component
Rdata[i] = ((analogRead(R_IN) / 8 ) - 256); // chose how to interpret the data from analog in
Rim[i] = 0; // imaginary component
// calculate the absolute values of bins in the array - only want positive values
for (int i = 0; i < 40; i++) {
data[i] = sqrt(data[i] * data[i] + im[i] * im[i]);
Rdata[i] = sqrt(Rdata[i] * Rdata[i] + Rim[i] * Rim[i]);
// COPY the Right low-band (0-15) into the Left high-band (16-31) for display ease
if (i < 20) {
data_avgs[i] = data[i];
}
else {
data_avgs[i] = Rdata[i - 20];
}
// Remap values to physical display constraints... that is, 8 display custom character indexes + "_"
data_avgs[i] = constrain(data_avgs[i], 0, 9 - gain); //data samples * range (0-9) = 9
data_avgs[i] = map(data_avgs[i], 0, 9 - gain, 0, Yres); // remap averaged values
}
u8g2.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
u8g2_2.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2_2.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
u8g2_3.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2_3.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
u8g2_4.drawBox(3 + i*16, 48-audio_bar_height[i], 10, audio_bar_height[i]); // draw bar
u8g2_4.drawBox(3 + i*16, 48-audio_bar_peak[i], 10, 1); // draw peak
}
u8g2_2.sendBuffer(); // send buffer from RAM to display controller
u8g2_3.sendBuffer(); // send buffer from RAM to display controller
u8g2_4.sendBuffer(); // send buffer from RAM to display controller
}
void Two16_LCD() {
for (int x = 1; x < 20; x++) { // init 0 to show lowest band overloaded with hum
int y = x + 20; // second display line
if (data_avgs[x] > peaks[x]) peaks[x] = data_avgs[x];
if (data_avgs[y] > peaks[y]) peaks[y] = data_avgs[y];
u8g2.setCursor(12, 12); // draw first (top) row Left
if (peaks[x] == 0) {
u8g2.print("_"); // less LCD artifacts than " "
}
else {
u8g2.print(peaks[x]);
}
u8g2.setCursor(20, 20); // draw second (bottom) row Right
if (peaks[y] == 0) {
u8g2.print("_");
}
else {
u8g2.print(peaks[y]);
}
debugLoop++;
if (DEBUG && (debugLoop > 99)) {
debugLoop = 0;
u8g2.sendBuffer(); // send buffer from RAM to display controller
}
}
}
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void decay(int decayrate) {
int DecayTest = 1;
// reduce the values of the last peaks by 1
if (DecayTest == decayrate) {
for (int x = 0; x < 40; x++) {
peaks[x] = peaks[x] - 1; // subtract 1 from each column peaks
DecayTest = 0;
}
}
DecayTest++;
}
Es gibt noch eine gute Lösung, aber da kriege ich fix_fft.h net zum laufen:
Youtube
Ja versuche immer Beispiele an zu sehen und mich irgendwie durch zu mogeln.
Im ersten Video verwendet er ein Sound Modul, im zweiten direkt am Pin mit ein paar Widerständen. Ich weiß nicht welche Lösung für den ESP32 einfacher wäre. CPU lastig weniger belastbar z.B.
Grüße