0% found this document useful (0 votes)
7 views5 pages

Ultimo Projeto

This document contains a C program for a Raspberry Pi Pico that implements a menu system to control a joystick, buzzer, and RGB LED. It utilizes I2C communication to drive an SSD1306 display and allows the user to navigate through options using a joystick. The program includes functions to handle each menu option, enabling PWM for the buzzer and RGB LED, and updates the display accordingly.

Uploaded by

luis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Ultimo Projeto

This document contains a C program for a Raspberry Pi Pico that implements a menu system to control a joystick, buzzer, and RGB LED. It utilizes I2C communication to drive an SSD1306 display and allows the user to navigate through options using a joystick. The program includes functions to handle each menu option, enabling PWM for the buzzer and RGB LED, and updates the display accordingly.

Uploaded by

luis
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <stdio.

h>

#include "pico/stdlib.h"

#include "hardware/i2c.h"

#include "hardware/pwm.h"

#include "pico/time.h"

#include "ssd1306.h"

#define I2C_PORT i2c0

#define SDA_PIN 4

#define SCL_PIN 5

#define JOYSTICK_Y 27

#define JOYSTICK_BTN 26

#define BUZZER_PIN 15

#define LED_R_PIN 16

#define LED_G_PIN 17

#define LED_B_PIN 18

const char *menu_items[] = {

"Joystick LED",

"Buzzer PWM",

"LED RGB"

};

#define MENU_SIZE (sizeof(menu_items) / sizeof(menu_items[0]))

int selected_item = 0;

bool in_menu = true;

ssd1306_t display;

void draw_menu() {

ssd1306_clear(&display);
for (int i = 0; i < MENU_SIZE; i++) {

if (i == selected_item) {

ssd1306_draw_string(&display, 0, i * 10, "->", 1);

ssd1306_draw_string(&display, 15, i * 10, menu_items[i], 1);

ssd1306_show(&display);

void joystick_led() {

printf("Executando Joystick LED\n");

sleep_ms(1000);

in_menu = true;

void buzzer_pwm() {

printf("Executando Buzzer PWM\n");

gpio_set_function(BUZZER_PIN, GPIO_FUNC_PWM);

uint slice_num = pwm_gpio_to_slice_num(BUZZER_PIN);

pwm_set_wrap(slice_num, 12500);

pwm_set_gpio_level(BUZZER_PIN, 6250);

pwm_set_enabled(slice_num, true);

sleep_ms(500);

pwm_set_enabled(slice_num, false);

in_menu = true;

void led_rgb() {

printf("Executando LED RGB\n");

gpio_set_function(LED_R_PIN, GPIO_FUNC_PWM);

gpio_set_function(LED_G_PIN, GPIO_FUNC_PWM);
gpio_set_function(LED_B_PIN, GPIO_FUNC_PWM);

uint slice_r = pwm_gpio_to_slice_num(LED_R_PIN);

uint slice_g = pwm_gpio_to_slice_num(LED_G_PIN);

uint slice_b = pwm_gpio_to_slice_num(LED_B_PIN);

pwm_set_wrap(slice_r, 255);

pwm_set_wrap(slice_g, 255);

pwm_set_wrap(slice_b, 255);

pwm_set_gpio_level(LED_R_PIN, 128);

pwm_set_gpio_level(LED_G_PIN, 64);

pwm_set_gpio_level(LED_B_PIN, 192);

pwm_set_enabled(slice_r, true);

pwm_set_enabled(slice_g, true);

pwm_set_enabled(slice_b, true);

sleep_ms(1000);

pwm_set_enabled(slice_r, false);

pwm_set_enabled(slice_g, false);

pwm_set_enabled(slice_b, false);

in_menu = true;

void run_selected_option() {

in_menu = false;

switch (selected_item) {

case 0:

joystick_led();

break;

case 1:

buzzer_pwm();

break;

case 2:

led_rgb();
break;

void button_callback(uint gpio, uint32_t events) {

if (in_menu) {

run_selected_option();

} else {

in_menu = true;

draw_menu();

int main() {

stdio_init_all();

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(&display, 128, 64, I2C_PORT, 0x3C);

gpio_init(JOYSTICK_Y);

gpio_set_dir(JOYSTICK_Y, GPIO_IN);

gpio_pull_up(JOYSTICK_Y);

gpio_init(JOYSTICK_BTN);

gpio_set_dir(JOYSTICK_BTN, GPIO_IN);

gpio_pull_up(JOYSTICK_BTN);
gpio_set_irq_enabled_with_callback(JOYSTICK_BTN, GPIO_IRQ_EDGE_FALL, true,
&button_callback);

gpio_init(BUZZER_PIN);

gpio_set_dir(BUZZER_PIN, GPIO_OUT);

gpio_init(LED_R_PIN);

gpio_init(LED_G_PIN);

gpio_init(LED_B_PIN);

gpio_set_dir(LED_R_PIN, GPIO_OUT);

gpio

draw_menu();_set_dir(LED_G_PIN, GPIO_OUT);

gpio_set_dir(LED_B_PIN, GPIO_OUT);

while (1) {

if (in_menu && gpio_get(JOYSTICK_Y) == 0) {

selected_item = (selected_item + 1) % MENU_SIZE;

draw_menu();

sleep_ms(200);

You might also like