1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "hal/gpio_types.h"
#include <driver/gpio.h>
#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);
}
}
|