/* Interrupt Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define LED_RED GPIO_NUM_2
#define LED_GREEN GPIO_NUM_15
#define BTN_RED GPIO_NUM_13
#define BTN_GREEN GPIO_NUM_12
#define BUZZER GPIO_NUM_4 // Pin asignado al buzzer
#define DELAY_TIME 200
volatile bool button_red_pressed = false;
volatile bool button_green_pressed = false;
static void gpio_isr_handler_red(void* arg)
{
button_red_pressed = true;
}
static void gpio_isr_handler_green(void* arg)
{
button_green_pressed = true;
}
void button_config()
{
gpio_install_isr_service(0);
printf("Configuring buttons\n");
gpio_reset_pin(BTN_RED);
gpio_set_direction(BTN_RED, GPIO_MODE_INPUT);
gpio_pullup_en(BTN_RED);
gpio_set_intr_type(BTN_RED, GPIO_INTR_POSEDGE);
gpio_isr_handler_add(BTN_RED, gpio_isr_handler_red, NULL);
gpio_reset_pin(BTN_GREEN);
gpio_set_direction(BTN_GREEN, GPIO_MODE_INPUT);
gpio_pullup_en(BTN_GREEN);
gpio_set_intr_type(BTN_GREEN, GPIO_INTR_POSEDGE);
gpio_isr_handler_add(BTN_GREEN, gpio_isr_handler_green, NULL);
printf("Button config complete\n");
}
void led_config()
{
gpio_reset_pin(LED_RED);
gpio_set_direction(LED_RED, GPIO_MODE_OUTPUT);
gpio_reset_pin(LED_GREEN);
gpio_set_direction(LED_GREEN, GPIO_MODE_OUTPUT);
// Configuración del buzzer
gpio_reset_pin(BUZZER);
gpio_set_direction(BUZZER, GPIO_MODE_OUTPUT);
gpio_set_level(BUZZER, 0); // Asegurarse de que esté apagado al inicio
}
void app_main()
{
uint8_t led_red_value = 0;
uint8_t led_green_value = 0;
button_config();
led_config();
while (1) {
if (button_red_pressed)
{
printf("Red button pressed\n");
button_red_pressed = false;
led_red_value = !led_red_value; // Cambia el estado del LED rojo
gpio_set_level(LED_RED, led_red_value);
if (led_red_value) {
// Si el LED rojo se enciende, activar el buzzer durante 5 segundos
gpio_set_level(BUZZER, 1);
vTaskDelay(5000 / portTICK_PERIOD_MS);
gpio_set_level(BUZZER, 0);
} else {
// Si el LED rojo se apaga, asegurar que el buzzer está apagado
gpio_set_level(BUZZER, 0);
}
}
if (button_green_pressed)
{
printf("Green button pressed\n");
button_green_pressed = false;
led_green_value = !led_green_value; // Cambia el estado del LED verde
gpio_set_level(LED_GREEN, led_green_value);
}
vTaskDelay(DELAY_TIME / portTICK_PERIOD_MS);
}
}