// Painel de alarme de incêndio - Raspberry Pi Pico (Pico SDK, C++)
#include <cstdio>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/pwm.h"
// ===== MAPA DE PINOS (GPIO) =====
// Saídas
constexpr uint PIN_LED_VERDE = 10; // pino físico 14 - painel normal
constexpr uint PIN_LED_VERM = 11; // pino físico 15 - alarme
constexpr uint PIN_LED_AZUL = 12; // pino físico 16 - pressurização
constexpr uint PIN_LED_AMAR = 13; // pino físico 17 - inibido
constexpr uint PIN_LED_BRANCO = 14; // pino físico 19 - sirenes
constexpr uint PIN_BUZZER = 15; // pino físico 20 - sirenes (PWM)
// Entradas digitais (botões ligados ao GND, com pull-up interno)
constexpr uint PIN_BTN_INIBIR = 16; // pino físico 21
constexpr uint PIN_BTN_RESET = 17; // pino físico 22
constexpr uint PIN_BTN_TESTE = 18; // pino físico 24
// Entrada analógica (potenciômetro = temperatura)
constexpr uint PIN_POT = 26; // pino físico 31 (ADC0)
// ===== PARÂMETROS (iguais ao programa original) =====
constexpr int TEMPO_PRESSURIZACAO = 10; // ticks (segundos)
constexpr int TEMPO_SIRENES = 120;
constexpr int SETPOINT = 60; // °C
constexpr uint32_t TICK_MS = 1000; // diminua (ex.: 100) para testar mais rápido
static const uint LEDS[] = {PIN_LED_VERDE, PIN_LED_VERM, PIN_LED_AZUL,
PIN_LED_AMAR, PIN_LED_BRANCO};
// ===== CONFIGURAÇÃO DAS PORTAS =====
void configura_saida(uint pino) {
gpio_init(pino);
gpio_set_dir(pino, GPIO_OUT);
gpio_put(pino, 0);
}
void configura_entrada(uint pino) {
gpio_init(pino);
gpio_set_dir(pino, GPIO_IN);
gpio_pull_up(pino); // apertado = nível baixo
}
void configura_buzzer() {
gpio_set_function(PIN_BUZZER, GPIO_FUNC_PWM);
uint slice = pwm_gpio_to_slice_num(PIN_BUZZER);
pwm_set_clkdiv(slice, 125.0f); // 125 MHz / 125 = 1 MHz
pwm_set_wrap(slice, 999); // 1 MHz / 1000 = 1 kHz
pwm_set_gpio_level(PIN_BUZZER, 0); // começa mudo
pwm_set_enabled(slice, true);
}
void buzzer(bool ligado) {
pwm_set_gpio_level(PIN_BUZZER, ligado ? 500 : 0); // 50% de duty = tom
}
bool pressionado(uint pino) { return !gpio_get(pino); }
int le_temperatura() {
uint16_t bruto = adc_read(); // 0..4095
return bruto * 100 / 4095; // 0..100 °C
}
uint32_t agora_ms() { return to_ms_since_boot(get_absolute_time()); }
int main() {
stdio_init_all();
for (uint pino : LEDS) configura_saida(pino);
configura_buzzer();
configura_entrada(PIN_BTN_INIBIR);
configura_entrada(PIN_BTN_RESET);
configura_entrada(PIN_BTN_TESTE);
adc_init();
adc_gpio_init(PIN_POT);
adc_select_input(0); // GP26 = ADC0
bool alarme = false;
bool inibido = false;
int contagem = 0;
bool antInibir = false, antReset = false;
const uint32_t inicio = agora_ms();
uint32_t ultimoTick = inicio;
auto log = [&](const char *msg) {
printf("%lu s: %s\n", (unsigned long)((agora_ms() - inicio) / 1000), msg);
};
while (true) {
// ambiente
int temperatura = le_temperatura();
// operador: inibir (age no clique)
bool btnInibir = pressionado(PIN_BTN_INIBIR);
if (btnInibir && !antInibir) {
inibido = true;
log("Alarme inibido");
}
antInibir = btnInibir;
// operador: reset (age no clique)
bool btnReset = pressionado(PIN_BTN_RESET);
if (btnReset && !antReset) {
if (temperatura < SETPOINT) {
alarme = false;
inibido = false;
contagem = 0;
log("Reset feito");
} else {
log("Reset recusado");
}
}
antReset = btnReset;
// painel
if (temperatura >= SETPOINT && !alarme) {
alarme = true;
log("Alarme");
}
if (agora_ms() - ultimoTick >= TICK_MS) {
ultimoTick += TICK_MS;
if (alarme && !inibido) contagem++;
if (contagem == TEMPO_PRESSURIZACAO) log("Pressurização ligada");
if (contagem == TEMPO_SIRENES) log("Sirenes acionadas");
}
// saídas
bool pressurizacao = contagem >= TEMPO_PRESSURIZACAO;
bool sirenes = contagem >= TEMPO_SIRENES;
bool teste = pressionado(PIN_BTN_TESTE);
gpio_put(PIN_LED_VERDE, !alarme || teste);
gpio_put(PIN_LED_VERM, alarme || teste);
gpio_put(PIN_LED_AMAR, inibido || teste);
gpio_put(PIN_LED_AZUL, pressurizacao || teste);
gpio_put(PIN_LED_BRANCO, sirenes || teste);
buzzer(sirenes || teste);
sleep_ms(10);
}
}