Hund mit Progress

This commit is contained in:
Christoph Sterz 2023-07-24 21:49:18 +02:00
parent 0ed602f61b
commit 052ed90317
1 changed files with 34 additions and 24 deletions

View File

@ -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() {
@ -140,28 +140,38 @@ 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);
if (std::abs(mean) < thresholdMin) {
Serial.print(mean);
Serial.print(" ");
Serial.println(thresholdMin);
delay(1);
return;
}
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));
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);
}
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));
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);
}
}
}