From 72b33e7705c0ad5fde92a6b8356e3db5ec117541 Mon Sep 17 00:00:00 2001 From: Fritz Heiden Date: Tue, 13 May 2025 18:29:25 +0200 Subject: [PATCH] feat: handle drum pad input and create sound --- src/main.cpp | 44 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 37ebe9f..87fbfdb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,18 +1,54 @@ #include const int ONBOARD_LED_PIN = 2; +const int ANALOG_INPUT_PIN = 34; +const int ANALOG_OUTPUT_PIN = 32; + +void handleToneGeneration(void *pvParameters); + +bool toneOn = false; +int frequency = 220; // A4 void setup() { Serial.begin(115200); pinMode(ONBOARD_LED_PIN, OUTPUT); + pinMode(ANALOG_INPUT_PIN, INPUT); + pinMode(ANALOG_OUTPUT_PIN, OUTPUT); + + xTaskCreate(handleToneGeneration, "handleToneGeneration", 2048, NULL, 1, NULL); Serial.println("setup complete"); } void loop() { - digitalWrite(ONBOARD_LED_PIN, HIGH); - delay(1000); - digitalWrite(ONBOARD_LED_PIN, LOW); - delay(1000); + int max = 0; + for (int i = 0; i < 100; i++) + { + int value = analogRead(ANALOG_INPUT_PIN); + if (!toneOn && value > 0) + { + toneOn = true; + } + if (value > max) + { + max = value; + } + delay(1); + } +} + +void handleToneGeneration(void *pvParameters) +{ + int toneDuration = 100; + while (true) + { + if (toneOn) + { + tone(ANALOG_OUTPUT_PIN, frequency, toneDuration); + delay(toneDuration); + toneOn = false; + } + delay(10); + } } \ No newline at end of file