项目实践案例征集K10-六年级第27课 定速巡航的控制
本帖最后由 Isabella_J 于 2025-2-20 18:31 编辑注意:本案例中需要另外加两个按钮,如下。按钮1连接到P0端口,按钮2连接到P1端口。
代码如下:
/*!
* MindPlus
* esp32s3bit
*
*/
#include "asr.h"
#include "unihiker_k10.h"
// 动态变量
volatile float mind_n_XunHangSuDu, mind_n_ShiJiSuDu;
// 函数声明
void DF_XunHangJianSu();
void DF_XunHangJiaSu();
// 创建对象
UNIHIKER_K10 k10;
uint8_t screen_dir=2;
ASR asr;
// 主程序开始
void setup() {
k10.begin();
k10.initScreen(screen_dir);
k10.creatCanvas();
pinMode(P0, INPUT);
asr.setAsrSpeed(2);
pinMode(P1, INPUT);
mind_n_XunHangSuDu = 50;
mind_n_ShiJiSuDu = 50;
k10.setScreenBackground(0xFFFFFF);
k10.canvas->canvasText("巡航速度:", 60, 50, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText(mind_n_XunHangSuDu, 60, 100, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText("实际速度:", 60, 150, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText(mind_n_ShiJiSuDu, 60, 200, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
void loop() {
if ((k10.buttonA->isPressed())) {
mind_n_XunHangSuDu += 5;
if ((mind_n_XunHangSuDu>120)) {
mind_n_XunHangSuDu = 120;
}
k10.canvas->canvasText(mind_n_XunHangSuDu, 60, 100, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
if ((k10.buttonB->isPressed())) {
mind_n_XunHangSuDu -= 5;
if ((mind_n_XunHangSuDu<5)) {
mind_n_XunHangSuDu = 5;
}
k10.canvas->canvasText(mind_n_XunHangSuDu, 60, 100, 0x0000FF, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
DF_XunHangJiaSu();
DF_XunHangJianSu();
}
// 自定义函数
void DF_XunHangJianSu() {
mind_n_ShiJiSuDu = mind_n_XunHangSuDu;
if (digitalRead(P0)) {
while (!(mind_n_ShiJiSuDu>=(mind_n_XunHangSuDu + 5))) {
mind_n_ShiJiSuDu += 1;
delay(200);
k10.canvas->canvasText("实际速度:", 60, 150, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText(mind_n_ShiJiSuDu, 60, 200, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
mind_n_ShiJiSuDu = (mind_n_XunHangSuDu + 5);
while (!(mind_n_ShiJiSuDu==mind_n_XunHangSuDu)) {
mind_n_ShiJiSuDu -= 1;
delay(200);
k10.canvas->canvasText("实际速度:", 60, 150, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText(mind_n_ShiJiSuDu, 60, 200, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
asr.speak("速度以设为巡航速度");
delay(800);
asr.speak(mind_n_XunHangSuDu);
}
}
void DF_XunHangJiaSu() {
mind_n_ShiJiSuDu = mind_n_XunHangSuDu;
if (digitalRead(P1)) {
while (!(mind_n_ShiJiSuDu<=(mind_n_XunHangSuDu - 5))) {
mind_n_ShiJiSuDu -= 1;
delay(200);
k10.canvas->canvasText("实际速度:", 60, 150, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText(mind_n_ShiJiSuDu, 60, 200, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
mind_n_ShiJiSuDu = (mind_n_XunHangSuDu - 5);
while (!(mind_n_ShiJiSuDu==mind_n_XunHangSuDu)) {
mind_n_ShiJiSuDu += 1;
delay(200);
k10.canvas->canvasText("实际速度:", 60, 150, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->canvasText(mind_n_ShiJiSuDu, 60, 200, 0xFF0000, k10.canvas->eCNAndENFont24, 50, true);
k10.canvas->updateCanvas();
}
asr.speak("速度以设为巡航速度");
delay(800);
asr.speak(mind_n_XunHangSuDu);
}
}
https://www.bilibili.com/video/BV1ZSKLejEe7/?vd_source=6ec67c00b0383a366d7377cd9c65f93f
页:
[1]