#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
const int buttonPin = 5;
// const int buttonPin2 = 5;
// const int buttonPin3 = 18;
// const int buttonPin4 = 19; // Change this to the pin you are using for the button
const int dacChannel = 0; // Change this to the pin you are using for the button
// DAC channel to use
const int toneFrequency = 1000; // Frequency of the tone (Hz)
TaskHandle_t buttonTaskHandle = NULL;
const int buzzerPin = 13; // Replace with your actual pin number
void buzzerTask(void *parameter) {
for (;;) {
if (digitalRead(buttonPin) == LOW) {
digitalWrite(buzzerPin, HIGH); // Turn the buzzer on
delay(1000);
tone(buzzerPin,440,1000);
delay(1000); // Adjust the delay time as needed
digitalWrite(buzzerPin, LOW); // Turn the buzzer off
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Delay to avoid busy-waiting
}
}
void buttonTask(void* parameter) {
while (1) {
if (digitalRead(buttonPin) == LOW) {
// Button is pressed, generate a tone
dacWrite(dacChannel, 2047); // Set DAC to a value for the desired tone
delay(100); // Adjust the duration of the tone as needed
dacWrite(dacChannel, 0); // Turn off the tone
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Adjust the delay as needed
}
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT); // Use INPUT_PULLUP to enable the internal pull-up resistor
// Initialize the DAC
dacWrite(dacChannel, 0); // Start with 0 output
xTaskCreate(buttonTask, "ButtonTask", 1000, NULL, 1, &buttonTaskHandle);
vTaskStartScheduler(); // Start FreeRTOS
}
void loop(){}