我在我的工作地点使用Arduino + NeoPixels为万圣节制作动画/点亮标志。我的两个功能之一(缓慢,扩散,红色发光)工作正常。另一个 - 心跳减慢 - 从不放慢速度,似乎陷入无限循环。
以下是完整的相关代码:
void loop() {
// HeartBeat Test
for(int bpm=60;bpm >= 0;bpm=bpm-3) {
systolicUp(bpm);
systolicDown(bpm);
diastolicUp(bpm);
diastolicDown(bpm);
restBeat(bpm);
}
}
void systolicUp(int i) {
uint16_t beatSeconds, firstPulse, firstIncrement, j, k;
beatSeconds = 60000/i;
firstPulse = beatSeconds * 0.6;
firstIncrement = firstPulse/170;
for(j=0; j <= 255; j=j+3) {
uint32_t redShade = strip.Color(j, 0, 0);
for (k=0; k<strip.numPixels(); k++) {
strip.setPixelColor(k, redShade);
}
strip.show();
delay(firstIncrement);
}
}
void systolicDown(int i) {
uint16_t beatSeconds, firstPulse, firstIncrement, j, k;
beatSeconds = 60000/i;
firstPulse = beatSeconds * 0.6;
firstIncrement = firstPulse/170;
for(j=255; j >= 0; j=j-3) {
uint32_t redShade = strip.Color(j, 0, 0);
for (k=0; k<strip.numPixels(); k++) {
strip.setPixelColor(k, redShade);
}
strip.show();
delay(firstIncrement);
}
}
void diastolicUp(int i) {
uint16_t beatSeconds, secondPulse, secondIncrement, j, k;
beatSeconds = 60000/i;
secondPulse = beatSeconds * 0.3;
secondIncrement = secondPulse/170;
for(j=0; j <= 255; j=j+3) {
uint32_t redShade = strip.Color(j, 0, 0);
for (k=0; k<strip.numPixels(); k++) {
strip.setPixelColor(k, redShade);
}
strip.show();
delay(secondIncrement);
}
}
void diastolicDown(int i) {
beatSeconds = 60000/i;
uint16_t beatSeconds, secondPulse, secondIncrement, j, k;
secondPulse = beatSeconds * 0.3;
secondIncrement = secondPulse/170;
for(j=255; j >= 0; j=j-3) {
uint32_t redShade = strip.Color(j, 0, 0);
for (k=0; k<strip.numPixels(); k++) {
strip.setPixelColor(k, redShade);
}
strip.show();
delay(secondIncrement);
}
}
void restBeat(int i) {
uint16_t beatSeconds, rest, g;
beatSeconds = 60000/i;
rest = beatSeconds * 0.1;
for (g=0; g<strip.numPixels(); g++) {
strip.setPixelColor(g, 0, 0, 0);
}
strip.show();
delay(rest);
}
通过数学计算,我应该每分钟交换一定数量的节拍,计算每次拍子的秒数,光脉冲超过60%,脉冲再次超过30%,并且然后沉默10%。这应该发生20次,每次拍打逐渐变慢,直到它完全停止。
相反,我每秒钟都会稳定闪烁。我确定它最终会成为我在NeoPixel文档中完全忽略或遗漏的东西。但是,由于我要么忽视它,要么完全错过它,我们将不胜感激。 :)
答案 0 :(得分:1)
void diastolicDown(int i) {
beatSeconds = 60000/i;
uint16_t beatSeconds, secondPulse, secondIncrement, j, k;
在这里,您尝试在声明之前为beatSeconds
分配值。