前言
今天了解和学习如何使用 bpibit 上的 按键与 LED 面板
下面示例会用到 NeoPixelBus 库,所以需要到 Arduino 库管理中下载
操作环境
测试使用的开发板: bpi-bit v1.2
应用到的外设: WS2812b
测试使用的软件:Arduino IDE 1.8.9
函数解析
RgbColor(uint8_t r, uint8_t g, uint8_t b) : R(r), G(g), B(b) { };
使用r、g、b值(0-255)构造 rgbcolor
void SetPixelColor(uint16_t indexPixel, typename T_COLOR_FEATURE::ColorObject color)
在指定的位置的灯点亮相应颜色
参数 | 功能 |
---|---|
indexPixel | LED 面板的标号(关于标号可以拉到下面看参考资料) |
color | 自定义的颜色 |
代码解释
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin)
配置 NeoPixelBus 对象
示例代码
#include <NeoPixelBus.h>
const uint16_t PixelCount = 25;
const uint8_t PixelPin = 4;
#define colorSaturation 128
#define delay_ms 500
NeoPixelBus<NeoGrbFeature, Neo800KbpsMethod> strip(PixelCount, PixelPin);
RgbColor red(colorSaturation, 0, 0);
RgbColor green(0, colorSaturation, 0);
RgbColor blue(0, 0, colorSaturation);
RgbColor white(colorSaturation);
RgbColor black(0);
RgbColor num1(0.8 * colorSaturation, 0, 0.2 * colorSaturation);
RgbColor num2(0.5 * colorSaturation, 0, 0.5 * colorSaturation);
RgbColor num3(0.25 * colorSaturation, 0, 0.75 * colorSaturation);
#define ledPower 2 //It's very important for the v1.2, if you use the v1.4 ,you can delete it
// #define BottomA 35
// #define BottomB 27
void setup()
{
Serial.begin(115200); //Serial Port Config 115200-8-N-1
//while (!Serial); // wait for serial attach
Serial.println();
Serial.println("Initializing...");
Serial.flush();
// this resets all the neopixels to an off state
strip.Begin();
strip.Show();
//You can also use BUTTON_A / BUTTON_B
pinMode(BUTTON_A, INPUT);//Botton A Pin Mode :Digital Input
pinMode(BUTTON_B, INPUT);//Botton B Pin Mode :Digital Input
//It's very important for the v1.2, if you use the v1.4 ,you can delete it
pinMode(ledPower,OUTPUT);
digitalWrite(ledPower, HIGH);
}
void loop()
{
if (digitalRead(BUTTON_A) == LOW)
{
delay(10);
if (digitalRead(BUTTON_A) == LOW)
{
strip.SetPixelColor( 6, num1);
strip.SetPixelColor( 7, num1);
strip.SetPixelColor( 8, num1);
strip.SetPixelColor( 9, num1);
strip.SetPixelColor(10, num1);
strip.SetPixelColor(13, num1);
strip.SetPixelColor(16, num1);
strip.SetPixelColor(17, num1);
strip.SetPixelColor(18, num1);
strip.SetPixelColor(19, num1);
strip.Show();
}
else
{
int i;
Serial.println("Turn Off");
for (i = 0; i <= PixelCount - 1; i++)
{
strip.SetPixelColor(i, black);
strip.Show();
}
}
}
if (digitalRead(BUTTON_B) == LOW)
{
delay(10);
if (digitalRead(BUTTON_B) == LOW)
{
//strip.SetPixelColor( 5, num2);
strip.SetPixelColor( 6, num2);
strip.SetPixelColor( 7, num2);
strip.SetPixelColor( 8, num2);
strip.SetPixelColor( 9, num2);
strip.SetPixelColor(10, num2);
strip.SetPixelColor(12, num2);
strip.SetPixelColor(14, num2);
strip.SetPixelColor(15, num2);
strip.SetPixelColor(16, num2);
strip.SetPixelColor(17, num2);
strip.SetPixelColor(18, num2);
strip.SetPixelColor(19, num2);
strip.Show();
}
else
{
int i;
Serial.println("Turn Off");
for (i = 0; i <= PixelCount - 1; i++)
{
strip.SetPixelColor(i, black);
strip.Show();
}
}
}
}
现象
按下按键A,LED面板显示字母A;按下按键B,LED面板显示字母B
参考资料
总结
利用 NeoPixelBus 库点亮一颗颗灯,便能形成图案。在了解到 bpi-bit 有这好看的 LED 矩阵,我萌生了利用 LED 矩阵以及 按键实现贪吃蛇小游戏的想法
需注意
- 在 Arduino IDE 上,开发板的选择需选 BPI-BIT