c++ - 在定义的次数和时间内将引脚设置为高电平的功能
问题描述
我有这个问题,我尝试将输出引脚设置为高电平一段时间。
我打电话hapticFeedback(1000, 2, 1);
变量定义为
unsigned long hapticPreviousMillis = 0;
int hapticState = LOW;
int oneshotHaptic = 0;
这是功能。出于某种原因,我只将引脚设置为高电平而不是闪烁和低电平
void hapticFeedback(int activeLength, int repeats, int oneshotHaptic) {
if (oneshotHaptic == 1) {
for (int x = 0; x <= repeats; x++) {
unsigned long currentMillis = millis();
if (currentMillis - hapticPreviousMillis >= (unsigned long)activeLength) {
hapticPreviousMillis = currentMillis;
if (hapticState == LOW) {
hapticState = HIGH;
}
else {
hapticState = LOW;
}
digitalWrite(haptic, hapticState);
}
}
}
oneshotHaptic = 0;
}
解决方案
所以我想通了,如果其他人在这里寻找这个就是我想出的。它可能不是最流畅的代码,但它完成了我想要做的事情
在循环中我有
if (setOneshotHaptic == 1) {
hapticFeedback(activeLength);
}
触觉功能看起来像这样
void hapticFeedback(int activeLength) {
unsigned long currentMillis = millis();
if (currentMillis - hapticPreviousMillis >= (unsigned long)activeLength) {
hapticPreviousMillis = currentMillis;
if (x == repeats) {
setOneshotHaptic = false;
hapticState = HIGH;
x = 0;
}
if (hapticState == LOW) {
hapticState = HIGH;
x++;
}
else {
hapticState = LOW;
}
digitalWrite(haptic, hapticState);
}
}
每当我想获得触觉反馈时,我都可以定义以下变量
setOneshotHaptic = true;
repeats = 3;
activeLength = 1000;
当达到重复次数时,我放下 oneshot,强制输出为高,使其通过例程变为低,最后重置我的重复计数器。
可能有更好的方法来做到这一点。但是我找不到它们,这对我有用....
推荐阅读
- amp-html - AMP HTML | 链接下拉列表 - 多个链接/依赖项
- algorithm - 我们如何找到具有两个不同“ID”的图的最大连续区域?
- javascript - 电子:具有多种功能的按钮?
- r - 错误:$ 运算符对于没有 $ 的代码的原子向量无效?
- project-reactor - 如何调用 Sinks.Many
.tryEmitNext 来自多个线程? - django-rest-framework - Django Rest 框架面板 - 登录和注销不适用于 Simple JWT
- sorting - 图表 - 日期顺序 - Google 数据洞察
- c# - 为什么它不在编辑器中绘制圆形 Gizmo?
- sql - 我在 pgweb Heroku 中创建一个表,并收到此错误“错误:pq:在“(”)或附近的语法错误
- python - 弃用警告:在 Windows 上使用带有 Python Selenium 和 Chromedriver 的 Brave 浏览器使用选项而不是 chrome_options 错误