feat: handle drum pad input and create sound
This commit is contained in:
parent
0f6cd913bd
commit
72b33e7705
44
src/main.cpp
44
src/main.cpp
@ -1,18 +1,54 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user