//lab 6 part 4
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_log.h"
void IRAM_ATTR button_isr_handler(void* arg);
#define SENSOR_PIN 4 // sensor connected to GPIO4
#define LED_PIN 3 // led connected to pin 3 tgh
bool sensor_called = false; // Global flag for ISR
bool Body_detected = false; //global variable set to false tgh
void app_main() {
// Configuring GPIOs as input and output tgh
gpio_set_direction(SENSOR_PIN, GPIO_MODE_INPUT);
gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
// Setting interrupt on ANY edge (sensor motion) tgh
gpio_set_intr_type(SENSOR_PIN, GPIO_INTR_ANYEDGE);
// Install ISR service (interrupt handling)
gpio_install_isr_service(0);
// Attaching ISR handler to SENSOR_PIN
gpio_isr_handler_add(SENSOR_PIN, button_isr_handler, NULL);
//infinite loop
while (1) {
if (sensor_called) { // Check flag in main loop
sensor_called = false; // Reset flag
//if it is still detecting it executes this if statement
if (Body_detected) {
printf("body detected\n");
gpio_set_direction(LED_PIN, 1);
}
// otherwise it prints no body detected
else {
printf("no body detected\n");
gpio_set_direction(LED_PIN, 0);
}
//printf to prove that the loop is running
}
printf("Main loop running...\n");
vTaskDelay(pdMS_TO_TICKS(100)); // loop continues running
}
}
// Interrupt Service Routine (ISR)
void IRAM_ATTR button_isr_handler(void* arg) {
/*if seomething happens to the original definition of sensor_called (false) this updates
it to true to allow it to enter the if statement in main*/
sensor_called = true;
}