main.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5. #include "esp_log.h"
  6. #include "sdkconfig.h"
  7. #include "sound.h"
  8. /* Use project configuration menu (idf.py menuconfig) to choose the GPIO to blink,
  9. or you can edit the following line and set a number here.
  10. */
  11. #define BLINK_GPIO CONFIG_BLINK_GPIO
  12. #define PHOTO_SENSOR_PIN GPIO_NUM_18
  13. static void configure_led(void)
  14. {
  15. ESP_LOGI("LED", "Example configured to blink GPIO LED!");
  16. gpio_reset_pin(BLINK_GPIO);
  17. /* Set the GPIO as a push/pull output */
  18. gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
  19. }
  20. // GPIO setup
  21. void setup_photo_sensor() {
  22. gpio_config_t io_conf = {
  23. .intr_type = GPIO_INTR_ANYEDGE, // Trigger on both rising & falling edges
  24. .pin_bit_mask = (1ULL << PHOTO_SENSOR_PIN),
  25. .mode = GPIO_MODE_INPUT,
  26. .pull_up_en = GPIO_PULLUP_DISABLE,
  27. .pull_down_en = GPIO_PULLDOWN_DISABLE
  28. };
  29. gpio_config(&io_conf);
  30. ESP_LOGI("PHOTO", "Photomicrosensor configured on GPIO %d", PHOTO_SENSOR_PIN);
  31. }
  32. void read_photo_sensor_task(void *pvParameters) {
  33. static int prevState = 0;
  34. while (1) {
  35. int sensorState = gpio_get_level(PHOTO_SENSOR_PIN);
  36. if (sensorState != prevState) {
  37. prevState = sensorState;
  38. ESP_EARLY_LOGI("PHOTO", "Triggered! State: %d", sensorState);
  39. if( sensorState == 0 ) {
  40. gpio_set_level(BLINK_GPIO, 1);
  41. }
  42. else {
  43. gpio_set_level(BLINK_GPIO, 0);
  44. }
  45. }
  46. vTaskDelay(pdMS_TO_TICKS(50)); // Delay 50ms (20 times per second)
  47. }
  48. }
  49. void app_main(void)
  50. {
  51. /* Configure the peripheral according to the LED type */
  52. configure_led();
  53. setup_photo_sensor();
  54. initSound();
  55. xTaskCreate(read_photo_sensor_task, "read_sensor_task", 2048, NULL, 10, NULL);
  56. }