From 052ed903176bc17b7a2e564cbff5f25bf1707958 Mon Sep 17 00:00:00 2001 From: Christoph Sterz Date: Mon, 24 Jul 2023 21:49:18 +0200 Subject: [PATCH] Hund mit Progress --- src/main.cpp | 58 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 709ba73..759aa8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,7 +33,7 @@ int16_t sBuffer[bufferLen]; #define LEDC_OUTPUT_IO (0) // Define the output GPIO #define LEDC_CHANNEL LEDC_CHANNEL_0 #define LEDC_DUTY_RES LEDC_TIMER_10_BIT // Set duty resolution to 13 bits -#define LEDC_DUTY (4095) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095 +#define LEDC_DUTY (4095) #define LEDC_FREQUENCY (20000) // Frequency in Hertz. Set frequency at 20 kHz void i2s_install() { @@ -118,7 +118,7 @@ void setup() { float thresholdMax = 1000; -float thresholdMin = 30; +float thresholdMin = 75; float threshold=thresholdMax; void loop() { @@ -139,29 +139,39 @@ void loop() { // Average the data reading mean /= samples_read; - - // Print to serial plotter - Serial.print(-threshold); - Serial.print(" "); - Serial.print(threshold); - Serial.print(" "); - Serial.println(mean); - if ( mean < threshold) threshold = std::max(thresholdMin, threshold * 0.9999f); - - auto dogOnDuty = std::max(0.0f, threshold - thresholdMin); - if(dogOnDuty < 500) dogOnDuty = 1; - ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, dogOnDuty)); - // Update duty to apply the new value - ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL)); - if ( std::abs(mean) > threshold) { - // DOG WAKEUP - threshold = thresholdMax; - // Set the LEDC peripheral configuration - // Set duty to 50% - //ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY)); - // Update duty to apply the new value - //ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL)); + if (std::abs(mean) < thresholdMin) { + Serial.print(mean); + Serial.print(" "); + Serial.println(thresholdMin); + delay(1); + return; + } + + Serial.print("CONTINUE"); + for(float progress=0; progress<1.0f; progress+=0.001f) { + int dogOnDuty; + if(progress < 0.2) //20% + { + dogOnDuty = progress * 5 * 1024; + } else if (progress < 0.8) //20% -80% + { + dogOnDuty = 1024; + } else + { + dogOnDuty = 1024 - ((progress - 0.8) * 5 * 1024); + } + + ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, dogOnDuty)); + // Update duty to apply the new value + ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL)); + + + // Print to serial plotter + Serial.print(progress*1024); + Serial.print(" "); + Serial.println(dogOnDuty); + delayMicroseconds(5000); } } }