#include <Arduino.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;
// const int dacChannel = 0;
const int buzzerPin = 13;
const int toneFrequency = 1000;
TaskHandle_t buttonTaskHandle = NULL;
TaskHandle_t buzzerTaskHandle = NULL;
bool buzzerState=false;
void controlBuzzer(bool state){
digitalWrite(buzzerPin,state ? HIGH:LOW);
}
void buttonTask(void* parameter) {
for(;;) {
bool buttonState = digitalRead(buttonPin)==LOW;
// if(buttonState){
// buzzerState=!buzzerState;
// controlBuzzer(buzzerState);
// Serial.println("Button is activated");
// }
if(digitalRead(buttonPin)==LOW){
Serial.println("Button is activated");
}
// if (digitalRead(buttonPin) == LOW) {
// activateBuzzer()=true;
// // dacWrite(dacChannel, 0);
// }
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void buzzerTask(void *parameter) {
digitalWrite(buzzerPin, LOW);
for (;;) {
// void activateBuzzer () {
// digitalWrite(buzzerPin, HIGH);
// // delay(1000);
// tone(buzzerPin,440,toneFrequency);
// delay(1000);
// // digitalWrite(buzzerPin, LOW);
// activateBuzzer=false;
// }
controlBuzzer(buzzerState);
Serial.println("Buzzer is activated");
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
// // dacWrite(dacChannel, 0);
xTaskCreate(buttonTask, "ButtonTask", 1000, NULL, 1, &buttonTaskHandle);
xTaskCreate(buzzerTask, "BuzzerTask", 1000, NULL, 1, &buzzerTaskHandle);
vTaskStartScheduler();
}
void loop(){}
#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) {
// Button is pressed
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
}