基于 Arduino 的 OLED 菜单显示

介绍:

在 Arduino 项目中,使用 OLED 显示屏可以为用户提供直观的交互界面。本文将介绍如何使用 Arduino 和 U8g2 库创建一个简单的 OLED 菜单显示器,以便用户可以浏览和选择不同的菜单选项。

准备材料:

Arduino 开发板
SSD1306 128x64 OLED 显示屏
两个按钮,用于向上和向下滚动菜单选项
代码解析:

首先,我们需要导入 U8g2 库,并设置 OLED 显示屏的引脚和类型。在这个示例中,我们使用 SSD1306 128x64 OLED 显示屏。

#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);

接下来,我们定义了一些变量,如选中的菜单选项、菜单选项的数量、菜单高度和行高等。

int selectedOption = 0;
const int numOptions = 8;  // 假设有8个菜单选项
const char* options[] = {"Option 1", "Option 2", "Option 3", "Option 4", "Option 5", "Option 6", "Option 7", "Option 8"};
const int menuHeight = 64; // 菜单高度(像素)
const int lineHeight = 12; // 每行的高度(像素)
const int visibleOptions = menuHeight / lineHeight; // 可见的菜单选项数量

我们还定义了两个按钮的引脚,用于向上和向下滚动菜单选项,以及滚动的速度。

const int scrollButtonPin = 2; // 向下滚动按钮所连接的引脚
const int scrollUpButtonPin = 3; // 向上滚动按钮所连接的引脚
const int scrollSpeed = 200; // 滚动速度(毫秒)

然后,我们实现了 drawMenu() 函数来绘制菜单。该函数使用 U8g2 库来绘制选项和滚动条,并根据选中的菜单选项进行高亮显示。

void drawMenu() {
  u8g2.firstPage();
  do {
    u8g2.setFont(u8g2_font_6x10_tf);
    int startOption = selectedOption - (visibleOptions / 2);
    if (startOption < 0) {
      startOption = 0;
    } else if (startOption + visibleOptions > numOptions) {
      startOption = numOptions - visibleOptions;
    }
    for (int i = 0; i < visibleOptions; i++) {
      int optionIndex = startOption + i;
      if (optionIndex >= 0 && optionIndex < numOptions) {
        if (optionIndex == selectedOption) {
          u8g2.drawBox(0, i * lineHeight + 2, u8g2.getDisplayWidth() - 4, lineHeight);
          u8g2.setDrawColor(0);
          u8g2.setFontMode(1);
        } else {
          u8g2.setDrawColor(1);
          u8g2.setFontMode(0);
        }
        u8g2.setCursor(2, i * lineHeight + 10);
        u8g2.print(options[optionIndex]);
      }
    }
    // 绘制滚动条
    int scrollBarHeight = menuHeight / numOptions;
    int scrollBarY = (menuHeight - scrollBarHeight) * selectedOption / (numOptions - 1);
    int scrollBarWidth = 2;
    u8g2.setDrawColor(1);
    u8g2.drawBox(u8g2.getDisplayWidth() - scrollBarWidth, scrollBarY, scrollBarWidth, scrollBarHeight);
  } while (u8g2.nextPage());
}

接下来,在 setup() 函数中,我们初始化 OLED 显示屏和设置按钮引脚。

void setup() {
  u8g2.begin();
  u8g2.enableUTF8Print();
  pinMode(scrollButtonPin, INPUT_PULLUP); // 设置向下滚动按钮引脚为输入,带上拉电阻
  pinMode(scrollUpButtonPin, INPUT_PULLUP); // 设置向上滚动按钮引脚为输入,带上拉电阻
}

最后,在 loop() 函数中,我们使用按钮的状态来滚动菜单选项,并调用 drawMenu() 函数来更新显示。

void loop() {
  if (digitalRead(scrollButtonPin) == LOW) {
    selectedOption = (selectedOption + 1) % numOptions;
    drawMenu();
    delay(scrollSpeed);
  }
  if (digitalRead(scrollUpButtonPin) == LOW) {
    selectedOption = (selectedOption - 1 + numOptions) % numOptions;
    drawMenu();
    delay(scrollSpeed);
  }
}

效果
在这里插入图片描述

  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值