/* TASK 3 - EMBARCATECH (20/12/2024)
TAREFA 3 - EMBARCATECH (20/12/2024)
Faculdade: Instituto Federal do Rio Grande do Norte - IFRN
Curso: Curso de Formação Inicial e Continuada em Sistemas Embarcados
Faculty: Federal Institute of Rio Grande do Norte - IFRN
Course: Initial and Continuing Training Course in Embedded Systems
Pt-Br
DESCRIÇÃO DO PROJETO:
Esta atividade serve para criar um sistema de semáforo para pessoas
com deficiência visual, onde, ao apertar o botão, será emitido um
sinal sonoro para que o deficiente possa atravessar a rua.
Para a realização desta tarefa, é necessário usar o RaspBerry Pi Pico W,
juntamente com todos os componentes listados abaixo.
Autor: Eduardo Henryque
Linkedln: https://www.linkedin.com/in/eduardo-henryque-303387320/
----------------------------------------------------------------------------------
English
PROJECT DESCRIPTION:
This activity serves to create a traffic light system for people
with visual impairment, where, when pressing the button, a warning will be emitted
sound signal so that the disabled person can cross the street.
To carry out this task, you need to use the RaspBerry Pi Pico W,
along with all components listed below.
Author: Eduardo Henryque
Linkedln: https://www.linkedin.com/in/eduardo-henryque-303387320/
*/
#include <stdio.h> //Permite usar funções de entrada e saída //Allows using input and output functions
#include "hardware/gpio.h" //Também faz parte do SDK do Raspberry Pi Pico. Ela gerencia os GPIOs (General-Purpose Input/Output), ou seja, os pinos da placa que podem ser configurados como entrada ou saída. //Also part of the Raspberry Pi Pico SDK. It manages GPIOs (General-Purpose Input/Output), i.e., the board pins that can be configured as input or output
#include "pico/stdlib.h" //Específico para esta placa. Ela fornece funções de suporte básicas //Specific to this board. It provides basic support functions
#include "hardware/pwm.h" //Essa biblioteca lida com PWM (Pulse Width Modulation, ou Modulação por Largura de Pulso). //This library handles PWM (Pulse Width Modulation)
#include "hardware/clocks.h" //Essa biblioteca lida com os clocks (relógios) do RP2040. //This library manages the clocks of the RP2040
//Definição dos LEDs //Definition of LEDs
#define LED_VERDE1 10
#define LED_AMARELO 11
#define LED_VERMELHO 12
#define LED_VERDE2 1
//Definição do botão e do buzzer //Definition of the button and the buzzer
#define BOTAO 15
#define BUZZER 16
//Definição da frequência do som que será emitido pelo buzzer. //Definition of the sound frequency to be emitted by the buzzer
#define FREQUENCIA_BUZZER 1500
//Função responsável por detectar o pressionamento do botão. //Function responsible for detecting button press
volatile bool botao_pressionado = false;
//Função para interrupção caso o botão seja acionado/pressionado //Interrupt function in case the button is pressed
void detectar_pedestre() {
botao_pressionado = true;
}
//Função responsável pelo funcionamento do buzzer //Function responsible for buzzer operation
void configurar_buzzer_pwm(uint pino) {
gpio_set_function(pino, GPIO_FUNC_PWM); //Saída de PWM //PWM output
uint slice_num = pwm_gpio_to_slice_num(pino); //Obtém o slice do PWM //Get the PWM slice
//Configurar a frequência do PWM //Configure the PWM frequency
pwm_config config = pwm_get_default_config();
pwm_config_set_clkdiv(&config, clock_get_hz(clk_sys) / (FREQUENCIA_BUZZER * 4096));
pwm_init(slice_num, &config, true);
//Iniciar o PWM no nível baixo //Start PWM at low level
pwm_set_gpio_level(pino, 0);
}
//Função para configurar os LEDs //Function to configure LEDs
void configurar_leds() {
gpio_init(LED_VERDE1);
gpio_init(LED_AMARELO);
gpio_init(LED_VERMELHO);
gpio_init(LED_VERDE2);
gpio_set_dir(LED_VERDE1, GPIO_OUT);
gpio_set_dir(LED_AMARELO, GPIO_OUT);
gpio_set_dir(LED_VERMELHO, GPIO_OUT);
gpio_set_dir(LED_VERDE2, GPIO_OUT);
}
//Função para configurar o botão //Function to configure the button
void configurar_botao() {
gpio_init(BOTAO);
gpio_set_dir(BOTAO, GPIO_IN);
gpio_pull_up(BOTAO);
gpio_set_irq_enabled_with_callback(BOTAO, GPIO_IRQ_EDGE_FALL, true, detectar_pedestre);
}
int main() {
stdio_init_all();
configurar_leds(); //Configura os LEDs //Configure LEDs
configurar_botao(); //Configura o botão //Configure the button
configurar_buzzer_pwm(BUZZER); //Configura o buzzer //Configure the buzzer
while (true) {
if (botao_pressionado) {
//LED amarelo aceso por 5 segundos //Yellow LED on for 5 seconds
gpio_put(LED_VERDE2, 0);
gpio_put(LED_VERDE1, 0);
gpio_put(LED_AMARELO, 1);
gpio_put(LED_VERMELHO, 0);
sleep_ms(5000);
//LED vermelho aceso por 15 segundos //Red LED on for 15 seconds
gpio_put(LED_VERDE2, 1);
gpio_put(LED_VERDE1, 0);
gpio_put(LED_AMARELO, 0);
gpio_put(LED_VERMELHO, 1);
pwm_set_gpio_level(BUZZER, 1024);
sleep_ms(15000);
//LED vermelho e LED dos pedestres apagados //Red LED and pedestrian LED off
pwm_set_gpio_level(BUZZER, 0);
gpio_put(LED_VERDE2, 0);
gpio_put(LED_VERMELHO, 0);
botao_pressionado = false; //Reseta a variável de estado do botão //Reset the button state variable
} else {
for (uint i = 0; i < 8; i++) {
if (botao_pressionado) break; //Verifica se o botão foi pressionado //Check if the button was pressed
gpio_put(LED_VERDE2, 0);
gpio_put(LED_VERDE1, 1);
gpio_put(LED_AMARELO, 0);
gpio_put(LED_VERMELHO, 0);
sleep_ms(1000);
}
for (uint j = 0; j < 2; j++) {
if (botao_pressionado) break; //Verifica se o botão foi pressionado //Check if the button was pressed
gpio_put(LED_VERDE2, 0);
gpio_put(LED_VERDE1, 0);
gpio_put(LED_AMARELO, 1);
gpio_put(LED_VERMELHO, 0);
sleep_ms(1000);
}
for (uint k = 0; k < 10; k++) {
if (botao_pressionado) break; //Verifica se o botão foi pressionado //Check if the button was pressed
gpio_put(LED_VERDE2, 0);
gpio_put(LED_VERDE1, 0);
gpio_put(LED_AMARELO, 0);
gpio_put(LED_VERMELHO, 1);
sleep_ms(1000);
}
}
}
}