#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
const int buttonPin = 2; // Replace with your button pin
const int buzzerPin = 4; // Replace with your buzzer pin
const TickType_t longPressTime = 2000 / portTICK_PERIOD_MS; // 2 seconds
TaskHandle_t buzzerTaskHandle = NULL;
bool buzzerActive = false;
void buttonTask(void *parameter) {
pinMode(buttonPin, INPUT_PULLUP);
while (1) {
if (digitalRead(buttonPin) == LOW) {
vTaskDelay(longPressTime); // Wait for a long press
if (digitalRead(buttonPin) == LOW) {
// Long press detected, set the buzzer flag
buzzerActive = true;
}
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Short delay to avoid busy-waiting
}
}
void buzzerTask(void *parameter) {
pinMode(buzzerPin, OUTPUT);
while (1) {
if (buzzerActive) {
digitalWrite(buzzerPin, HIGH); // Turn on the buzzer
vTaskDelay(500 / portTICK_PERIOD_MS); // Buzzer on for 0.5 seconds
digitalWrite(buzzerPin, LOW); // Turn off the buzzer
buzzerActive = false; // Reset the buzzer flag
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Short delay to avoid busy-waiting
}
}
void setup() {
xTaskCreate(buttonTask, "Button Task", 2048, NULL, 1, NULL);
xTaskCreate(buzzerTask, "Buzzer Task", 2048, NULL, 1, &buzzerTaskHandle);
}
void loop() {
// Your main code (if any) can go here
}