#include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "hal/gpio_types.h" #include #include "esp_wifi.h" #include "hal/adc_types.h" #include "esp_adc/adc_oneshot.h" #define LED_GPIO_PIN 22 #define SENSOR_CHANNEL ADC_CHANNEL_7 #define TAG "sprayduck" static int adc_raw[10]; void app_main(void) { bool led_state = false; gpio_reset_pin(LED_GPIO_PIN); gpio_set_direction(LED_GPIO_PIN, GPIO_MODE_OUTPUT); adc_oneshot_unit_handle_t handler; adc_oneshot_unit_init_cfg_t adc_config = { .unit_id = ADC_UNIT_1, .ulp_mode = ADC_ULP_MODE_DISABLE, }; ESP_ERROR_CHECK(adc_oneshot_new_unit(&adc_config, &handler)); adc_oneshot_chan_cfg_t channel_config = { .bitwidth = ADC_BITWIDTH_DEFAULT, .atten = ADC_ATTEN_DB_12, }; ESP_ERROR_CHECK(adc_oneshot_config_channel(handler, SENSOR_CHANNEL, &channel_config)); while (1) { //ESP_LOGI("sprayduck", "Flipping LED - state %d!", led_state); if (led_state) { gpio_set_level((gpio_num_t) LED_GPIO_PIN, 0); } else { gpio_set_level((gpio_num_t) LED_GPIO_PIN, 1); } led_state = !led_state; ESP_ERROR_CHECK(adc_oneshot_read(handler, SENSOR_CHANNEL, &adc_raw[0])); ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT_1 + 1, SENSOR_CHANNEL, adc_raw[0]); vTaskDelay(1000 / portTICK_PERIOD_MS); } }