This commit is contained in:
Alexander Alber 2022-06-07 22:07:07 +02:00
parent 0d4eb26ca3
commit a19f1a0efc

View File

@ -11,7 +11,7 @@
#define SAMPLING_FREQUENCY_CONTROL 1 // check number of times per second if the current band pass is the best one
#define Q 20. // quality factor of band pass filters
#define PI 3.1415926535897932384626433832795
#define n_BP 20 //number of band pass filters
#define n_BP 30 //number of band pass filters
CRGB leds[NUM_LEDS];
@ -53,7 +53,7 @@ double energy_fil = 800.;
float pos_target = NUM_LEDS/2;
float pos_target_filtered = NUM_LEDS/2;
int microphone_offset;
float microphone_offset = 512;
long initial_time;
int active = 15;
@ -65,42 +65,42 @@ int rounds = 0;
void setup() {
//Serial.begin(115200);
Serial.begin(115200);
FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 350);
FastLED.setMaxPowerInVoltsAndMilliamps(5, 300);
// for(int i = 0; i < NUM_LEDS; i++)
// { int brightness = get_value(i, pos_target_filtered);
// leds[i].setRGB(brightness, brightness, brightness); }
// FastLED.show();
//
// long sumsamples = 0;
// for(int j = 1; j<1000; j++)
// {
// int sample = analogRead(1);
// sumsamples += sample;
// delay(1);
// if(j==500)
// {
// sumsamples = 0;
// }
// }
// microphone_offset = sumsamples/500;
for(int i = 0; i < NUM_LEDS; i++)
{ int brightness = get_value(i, pos_target_filtered);
leds[i].setRGB(brightness, brightness, brightness); }
FastLED.show();
long sumsamples = 0;
for(int j = 1; j<1000; j++)
{
int sample = analogRead(1);
sumsamples += sample;
delay(1);
if(j==500)
{
sumsamples = 0;
}
}
microphone_offset = sumsamples/500;
set_filter();
initial_time = micros();
}
void set_filter() {
for(int i = 0; i < n_BP; i++)
{
float frequency = 1.75+i*0.033;
float frequency = 1.75+i*(2.4-1.75)/n_BP;
w0[i] = 2.*PI*frequency/SAMPLING_FREQUENCY_BP;
a[i] = sin(w0[i]/(2.*Q));
b0[i] = a[i];
//b1[i] = 0;
b2[i] = -a[i];
@ -111,24 +111,24 @@ void set_filter() {
}
int get_value(int pos, float pos0) {
if (abs(pos0-pos) > 7)
{ return 0; }
else
{ return (255-abs(pos0-pos)*35); }
if (abs(pos0-pos) > 20) { return 0; }
else { return (40-abs(pos0-pos)*2); }
}
void loop() {
int sample = int(analogRead(1) - microphone_offset);
int sample = int(analogRead(1) - microphone_offset);
energy += abs(sample)*abs(sample);
if (micros() - last_us_bp > sampling_period_bp)
{
Serial.println(sample);
microphone_offset += (analogRead(1)-microphone_offset)*0.001;
//Serial.println(microphone_offset);
last_us_bp += sampling_period_bp;
energy_fil += (energy - energy_fil) * 0.01;
//Serial.println(energy);
@ -143,12 +143,10 @@ void loop() {
yy3[i] = yy2[i];
yy2[i] = yy1[i];
yy1[i] = y[i];
y_fil[i] += (abs(y[i]) - y_fil[i]) * 0.005; //linie der scheitelpunkte
}
float delays = constrain( SAMPLING_FREQUENCY_BP * 0.25/(1.75+active*0.033) , 4., 6.);
float delays = constrain( SAMPLING_FREQUENCY_BP * 0.25/(1.75+active*(2.4-1.75)/n_BP) , 4., 6.);
float delayed = 0;
if (delays > 5)
@ -163,20 +161,29 @@ void loop() {
else
{ angle2 = angle; }
pos_target = map(angle2, -PI, 3*PI, -0.1*NUM_LEDS, NUM_LEDS*1.1);
pos_target = map(angle2, -PI, 3*PI, -0.3*NUM_LEDS, NUM_LEDS*1.5);
if (pos_target > pos_target_filtered)
{ pos_target_filtered += (pos_target - pos_target_filtered)*0.5; }
{ pos_target_filtered += (pos_target - pos_target_filtered)*0.35; }
else
{ pos_target_filtered = pos_target; }
// Serial.print(y_fil[active]);
// Serial.print(",");
// Serial.println(y[active]);
energy = 0;
for(int i = 0; i < NUM_LEDS; i++)
{ int brightness = get_value(i, pos_target_filtered);
leds[i].setRGB(brightness, brightness, brightness); }
{
int brightness = get_value(i, pos_target_filtered);
//leds[i].setRGB(brightness, brightness, brightness);
leds[i].setHSV( 160, (rounds == 6) ? 60 : 0, brightness);
}
FastLED.show();
}
@ -205,7 +212,7 @@ void loop() {
rounds = 0;
candidate = argmax;
}
if(rounds > 5)
if(rounds > 6)
{
rounds = 0;
active = candidate;