通过定点亮灭实现下雨效果
2024-02-04205
AI 快速预览详细
本文介绍了如何使用Micro:bit通过定点亮灭实现下雨效果。项目名称为“下雨效果”,硬件清单包括Micro:bit和其内置的LED矩阵。最终效果是通过控制LED的亮灭,模拟雨滴下落的过程。难度等级适中,适合少儿编程入门。

代码
/*!
* MindPlus
* microbit
*
*/
#include <Microbit_Matrix.h>
#include <Microbit_Sensors.h>
// 函数声明
void buttonABCallback();
void ScreenUpEvent();
void buttonACallback();
void buttonBCallback();
// 静态常量
const uint8_t bbcBitmap[][5] = {
{B01010,B10101,B10001,B01010,B00100},
{B01000,B01000,B01000,B01110,B00000},
{B01110,B01010,B01010,B01010,B01110},
{B01010,B01010,B01010,B01010,B00100},
{B01110,B01000,B01110,B01000,B01110},
{B00000,B01010,B00000,B10001,B01110},
{B11111,B10001,B01010,B10001,B01110},
{B11111,B10001,B01010,B10001,B11111},
{B11111,B11111,B10001,B01010,B00000},
{B10101,B01010,B10101,B01010,B10101},
{B11110,B11100,B11100,B11000,B11000},
{B00001,B00011,B00011,B00111,B01111}
};
// 主程序开始
void setup() {
onEvent(ID_BUTTON_AB, PRESS, buttonABCallback);
Sensors.onGesture(Sensors.ScreenUp,ScreenUpEvent);
onEvent(ID_BUTTON_A, PRESS, buttonACallback);
onEvent(ID_BUTTON_B, PRESS, buttonBCallback);
MMatrix.clear();
}
void loop() {
MMatrix.show(bbcBitmap[0]);
delay(1000);
MMatrix.show(bbcBitmap[1]);
delay(1000);
MMatrix.show(bbcBitmap[2]);
delay(1000);
MMatrix.show(bbcBitmap[3]);
delay(1000);
MMatrix.show(bbcBitmap[4]);
}
// 事件回调函数
void buttonABCallback() {
MMatrix.clear();
while (1) {
MMatrix.drawPixel(2, 4, LED_ON);
delay(1000);
MMatrix.drawPixel(2, 4, LED_OFF);
MMatrix.drawPixel(2, 3, LED_ON);
delay(1000);
MMatrix.drawPixel(2, 3, LED_OFF);
MMatrix.drawPixel(2, 3, LED_ON);
delay(1000);
MMatrix.drawPixel(2, 3, LED_OFF);
MMatrix.drawPixel(2, 2, LED_ON);
delay(1000);
MMatrix.drawPixel(2, 2, LED_OFF);
MMatrix.drawPixel(2, 1, LED_ON);
delay(1000);
MMatrix.drawPixel(2, 1, LED_OFF);
MMatrix.drawPixel(2, 0, LED_ON);
delay(1000);
MMatrix.drawPixel(2, 0, LED_OFF);
while (1) {
MMatrix.drawPixel(1, 4, LED_ON);
delay(1000);
MMatrix.drawPixel(1, 4, LED_OFF);
MMatrix.drawPixel(1, 3, LED_ON);
delay(1000);
MMatrix.drawPixel(1, 3, LED_OFF);
MMatrix.drawPixel(1, 2, LED_ON);
delay(1000);
MMatrix.drawPixel(1, 2, LED_OFF);
MMatrix.drawPixel(1, 1, LED_ON);
delay(1000);
MMatrix.drawPixel(1, 1, LED_OFF);
MMatrix.drawPixel(1, 0, LED_ON);
delay(1000);
MMatrix.drawPixel(1, 0, LED_OFF);
yield();
}
yield();
}
}
void ScreenUpEvent() {
MMatrix.clear();
while (1) {
MMatrix.show(bbcBitmap[0]);
delay(3000);
MMatrix.show(bbcBitmap[5]);
yield();
}
}
void buttonACallback() {
MMatrix.clear();
while (1) {
MMatrix.show(bbcBitmap[6]);
delay(1000);
MMatrix.show(bbcBitmap[7]);
delay(1000);
MMatrix.show(bbcBitmap[8]);
delay(1000);
MMatrix.show(bbcBitmap[9]);
yield();
}
}
void buttonBCallback() {
MMatrix.clear();
while (1) {
if (((Sensors.acceleration(Sensors.X))<0)) {
MMatrix.show(bbcBitmap[10]);
}
if ((0<(Sensors.acceleration(Sensors.X)))) {
MMatrix.show(bbcBitmap[11]);
}
yield();
}
}



