#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include <math.h>
#include "ssd1306.h"
#include "hardware/i2c.h"
#define BETA 3950
#define R_SERIE 10000.0
#define T0 298.15
#define R0 10000.0
#define TRIG_PIN 27
#define ECHO_PIN 22
#define TEMP_SENSOR_PIN 26 // Usando GPIO 26 (ADC0)
#define BUZZER_PIN 17
#define LED_RED_PIN 13
#define LED_GREEN_PIN 14
#define LED_BLUE_PIN 12
#define SDA_PIN 0
#define SCL_PIN 1
#define I2C_PORT i2c0
#define UART_ID uart0
#define BAUD_RATE 115200
#define TX_PIN 0
#define RX_PIN 1
ssd1306_t oled;
float measure_distance() {
gpio_put(TRIG_PIN, true);
sleep_us(10);
gpio_put(TRIG_PIN, false);
while (!gpio_get(ECHO_PIN));
absolute_time_t start_time = get_absolute_time();
while (gpio_get(ECHO_PIN));
absolute_time_t end_time = get_absolute_time();
uint64_t pulse_duration = absolute_time_diff_us(start_time, end_time);
return pulse_duration * 0.0343 / 2;
}
float read_temperature() {
adc_select_input(0); // Seleciona o canal ADC0 (GPIO 26)
uint16_t raw_value = adc_read();
float voltage = raw_value * 3.3 / 4096.0;
if (voltage < 0.01 || voltage > 3.29) return -273.15;
float resistance = R_SERIE * (voltage / (3.3 - voltage));
float temperature = 1.0 / ((1.0 / T0) + (1.0 / BETA) * log(resistance / R0));
return temperature - 273.15;
}
void set_led_color(bool red, bool green, bool blue) {
gpio_put(LED_RED_PIN, red);
gpio_put(LED_GREEN_PIN, green);
gpio_put(LED_BLUE_PIN, blue);
}
int main() {
stdio_init_all();
sleep_ms(2000);
uart_init(UART_ID, BAUD_RATE);
gpio_set_function(TX_PIN, GPIO_FUNC_UART);
gpio_set_function(RX_PIN, GPIO_FUNC_UART);
gpio_init(TRIG_PIN);
gpio_set_dir(TRIG_PIN, GPIO_OUT);
gpio_init(ECHO_PIN);
gpio_set_dir(ECHO_PIN, GPIO_IN);
gpio_init(BUZZER_PIN);
gpio_set_dir(BUZZER_PIN, GPIO_OUT);
gpio_init(LED_RED_PIN);
gpio_set_dir(LED_RED_PIN, GPIO_OUT);
gpio_init(LED_GREEN_PIN);
gpio_set_dir(LED_GREEN_PIN, GPIO_OUT);
gpio_init(LED_BLUE_PIN);
gpio_set_dir(LED_BLUE_PIN, GPIO_OUT);
adc_init();
adc_gpio_init(TEMP_SENSOR_PIN);
i2c_init(I2C_PORT, 400 * 1000);
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(SCL_PIN, GPIO_FUNC_I2C);
gpio_pull_up(SDA_PIN);
gpio_pull_up(SCL_PIN);
ssd1306_init(&oled, 128, 64, 0x3C, I2C_PORT);
ssd1306_clear(&oled);
while (1) {
float distance = measure_distance();
float temperature = read_temperature();
printf("Distância: %.2f cm, Temperatura: %.2f °C\n", distance, temperature);
char* display_text = NULL;
if (distance > 135.1) {
set_led_color(true, false, false);
gpio_put(BUZZER_PIN, true);
sleep_ms(200);
gpio_put(BUZZER_PIN, false);
display_text = "Porta ABERTA";
} else if (temperature > 10) {
set_led_color(true, false, false);
gpio_put(BUZZER_PIN, true);
sleep_ms(200);
gpio_put(BUZZER_PIN, false);
display_text = "Temp. alta!";
} else if (temperature < 3) {
set_led_color(false, false, true);
gpio_put(BUZZER_PIN, true);
sleep_ms(200);
gpio_put(BUZZER_PIN, false);
display_text = "Temp. baixa!";
} else {
set_led_color(false, true, false);
display_text = "Normal";
}
ssd1306_clear(&oled);
ssd1306_draw_string(&oled, 0, 0, 1, display_text);
ssd1306_show(&oled);
sleep_ms(100);
}
}