#define LED 2
#define BT 5
#define POT 34
#define MAX_PRESS 100
SemaphoreHandle_t semaforo;
TaskHandle_t taskTrataBTHandle;
TaskHandle_t xTaskADCHandle;
TaskHandle_t xTaskLEDHandle;
QueueHandle_t xQueue;
volatile int potValue;
void vTaskTrataBT(void * pvParameters) {
int buttonState = 0;
int lastButtonState = 0;
unsigned long lastTime = 0;
unsigned long debounceDelay = 50;
int contador = 0;
while (1) {
xSemaphoreTake(semaforo, portMAX_DELAY);
int currentState = digitalRead(buttonPin);
if (currentState != lastButtonState) {
lastTime = millis();
}
bool value = true;
if ((millis() - lastTime) > debounceDelay) {
if (currentState == HIGH) {
xQueueSend(xQueue, &value, portMAX_DELAY)
}
}
lastButtonState = currentState;
Serial.println("x: " + String(contador));
}
}
void vTaskADC(void * pvParameters) {
while (1) {
xSemaphoreTake(semaforo, portMAX_DELAY);
potValue = analogRead(POT);
Serial.println("Valor do ADC: " + String(potValue));
Serial.println(" ");
}
}
voud vTaskLED(void *parameters) {
bool receivedValue;
xQueueReceive(xQueue, &receivedValue, portMAX_DELAY);
int mappedDelay = map(potValue, 0, 4095, 0, 5000);
digitalWrite(LED, HIGH);
vTaskDelay(pdMS_TO_TICKS(mappedDelay));
digitalWrite(LED, LOW);
vTaskDelay(pdMS_TO_TICKS(mappedDelay));
}
void ISR_CallBack() {
BaseType_t xHighPriorityTaskWoken = pdTRUE;
xSemaphoreGiveFromISR(semaforo, &xHighPriorityTaskWoken);
if (xHighPriorityTaskWoken == pdTRUE)
{
portYIELD_FROM_ISR();
}
}
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
pinMode(BT, INPUT_PULLUP);
xQueue = xQueueCreate(MAX_PRESS, sizeof(bool));
semaforo = xSemaphoreCreateBinary();
xTaskCreate(vTaskTrataBT, "Task BT", configMINIMAL_STACK_SIZE + 1024, NULL, 3, &taskTrataBTHandle);
xTaskCreate(vTaskADC, "TaskADC", configMINIMAL_STACK_SIZE + 1024, NULL, 1, &xTaskADCHandle);
xTaskCreate(vTaskLED, "Task LED", configMINIMAL_STACK_SIZE + 1024, NULL, 1, &xTaskLEDHandle);
}
void loop() {
}