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